From 18769b704b3efca1559bf09d50ed5a01994b9ead Mon Sep 17 00:00:00 2001 From: Alex Landau Date: Wed, 16 Oct 2019 13:15:09 -0700 Subject: [PATCH] Don't fail the test suite when convert-source-map throws an error Fixes #8966. --- .../jest-transform/src/ScriptTransformer.ts | 14 +++++---- .../src/__tests__/script_transformer.test.js | 29 +++++++++++++++++++ 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/packages/jest-transform/src/ScriptTransformer.ts b/packages/jest-transform/src/ScriptTransformer.ts index b2765e909a35..577b1e43f530 100644 --- a/packages/jest-transform/src/ScriptTransformer.ts +++ b/packages/jest-transform/src/ScriptTransformer.ts @@ -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. } } diff --git a/packages/jest-transform/src/__tests__/script_transformer.test.js b/packages/jest-transform/src/__tests__/script_transformer.test.js index 7a826e079600..3d35ea6e3a69 100644 --- a/packages/jest-transform/src/__tests__/script_transformer.test.js +++ b/packages/jest-transform/src/__tests__/script_transformer.test.js @@ -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', }); @@ -434,6 +435,7 @@ describe('ScriptTransformer', () => { collectCoverage: true, }); expect(result.sourceMapPath).toEqual(expect.any(String)); + expect(writeFileAtomic.sync).toBeCalledTimes(2); expect(writeFileAtomic.sync).toBeCalledWith( result.sourceMapPath, sourceMap, @@ -441,6 +443,32 @@ describe('ScriptTransformer', () => { ); }); + 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, @@ -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),