Skip to content

Commit 69a3f52

Browse files
author
ismay
authored
Add test for filename prefix on rendering error message (#171)
1 parent 6604a57 commit 69a3f52

File tree

7 files changed

+59
-21
lines changed

7 files changed

+59
-21
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`metalsmith-layouts should prefix rendering errors with the filename 1`] = `"index.html: Something went wrong while rendering"`;
4+
35
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"`;
46

57
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"`;

lib/get-transformer.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const jstransformer = require('jstransformer');
2+
const toTransformer = require('inputformat-to-jstransformer');
3+
4+
/**
5+
* Gets jstransformer for an extension, and caches them
6+
*/
7+
8+
const cache = {};
9+
10+
function getTransformer(ext) {
11+
if (ext in cache) {
12+
return cache[ext];
13+
}
14+
15+
const transformer = toTransformer(ext);
16+
cache[ext] = transformer ? jstransformer(transformer) : false;
17+
18+
return cache[ext];
19+
}
20+
21+
module.exports = getTransformer;

lib/index.js

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,7 @@ const debug = require('debug')('metalsmith-layouts');
22
const match = require('multimatch');
33
const path = require('path');
44
const isUtf8 = require('is-utf8');
5-
const jstransformer = require('jstransformer');
6-
const toTransformer = require('inputformat-to-jstransformer');
7-
8-
/**
9-
* Gets jstransformer for an extension, and caches them
10-
*/
11-
12-
const cache = {};
13-
14-
function getTransformer(ext) {
15-
if (ext in cache) {
16-
return cache[ext];
17-
}
18-
19-
const transformer = toTransformer(ext);
20-
cache[ext] = transformer ? jstransformer(transformer) : false;
21-
22-
return cache[ext];
23-
}
5+
const getTransformer = require('./get-transformer');
246

257
/**
268
* Resolves layouts, in the following order:
@@ -68,7 +50,7 @@ function render({ filename, files, metadata, settings, metalsmith }) {
6850
// Prepend error message with file path
6951
// eslint-disable-next-line no-param-reassign
7052
err.message = `${filename}: ${err.message}`;
71-
return Promise.reject(err);
53+
throw err;
7254
});
7355
}
7456

lib/index.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ const path = require('path');
77
const plugin = require('./index');
88

99
describe('metalsmith-layouts', () => {
10+
beforeEach(() => {
11+
jest.resetModules();
12+
});
13+
1014
it('should apply a single layout to a single file', done => {
1115
const base = path.join(process.cwd(), 'test', 'fixtures', 'single-file');
1216
const actual = path.join(base, 'build');
@@ -327,4 +331,22 @@ describe('metalsmith-layouts', () => {
327331
return done();
328332
});
329333
});
334+
335+
it('should prefix rendering errors with the filename', done => {
336+
jest.doMock('./get-transformer', () =>
337+
jest.fn(() => ({
338+
renderFileAsync: () => Promise.reject(new Error('Something went wrong while rendering'))
339+
}))
340+
);
341+
const plugin = require('./index'); // eslint-disable-line global-require, no-shadow
342+
343+
const base = path.join(process.cwd(), 'test', 'fixtures', 'rendering-error');
344+
const metalsmith = new Metalsmith(base);
345+
346+
return metalsmith.use(plugin()).build(err => {
347+
expect(err).toBeInstanceOf(Error);
348+
expect(err.message).toMatchSnapshot();
349+
done();
350+
});
351+
});
330352
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"eslint-config-prettier": "^2.9.0",
4444
"eslint-plugin-import": "^2.13.0",
4545
"husky": "^0.14.3",
46-
"jest": "^23.5.0",
46+
"jest": "^24.7.1",
4747
"jstransformer-handlebars": "^1.0.0",
4848
"jstransformer-qejs": "^0.2.0",
4949
"lint-staged": "^7.0.0",
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head></head>
4+
<body>
5+
{{{ contents }}}
6+
</body>
7+
</html>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
layout: standard.hbs
3+
---
4+
<h1>The title</h1>

0 commit comments

Comments
 (0)