Skip to content

Commit

Permalink
Don't fail the test suite when convert-source-map throws an error
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Landau committed Oct 16, 2019
1 parent d74da1a commit 18769b7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
14 changes: 9 additions & 5 deletions packages/jest-transform/src/ScriptTransformer.ts
Expand Up @@ -296,12 +296,16 @@ export default class ScriptTransformer {
}

if (!transformed.map) {
//Could be a potential freeze here.
//See: https://github.com/facebook/jest/pull/5177#discussion_r158883570
const inlineSourceMap = sourcemapFromSource(transformed.code);
try {
//Could be a potential freeze here.
//See: https://github.com/facebook/jest/pull/5177#discussion_r158883570
const inlineSourceMap = sourcemapFromSource(transformed.code);

if (inlineSourceMap) {
transformed.map = inlineSourceMap.toJSON();
if (inlineSourceMap) {
transformed.map = inlineSourceMap.toJSON();
}
} catch (e) {
// Error processing the source map; proceed as if it doesn't exist.
}
}

Expand Down
29 changes: 29 additions & 0 deletions packages/jest-transform/src/__tests__/script_transformer.test.js
Expand Up @@ -406,6 +406,7 @@ describe('ScriptTransformer', () => {
});
expect(result.sourceMapPath).toEqual(expect.any(String));
const mapStr = JSON.stringify(map);
expect(writeFileAtomic.sync).toBeCalledTimes(2);
expect(writeFileAtomic.sync).toBeCalledWith(result.sourceMapPath, mapStr, {
encoding: 'utf8',
});
Expand Down Expand Up @@ -434,13 +435,40 @@ describe('ScriptTransformer', () => {
collectCoverage: true,
});
expect(result.sourceMapPath).toEqual(expect.any(String));
expect(writeFileAtomic.sync).toBeCalledTimes(2);
expect(writeFileAtomic.sync).toBeCalledWith(
result.sourceMapPath,
sourceMap,
{encoding: 'utf8'},
);
});

it('ignores the inlined source map from the preprocessor if parsing it fails', () => {
config = {
...config,
transform: [['^.+\\.js$', 'preprocessor-with-sourcemaps']],
};
const scriptTransformer = new ScriptTransformer(config);

const sourceMap = JSON.stringify({
mappings: 'AAAA,IAAM,CAAC,GAAW,CAAC,CAAC',
version: 3,
});

const content =
'var x = 1;\n' +
'//# sourceMappingURL=data:application/json;base64,' +
Buffer.from(sourceMap).toString('base64').slice(0, 16);

require('preprocessor-with-sourcemaps').process.mockReturnValue(content);

const result = scriptTransformer.transform('/fruits/banana.js', {
collectCoverage: true,
});
expect(result.sourceMapPath).toBeNull();
expect(writeFileAtomic.sync).toBeCalledTimes(1);
});

it('writes source maps if given by the transformer', () => {
config = {
...config,
Expand All @@ -462,6 +490,7 @@ describe('ScriptTransformer', () => {
collectCoverage: true,
});
expect(result.sourceMapPath).toEqual(expect.any(String));
expect(writeFileAtomic.sync).toBeCalledTimes(2);
expect(writeFileAtomic.sync).toBeCalledWith(
result.sourceMapPath,
JSON.stringify(map),
Expand Down

0 comments on commit 18769b7

Please sign in to comment.