diff --git a/lib/__snapshots__/index.test.js.snap b/lib/__snapshots__/index.test.js.snap index 3477243..e4927c1 100644 --- a/lib/__snapshots__/index.test.js.snap +++ b/lib/__snapshots__/index.test.js.snap @@ -1,5 +1,7 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`metalsmith-layouts should prefix rendering errors with the filename 1`] = `"index.html: Something went wrong while rendering"`; + exports[`metalsmith-layouts should return an error for an invalid pattern 1`] = `"invalid pattern, the pattern option should be a string or array of strings. See https://www.npmjs.com/package/metalsmith-layouts#pattern"`; exports[`metalsmith-layouts should return an error when there are no valid files to process 1`] = `"no files to process. See https://www.npmjs.com/package/metalsmith-layouts#no-files-to-process"`; diff --git a/lib/get-transformer.js b/lib/get-transformer.js new file mode 100644 index 0000000..66f3804 --- /dev/null +++ b/lib/get-transformer.js @@ -0,0 +1,21 @@ +const jstransformer = require('jstransformer'); +const toTransformer = require('inputformat-to-jstransformer'); + +/** + * Gets jstransformer for an extension, and caches them + */ + +const cache = {}; + +function getTransformer(ext) { + if (ext in cache) { + return cache[ext]; + } + + const transformer = toTransformer(ext); + cache[ext] = transformer ? jstransformer(transformer) : false; + + return cache[ext]; +} + +module.exports = getTransformer; diff --git a/lib/index.js b/lib/index.js index a16c85b..5f5ecff 100644 --- a/lib/index.js +++ b/lib/index.js @@ -2,25 +2,7 @@ const debug = require('debug')('metalsmith-layouts'); const match = require('multimatch'); const path = require('path'); const isUtf8 = require('is-utf8'); -const jstransformer = require('jstransformer'); -const toTransformer = require('inputformat-to-jstransformer'); - -/** - * Gets jstransformer for an extension, and caches them - */ - -const cache = {}; - -function getTransformer(ext) { - if (ext in cache) { - return cache[ext]; - } - - const transformer = toTransformer(ext); - cache[ext] = transformer ? jstransformer(transformer) : false; - - return cache[ext]; -} +const getTransformer = require('./get-transformer'); /** * Resolves layouts, in the following order: @@ -68,7 +50,7 @@ function render({ filename, files, metadata, settings, metalsmith }) { // Prepend error message with file path // eslint-disable-next-line no-param-reassign err.message = `${filename}: ${err.message}`; - return Promise.reject(err); + throw err; }); } diff --git a/lib/index.test.js b/lib/index.test.js index 65e837f..114129d 100644 --- a/lib/index.test.js +++ b/lib/index.test.js @@ -7,6 +7,10 @@ const path = require('path'); const plugin = require('./index'); describe('metalsmith-layouts', () => { + beforeEach(() => { + jest.resetModules(); + }); + it('should apply a single layout to a single file', done => { const base = path.join(process.cwd(), 'test', 'fixtures', 'single-file'); const actual = path.join(base, 'build'); @@ -327,4 +331,22 @@ describe('metalsmith-layouts', () => { return done(); }); }); + + it('should prefix rendering errors with the filename', done => { + jest.doMock('./get-transformer', () => + jest.fn(() => ({ + renderFileAsync: () => Promise.reject(new Error('Something went wrong while rendering')) + })) + ); + const plugin = require('./index'); // eslint-disable-line global-require, no-shadow + + const base = path.join(process.cwd(), 'test', 'fixtures', 'rendering-error'); + const metalsmith = new Metalsmith(base); + + return metalsmith.use(plugin()).build(err => { + expect(err).toBeInstanceOf(Error); + expect(err.message).toMatchSnapshot(); + done(); + }); + }); }); diff --git a/package.json b/package.json index 234c93b..3f85e44 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,7 @@ "eslint-config-prettier": "^2.9.0", "eslint-plugin-import": "^2.13.0", "husky": "^0.14.3", - "jest": "^23.5.0", + "jest": "^24.7.1", "jstransformer-handlebars": "^1.0.0", "jstransformer-qejs": "^0.2.0", "lint-staged": "^7.0.0", diff --git a/test/fixtures/rendering-error/layouts/standard.hbs b/test/fixtures/rendering-error/layouts/standard.hbs new file mode 100644 index 0000000..3eddc9f --- /dev/null +++ b/test/fixtures/rendering-error/layouts/standard.hbs @@ -0,0 +1,7 @@ + + + + + {{{ contents }}} + + diff --git a/test/fixtures/rendering-error/src/index.html b/test/fixtures/rendering-error/src/index.html new file mode 100644 index 0000000..0d5ede3 --- /dev/null +++ b/test/fixtures/rendering-error/src/index.html @@ -0,0 +1,4 @@ +--- +layout: standard.hbs +--- +

The title