Skip to content

Commit

Permalink
fix(runtime): generates correct source maps when instrumenting
Browse files Browse the repository at this point in the history
  • Loading branch information
huafu committed Sep 30, 2018
1 parent 8c86fe8 commit 70c4405
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 19 deletions.
30 changes: 27 additions & 3 deletions packages/jest-runtime/src/__tests__/script_transformer.test.js
Expand Up @@ -364,6 +364,14 @@ describe('ScriptTransformer', () => {
mappings: ';AAAA',
version: 3,
};
const expectedMap = {
version: 3,
sources: ['banana.js'],
names: ['content'],
mappings: ';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAAA',
file: 'banana.js',
sourcesContent: ['content'],
};

require('preprocessor-with-sourcemaps').process.mockReturnValue({
code: 'content',
Expand All @@ -374,7 +382,7 @@ describe('ScriptTransformer', () => {
collectCoverage: true,
});
expect(result.sourceMapPath).toEqual(expect.any(String));
const mapStr = JSON.stringify(map);
const mapStr = JSON.stringify(expectedMap);
expect(writeFileAtomic.sync).toBeCalledWith(result.sourceMapPath, mapStr, {
encoding: 'utf8',
});
Expand All @@ -388,8 +396,16 @@ describe('ScriptTransformer', () => {

const sourceMap = JSON.stringify({
mappings: 'AAAA,IAAM,CAAC,GAAW,CAAC,CAAC',
sources: ['banana.js'],
version: 3,
});
const expectedSourceMaps = {
version: 3,
sources: ['banana.js'],
names: [],
mappings:
';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,IAAM,sDAAY,CAAZ,CAAN',
};

const content =
'var x = 1;\n' +
Expand All @@ -404,7 +420,7 @@ describe('ScriptTransformer', () => {
expect(result.sourceMapPath).toEqual(expect.any(String));
expect(writeFileAtomic.sync).toBeCalledWith(
result.sourceMapPath,
sourceMap,
JSON.stringify(expectedSourceMaps),
{encoding: 'utf8'},
);
});
Expand All @@ -419,6 +435,14 @@ describe('ScriptTransformer', () => {
mappings: ';AAAA',
version: 3,
};
const expectedMap = {
version: 3,
sources: ['banana.js'],
names: ['content'],
mappings: ';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAAA',
file: 'banana.js',
sourcesContent: ['content'],
};

require('preprocessor-with-sourcemaps').process.mockReturnValue({
code: 'content',
Expand All @@ -431,7 +455,7 @@ describe('ScriptTransformer', () => {
expect(result.sourceMapPath).toEqual(expect.any(String));
expect(writeFileAtomic.sync).toBeCalledWith(
result.sourceMapPath,
JSON.stringify(map),
JSON.stringify(expectedMap),
{
encoding: 'utf8',
},
Expand Down
42 changes: 26 additions & 16 deletions packages/jest-runtime/src/script_transformer.js
Expand Up @@ -151,24 +151,35 @@ export default class ScriptTransformer {
return transform;
}

_instrumentFile(filename: Path, content: string): string {
return babelTransform(content, {
_instrumentFile(filename: Path, input: TransformedSource): TransformedSource {
const istanbulConfig: any = {
compact: false,
// files outside `cwd` will not be instrumented
cwd: this._config.rootDir,
exclude: [],
useInlineSourceMaps: false,
}
// inject source map if we have it
if (input.map) {
istanbulConfig.inputSourceMap = input.map;
}

// instrument with babel-plugin-istanbul
const transformed = babelTransform(input.code, {
auxiliaryCommentBefore: ' istanbul ignore next ',
babelrc: false,
filename,
plugins: [
[
babelPluginIstanbul,
{
compact: false,
// files outside `cwd` will not be instrumented
cwd: this._config.rootDir,
exclude: [],
useInlineSourceMaps: false,
},
],
[babelPluginIstanbul,istanbulConfig],
],
}).code;
sourceMap: !!input.map,
});

return {
code: transformed.code,
// ensure we return stringified source map if the input was
map: typeof input.map === 'string' ? JSON.stringify(transformed.map) : transformed.map,
};
}

_getRealPath(filepath: Path): Path {
Expand Down Expand Up @@ -243,10 +254,9 @@ export default class ScriptTransformer {
}

if (!transformWillInstrument && instrument) {
code = this._instrumentFile(filename, transformed.code);
} else {
code = transformed.code;
transformed = this._instrumentFile(filename, transformed);
}
code = transformed.code;

if (transformed.map) {
const sourceMapContent =
Expand Down

0 comments on commit 70c4405

Please sign in to comment.