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 5 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 a generated source map is invalid ([#9058](https://github.com/facebook/jest/pull/9058))

### Chore & Maintenance

Expand Down
17 changes: 12 additions & 5 deletions packages/jest-transform/src/ScriptTransformer.ts
Expand Up @@ -296,12 +296,19 @@ 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) {
console.warn(
SimenB marked this conversation as resolved.
Show resolved Hide resolved
`jest-transform: The source map produced for the file ${filename} ` +
'was invalid. Proceeding without source mapping for that file.',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Information on what transformer is responsible may also be helpful here (for the case in which the transformer is consistently failing), but the closest I can find is the transform path, which may look a bit wonky. Is that worth adding, or should we leave the user to infer the transformer from which file is affected?

Copy link
Member

Choose a reason for hiding this comment

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

using _getTransformPath seems reasonable. You can make the path to it relative from config.rootDir, and it shouldn't be too bad

Copy link
Contributor Author

Choose a reason for hiding this comment

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

On further examination, the transform path is used by require and is thus probably a package name or relative path, so I'm including it as-is.

);
}
}

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