From 4a45111e232a0023cf6e57b823e57bb948ce3c6a Mon Sep 17 00:00:00 2001 From: ismay Date: Sun, 7 Apr 2019 13:34:49 +0200 Subject: [PATCH] Add test for filename prefix on rendering error message --- lib/__snapshots__/index.test.js.snap | 2 ++ lib/{util.js => get-transformer.js} | 4 +--- lib/index.js | 2 +- lib/index.test.js | 22 +++++++++++++++++++++ package.json | 2 +- test/fixtures/rendering-error/src/index.hbs | 0 6 files changed, 27 insertions(+), 5 deletions(-) rename lib/{util.js => get-transformer.js} (91%) create mode 100644 test/fixtures/rendering-error/src/index.hbs diff --git a/lib/__snapshots__/index.test.js.snap b/lib/__snapshots__/index.test.js.snap index b9b0587..9b282db 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-in-place should prefix rendering errors with the filename 1`] = `"index.hbs: Something went wrong while rendering"`; + exports[`metalsmith-in-place 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-in-place#pattern"`; exports[`metalsmith-in-place should return an error when there are no valid files to process 1`] = `"no files to process. See https://www.npmjs.com/package/metalsmith-in-place#no-files-to-process"`; diff --git a/lib/util.js b/lib/get-transformer.js similarity index 91% rename from lib/util.js rename to lib/get-transformer.js index 75a6401..66f3804 100644 --- a/lib/util.js +++ b/lib/get-transformer.js @@ -18,6 +18,4 @@ function getTransformer(ext) { return cache[ext]; } -module.exports = { - getTransformer -}; +module.exports = getTransformer; diff --git a/lib/index.js b/lib/index.js index 76a0853..808d421 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,7 +1,7 @@ const debug = require('debug')('metalsmith-in-place'); const match = require('multimatch'); const isUtf8 = require('is-utf8'); -const { getTransformer } = require('./util'); +const getTransformer = require('./get-transformer'); /** * Engine, renders file contents with all available transformers diff --git a/lib/index.test.js b/lib/index.test.js index 0c992da..03e251e 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-in-place', () => { + beforeEach(() => { + jest.resetModules(); + }); + it('should process a single file', done => { const base = path.join(process.cwd(), 'test', 'fixtures', 'single-file'); const actual = path.join(base, 'build'); @@ -214,4 +218,22 @@ describe('metalsmith-in-place', () => { return done(); }); }); + + it('should prefix rendering errors with the filename', done => { + jest.doMock('./get-transformer', () => + jest.fn(() => ({ + renderAsync: () => 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 3092eb1..fd249b4 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "eslint-config-prettier": "^2.9.0", "eslint-plugin-import": "^2.13.0", "husky": "^0.14.3", - "jest": "^23.4.1", + "jest": "^24.7.1", "jstransformer-handlebars": "^1.0.0", "jstransformer-marked": "^1.0.2", "lint-staged": "^7.0.0", diff --git a/test/fixtures/rendering-error/src/index.hbs b/test/fixtures/rendering-error/src/index.hbs new file mode 100644 index 0000000..e69de29