diff --git a/src/preprocessor.ts b/src/preprocessor.ts
index 851563e4d6..a5a04e8ad5 100644
--- a/src/preprocessor.ts
+++ b/src/preprocessor.ts
@@ -29,7 +29,7 @@ export function process(
// This is to support angular 2. See https://github.com/kulshekhar/ts-jest/pull/145
if (isHtmlFile && jestConfig.globals.__TRANSFORM_HTML__) {
- src = 'module.exports=`' + src + '`;';
+ src = 'module.exports=' + JSON.stringify(src) + ';';
}
const processFile =
diff --git a/tests/__tests__/__snapshots__/html-transform.spec.ts.snap b/tests/__tests__/__snapshots__/html-transform.spec.ts.snap
index bdea8378b9..1489c73e55 100644
--- a/tests/__tests__/__snapshots__/html-transform.spec.ts.snap
+++ b/tests/__tests__/__snapshots__/html-transform.spec.ts.snap
@@ -1,13 +1,17 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
-exports[`Html transforms transforms html if config.globals.__TRANSFORM_HTML__ is set 1`] = `
-"module.exports=\`
+exports[`Html transforms transforms html if config.globals.__TRANSFORM_HTML__ is set: module 1`] = `
+"
This is element
-
\`;"
+
This is a backtilt \`
+
"
`;
-exports[`Html transforms transforms html if config.globals.__TRANSFORM_HTML__ is set 2`] = `
+exports[`Html transforms transforms html if config.globals.__TRANSFORM_HTML__ is set: source 1`] = `"module.exports=\\"\\\\n This is element\\\\n This is a backtilt \`
\\\\n
\\";"`;
+
+exports[`Html transforms transforms html if config.globals.__TRANSFORM_HTML__ is set: untransformed 1`] = `
"
This is element
+ This is a backtilt \`
"
`;
diff --git a/tests/__tests__/html-transform.spec.ts b/tests/__tests__/html-transform.spec.ts
index 3a2ad22e0f..ec00cd8e09 100644
--- a/tests/__tests__/html-transform.spec.ts
+++ b/tests/__tests__/html-transform.spec.ts
@@ -1,19 +1,30 @@
import { process } from '../../src/preprocessor';
+const path = '/path/to/file.html';
+const config = { globals: {} as any };
+// wrap a transformed source so that we can fake a `require()` on it by calling the returned wrapper
+const wrap = (src: string) =>
+ new Function(`var module={}; ${src} return module.exports;`) as any;
+
const source = `
This is element
+ This is a backtilt \`
`;
-const path = '/path/to/file.html';
-const config = {
- globals: {
- __TRANSFORM_HTML__: true,
- },
-};
describe('Html transforms', () => {
it('transforms html if config.globals.__TRANSFORM_HTML__ is set', () => {
- expect(process(source, path, config)).toMatchSnapshot();
- delete config.globals.__TRANSFORM_HTML__;
- expect(process(source, path, config)).toMatchSnapshot();
+ // get the untransformed version
+ const untransformed = process(source, path, config);
+ // ... then the one which should be transformed
+ config.globals.__TRANSFORM_HTML__ = true;
+ const transformed = process(source, path, config) as string;
+ // ... finally the result of a `require('module-with-transformed-version')`
+ const exported = wrap(transformed)();
+
+ expect(exported).toMatchSnapshot('module');
+ expect(transformed).toMatchSnapshot('source');
+ expect(untransformed).toMatchSnapshot('untransformed');
+ // requiring the transformed version should return the same string as the untransformed version
+ expect(exported).toBe(untransformed);
});
});