diff --git a/index.js b/index.js index 1c4ed6e8..aedcc40e 100644 --- a/index.js +++ b/index.js @@ -52,6 +52,12 @@ module.exports = function (fastify, opts, next) { fastify.register(plugin, pluginOptions) } } catch (err) { + // Hack SyntaxError message so that we provide + // the line number to the user, otherwise they + // will be left in the cold. + if (err instanceof SyntaxError) { + err.message += ' at ' + err.stack.split('\n')[0] + } next(err) return } diff --git a/package.json b/package.json index f1cc5375..6f029946 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "Require all plugins in a directory", "main": "index.js", "scripts": { - "test": "standard | snazzy && tap test.js" + "test": "standard | snazzy && tap test/*.js" }, "repository": { "type": "git", @@ -36,5 +36,10 @@ }, "dependencies": { "steed": "^1.1.3" + }, + "standard": { + "ignore": [ + "test/error/lib/a.js" + ] } } diff --git a/test.js b/test/basic.js similarity index 97% rename from test.js rename to test/basic.js index a2a9e574..10075f4e 100644 --- a/test.js +++ b/test/basic.js @@ -7,7 +7,7 @@ t.plan(29) const app = Fastify() -app.register(require('./fixtures/app')) +app.register(require('./basic/app')) app.ready(function (err) { t.error(err) diff --git a/fixtures/app.js b/test/basic/app.js similarity index 91% rename from fixtures/app.js rename to test/basic/app.js index e0947217..f559cc35 100644 --- a/fixtures/app.js +++ b/test/basic/app.js @@ -1,7 +1,7 @@ 'use strict' const path = require('path') -const AutoLoad = require('..') +const AutoLoad = require('../..') module.exports = function (fastify, opts, next) { fastify.register(AutoLoad, { diff --git a/fixtures/defaultPrefix/defaultPrefix.js b/test/basic/defaultPrefix/defaultPrefix.js similarity index 100% rename from fixtures/defaultPrefix/defaultPrefix.js rename to test/basic/defaultPrefix/defaultPrefix.js diff --git a/fixtures/defaultPrefix/noDefaultPrefix.js b/test/basic/defaultPrefix/noDefaultPrefix.js similarity index 100% rename from fixtures/defaultPrefix/noDefaultPrefix.js rename to test/basic/defaultPrefix/noDefaultPrefix.js diff --git a/fixtures/defaultPrefix/overridePrefix.js b/test/basic/defaultPrefix/overridePrefix.js similarity index 100% rename from fixtures/defaultPrefix/overridePrefix.js rename to test/basic/defaultPrefix/overridePrefix.js diff --git a/fixtures/defaultPrefix/prefixed.js b/test/basic/defaultPrefix/prefixed.js similarity index 100% rename from fixtures/defaultPrefix/prefixed.js rename to test/basic/defaultPrefix/prefixed.js diff --git a/fixtures/foo/bar/index.js b/test/basic/foo/bar/index.js similarity index 100% rename from fixtures/foo/bar/index.js rename to test/basic/foo/bar/index.js diff --git a/fixtures/foo/ignored.test.js b/test/basic/foo/ignored.test.js similarity index 100% rename from fixtures/foo/ignored.test.js rename to test/basic/foo/ignored.test.js diff --git a/fixtures/foo/options.js b/test/basic/foo/options.js similarity index 100% rename from fixtures/foo/options.js rename to test/basic/foo/options.js diff --git a/fixtures/foo/prefixed.js b/test/basic/foo/prefixed.js similarity index 100% rename from fixtures/foo/prefixed.js rename to test/basic/foo/prefixed.js diff --git a/fixtures/foo/skipAutoload.js b/test/basic/foo/skipAutoload.js similarity index 100% rename from fixtures/foo/skipAutoload.js rename to test/basic/foo/skipAutoload.js diff --git a/fixtures/foo/something.js b/test/basic/foo/something.js similarity index 100% rename from fixtures/foo/something.js rename to test/basic/foo/something.js diff --git a/fixtures/foo/something.sh b/test/basic/foo/something.sh similarity index 100% rename from fixtures/foo/something.sh rename to test/basic/foo/something.sh diff --git a/test/error.js b/test/error.js new file mode 100644 index 00000000..435a4a5a --- /dev/null +++ b/test/error.js @@ -0,0 +1,15 @@ +'use strict' + +const t = require('tap') +const Fastify = require('fastify') + +t.plan(2) + +const app = Fastify() + +app.register(require('./error/app')) + +app.ready(function (err) { + t.type(err, SyntaxError) + t.match(err.message, /Unexpected token \} at .*\/test\/error\/lib\/a.js:6/) +}) diff --git a/test/error/app.js b/test/error/app.js new file mode 100644 index 00000000..3786b907 --- /dev/null +++ b/test/error/app.js @@ -0,0 +1,12 @@ +'use strict' + +const path = require('path') +const AutoLoad = require('../..') + +module.exports = function (fastify, opts, next) { + fastify.register(AutoLoad, { + dir: path.join(__dirname, 'lib') + }) + + next() +} diff --git a/test/error/lib/a.js b/test/error/lib/a.js new file mode 100644 index 00000000..3e7bd1ac --- /dev/null +++ b/test/error/lib/a.js @@ -0,0 +1,6 @@ +'use strict' + +module.exports = function (app, opts, next) { + // this has a syntax error on purpose + [ +}