Skip to content

Commit 9a2d74f

Browse files
committed
fix(html): correctly transforms html source when needed
1 parent 902bd9f commit 9a2d74f

3 files changed

Lines changed: 29 additions & 14 deletions

File tree

src/preprocessor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function process(
2929

3030
// This is to support angular 2. See https://github.com/kulshekhar/ts-jest/pull/145
3131
if (isHtmlFile && jestConfig.globals.__TRANSFORM_HTML__) {
32-
src = 'module.exports=`' + src + '`;';
32+
src = 'module.exports=' + JSON.stringify(src) + ';';
3333
}
3434

3535
const processFile =
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`Html transforms transforms html if config.globals.__TRANSFORM_HTML__ is set 1`] = `
4-
"module.exports=\`<div class=\\"html-test\\">
3+
exports[`Html transforms transforms html if config.globals.__TRANSFORM_HTML__ is set: module 1`] = `
4+
"<div class=\\"html-test\\">
55
<span class=\\"html-test__element\\">This is element</span>
6-
</div>\`;"
6+
<code>This is a backtilt \`</code>
7+
</div>"
78
`;
89
9-
exports[`Html transforms transforms html if config.globals.__TRANSFORM_HTML__ is set 2`] = `
10+
exports[`Html transforms transforms html if config.globals.__TRANSFORM_HTML__ is set: source 1`] = `"module.exports=\\"<div class=\\\\\\"html-test\\\\\\">\\\\n <span class=\\\\\\"html-test__element\\\\\\">This is element</span>\\\\n <code>This is a backtilt \`</code>\\\\n</div>\\";"`;
11+
12+
exports[`Html transforms transforms html if config.globals.__TRANSFORM_HTML__ is set: untransformed 1`] = `
1013
"<div class=\\"html-test\\">
1114
<span class=\\"html-test__element\\">This is element</span>
15+
<code>This is a backtilt \`</code>
1216
</div>"
1317
`;
Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
11
import { process } from '../../src/preprocessor';
22

3+
const path = '/path/to/file.html';
4+
const config = { globals: {} as any };
5+
// wrap a transformed source so that we can fake a `require()` on it by calling the returned wrapper
6+
const wrap = (src: string) =>
7+
new Function(`var module={}; ${src} return module.exports;`) as any;
8+
39
const source = `<div class="html-test">
410
<span class="html-test__element">This is element</span>
11+
<code>This is a backtilt \`</code>
512
</div>`;
6-
const path = '/path/to/file.html';
7-
const config = {
8-
globals: {
9-
__TRANSFORM_HTML__: true,
10-
},
11-
};
1213

1314
describe('Html transforms', () => {
1415
it('transforms html if config.globals.__TRANSFORM_HTML__ is set', () => {
15-
expect(process(source, path, config)).toMatchSnapshot();
16-
delete config.globals.__TRANSFORM_HTML__;
17-
expect(process(source, path, config)).toMatchSnapshot();
16+
// get the untransformed version
17+
const untransformed = process(source, path, config);
18+
// ... then the one which should be transformed
19+
config.globals.__TRANSFORM_HTML__ = true;
20+
const transformed = process(source, path, config) as string;
21+
// ... finally the result of a `require('module-with-transformed-version')`
22+
const exported = wrap(transformed)();
23+
24+
expect(exported).toMatchSnapshot('module');
25+
expect(transformed).toMatchSnapshot('source');
26+
expect(untransformed).toMatchSnapshot('untransformed');
27+
// requiring the transformed version should return the same string as the untransformed version
28+
expect(exported).toBe(untransformed);
1829
});
1930
});

0 commit comments

Comments
 (0)