Skip to content

Commit

Permalink
accept external source maps from transformers
Browse files Browse the repository at this point in the history
  • Loading branch information
jwbay committed Dec 21, 2016
1 parent 851f5bd commit e090153
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
exports[`transform instruments with source map if preprocessor supplies it 1`] = `
"({\"Object.<anonymous>\":function(module,exports,require,__dirname,__filename,global,jest){/* istanbul ignore next */var cov_25u22311x4 = function () {var path = \"/fruits/banana.js\",hash = \"2af15ff13eaa7e17fc0a001b1b75722b8fbeeb74\",global = new Function(\'return this\')(),gcv = \"__coverage__\",coverageData = { path: \"/fruits/banana.js\", statementMap: { \"0\": { start: { line: 1, column: 0 }, end: { line: 1, column: 7 } } }, fnMap: {}, branchMap: {}, s: { \"0\": 0 }, f: {}, b: {}, inputSourceMap: { mappings: \";AAAA\", version: 3 }, _coverageSchema: \"332fd63041d2c1bcb487cc26dd0d5f7d97098a6c\" },coverage = global[gcv] || (global[gcv] = {});if (coverage[path] && coverage[path].hash === hash) {return coverage[path];}coverageData.hash = hash;return coverage[path] = coverageData;}();++cov_25u22311x4.s[0];content;
}});"
`;

exports[`transform transforms a file properly 1`] = `
"({\"Object.<anonymous>\":function(module,exports,require,__dirname,__filename,global,jest){/* istanbul ignore next */var cov_25u22311x4 = function () {var path = \"/fruits/banana.js\",hash = \"04636d4ae73b4b3e24bf6fba39e08c946fd0afb5\",global = new Function(\'return this\')(),gcv = \"__coverage__\",coverageData = { path: \"/fruits/banana.js\", statementMap: { \"0\": { start: { line: 1, column: 0 }, end: { line: 1, column: 26 } } }, fnMap: {}, branchMap: {}, s: { \"0\": 0 }, f: {}, b: {}, _coverageSchema: \"332fd63041d2c1bcb487cc26dd0d5f7d97098a6c\" },coverage = global[gcv] || (global[gcv] = {});if (coverage[path] && coverage[path].hash === hash) {return coverage[path];}coverageData.hash = hash;return coverage[path] = coverageData;}();++cov_25u22311x4.s[0];module.exports = \"banana\";
}});"
Expand Down
29 changes: 29 additions & 0 deletions packages/jest-runtime/src/__tests__/transform-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,25 @@ jest.mock(
{virtual: true},
);

jest.mock(
'preprocessor-with-sourcemaps',
() => {
return {
getCacheKey: jest.fn((content, filename, configStr) => 'ab'),
process: (content, filename, config) => {
return ({
content: 'content',
sourceMap: {
mappings: ';AAAA',
version: 3,
},
});
},
};
},
{virtual: true},
);

jest.mock(
'css-preprocessor',
() => {
Expand Down Expand Up @@ -208,6 +227,16 @@ describe('transform', () => {
expect(vm.Script.mock.calls[2][0]).toMatchSnapshot();
});

it('instruments with source map if preprocessor supplies it', () => {
config = Object.assign(config, {
collectCoverage: true,
transform: [['^.+\\.js$', 'preprocessor-with-sourcemaps']],
});

transform('/fruits/banana.js', config);
expect(vm.Script.mock.calls[0][0]).toMatchSnapshot();
});

it('reads values from the cache', () => {
if (skipOnWindows.test()) {
return;
Expand Down
28 changes: 20 additions & 8 deletions packages/jest-runtime/src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
'use strict';

import type {Config, Path} from 'types/Config';
import type {Transformer} from 'types/Transform';
import type {Transformer, TransformedSource} from 'types/Transform';

const createDirectory = require('jest-util').createDirectory;
const crypto = require('crypto');
Expand Down Expand Up @@ -235,7 +235,7 @@ const stripShebang = content => {
};

const instrumentFile = (
content: string,
transformedSource: TransformedSource,
filename: Path,
config: Config,
): string => {
Expand All @@ -244,7 +244,7 @@ const instrumentFile = (
const babel = require('babel-core');
const babelPluginIstanbul = require('babel-plugin-istanbul').default;

return babel.transform(content, {
return babel.transform(transformedSource.content, {
auxiliaryCommentBefore: ' istanbul ignore next ',
babelrc: false,
filename,
Expand All @@ -254,6 +254,7 @@ const instrumentFile = (
{
cwd: config.rootDir, // files outside `cwd` will not be instrumented
exclude: [],
inputSourceMap: transformedSource.sourceMap,
},
],
],
Expand All @@ -276,21 +277,32 @@ const transformSource = (
return result;
}

result = content;
let transformed: TransformedSource = {
content,
sourceMap: null,
};

if (transform && shouldTransform(filename, config)) {
result = transform.process(result, filename, config, {
const processed = transform.process(content, filename, config, {
instrument,
watch: config.watch,
});

if (typeof processed === 'string') {
transformed.content = processed;
} else {
transformed = processed;
}
}

// That means that the transform has a custom instrumentation
// logic and will handle it based on `config.collectCoverage` option
const transformWillInstrument = transform && transform.canInstrument;
const transformDidInstrument = transform && transform.canInstrument;

if (!transformWillInstrument && instrument) {
result = instrumentFile(result, filename, config);
if (!transformDidInstrument && instrument) {
result = instrumentFile(transformed, filename, config);
} else {
result = transformed.content;
}

writeCacheFile(cacheFilePath, result);
Expand Down
7 changes: 6 additions & 1 deletion types/Transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

import type {Config, Path} from 'types/Config';

export type TransformedSource = {|
content: string,
sourceMap: ?SourceMap,
|};

export type SourceMap = {|
file: string,
mappings: string,
Expand Down Expand Up @@ -41,5 +46,5 @@ export type Transformer = {|
sourcePath: Path,
config: Config,
options?: TransformOptions,
) => string,
) => string | TransformedSource,
|};

0 comments on commit e090153

Please sign in to comment.