Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't fail the test suite when convert-source-map throws an error #9058

Merged
merged 6 commits into from Nov 8, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -39,6 +39,7 @@
- `[jest-snapshot]` Distinguish empty string from external snapshot not written ([#8880](https://github.com/facebook/jest/pull/8880))
- `[jest-snapshot]` [**BREAKING**] Distinguish empty string from internal snapshot not written ([#8898](https://github.com/facebook/jest/pull/8898))
- `[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 convert-source-map throws an error ([#9058](https://github.com/facebook/jest/pull/9058))

### Chore & Maintenance

Expand Down
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should at least warn in this case, no? likely a bug in a transformer. Will also mess up breakpoints and code coverage

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I've added a warning via console.warn (not sure if that's the preferred method, or if we should add chalking, etc.).

}
}

Expand Down
32 changes: 32 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,43 @@ 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,
});

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

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