Skip to content

Commit

Permalink
Don't fail the test suite when convert-source-map throws an error (#9058
Browse files Browse the repository at this point in the history
)
  • Loading branch information
AlexLandau authored and SimenB committed Nov 8, 2019
1 parent bbfef53 commit 85789b5
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
- `[jest-snapshot]` [**BREAKING**] Distinguish empty string from internal snapshot not written ([#8898](https://github.com/facebook/jest/pull/8898))
- `[jest-snapshot]` [**BREAKING**] Remove `report` method and throw matcher errors ([#9049](https://github.com/facebook/jest/pull/9049))
- `[jest-transform]` Properly cache transformed files across tests ([#8890](https://github.com/facebook/jest/pull/8890))
- `[jest-transform]` Don't fail the test suite when a generated source map is invalid ([#9058](https://github.com/facebook/jest/pull/9058))

### Chore & Maintenance

Expand Down
19 changes: 14 additions & 5 deletions packages/jest-transform/src/ScriptTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,21 @@ 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) {
const transformPath = this._getTransformPath(filename);
console.warn(
`jest-transform: The source map produced for the file ${filename} ` +
`by ${transformPath} was invalid. Proceeding without source ` +
'mapping for that file.',
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,5 @@ exports[`ScriptTransformer uses the supplied preprocessor 2`] = `
"({\\"Object.<anonymous>\\":function(module,exports,require,__dirname,__filename,global,jest){module.exports = \\"react\\";
}});"
`;
exports[`ScriptTransformer warns of unparseable inlined source maps from the preprocessor 1`] = `"jest-transform: The source map produced for the file /fruits/banana.js by preprocessor-with-sourcemaps was invalid. Proceeding without source mapping for that file."`;
39 changes: 39 additions & 0 deletions packages/jest-transform/src/__tests__/script_transformer.test.js
Original file line number Diff line number Diff line change
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,50 @@ 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('warns of unparseable inlined source maps from the preprocessor', () => {
const warn = console.warn;
console.warn = jest.fn();

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,
});

// Cut off the inlined map prematurely with slice so the JSON ends abruptly
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);

expect(console.warn).toHaveBeenCalledTimes(1);
expect(console.warn.mock.calls[0][0]).toMatchSnapshot();
console.warn = warn;
});

it('writes source maps if given by the transformer', () => {
config = {
...config,
Expand All @@ -462,6 +500,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 85789b5

Please sign in to comment.