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

Sourcemaps #3458

Merged
merged 10 commits into from
Oct 9, 2017
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"rollup-plugin-node-globals": "^1.1.0",
"rollup-plugin-node-resolve": "^3.0.0",
"slash": "^1.0.0",
"source-map-support": "^0.5.0",
Copy link
Member

Choose a reason for hiding this comment

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

Why is this here? Shouldn't it be a dependency of jest-jasmine2?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right. I just moved it there

"string-length": "^2.0.0",
"strip-ansi": "^4.0.0",
"typescript": "^2.2.2",
Expand Down
20 changes: 20 additions & 0 deletions packages/jest-jasmine2/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {TestResult} from 'types/TestResult';
import type Runtime from 'jest-runtime';

import path from 'path';
import fs from 'fs';
import JasmineReporter from './reporter';
import {install as jasmineAsyncInstall} from './jasmine_async';

Expand Down Expand Up @@ -93,6 +94,25 @@ async function jasmine2(
runtime.requireModule(config.setupTestFrameworkScriptFile);
}

runtime
.requireModule(require.resolve('source-map-support'), 'source-map-support')
.install({
handleUncaughtExceptions: false,
retrieveSourceMap: source => {
if (runtime._sourceMapRegistry[source]) {
Copy link
Member

Choose a reason for hiding this comment

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

I don't think accessing a private field of runtime should be done here :)

try {
return {
map: JSON.parse(
fs.readFileSync(runtime._sourceMapRegistry[source]),
),
url: source,
};
} catch (e) {}
}
return null;
},
});

if (globalConfig.testNamePattern) {
const testNameRegex = new RegExp(globalConfig.testNamePattern, 'i');
env.specFilter = spec => testNameRegex.test(spec.getFullName());
Expand Down
47 changes: 47 additions & 0 deletions packages/jest-runtime/src/__tests__/Runtime-sourceMaps-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @emails oncall+jsinfra
*/
'use strict';

let createRuntime;

describe('Runtime', () => {
beforeEach(() => {
createRuntime = require('createRuntime');
});

describe('requireModule', () => {
it('installs source maps if available', () =>
createRuntime(__filename).then(runtime => {
let hasThrown = false;
const sum = runtime.requireModule(
runtime.__mockRootPath,
'./sourcemaps/out/throwing-mapped-fn.js',
).sum;

try {
sum();
} catch (err) {
hasThrown = true;
/* eslint-disable max-len */
if (process.platform === 'win32') {
expect(err.stack).toMatch(
/^Error: throwing fn\s+at sum.+\\__tests__\\test_root\\sourcemaps\\throwing-mapped-fn.js:10:9/,
);
} else {
expect(err.stack).toMatch(
/^Error: throwing fn\s+at sum.+\/__tests__\/test_root\/sourcemaps\/throwing-mapped-fn.js:10:9/,
);
}
/* eslint-enable max-len */
}
expect(hasThrown).toBe(true);
}));
});
});
25 changes: 24 additions & 1 deletion packages/jest-runtime/src/__tests__/script_transformer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ describe('ScriptTransformer', () => {
).toBeCalledWith(result.sourceMapPath, sourceMap, {encoding: 'utf8'});
});

it('does not write source map if mapCoverage option is false', () => {
it('writes source maps if given by the transformer', () => {
config = Object.assign(config, {
transform: [['^.+\\.js$', 'preprocessor-with-sourcemaps']],
});
Expand All @@ -350,6 +350,29 @@ describe('ScriptTransformer', () => {
map,
});

const result = scriptTransformer.transform('/fruits/banana.js', {
collectCoverage: true,
mapCoverage: false,
});
expect(result.sourceMapPath).toEqual(expect.any(String));
expect(writeFileAtomic.sync).toBeCalledWith(
result.sourceMapPath,
JSON.stringify(map),
{encoding: 'utf8'},
);
});

it('does not write source map if not given by the transformer', () => {
config = Object.assign(config, {
transform: [['^.+\\.js$', 'preprocessor-with-sourcemaps']],
});
const scriptTransformer = new ScriptTransformer(config);

require('preprocessor-with-sourcemaps').process.mockReturnValue({
code: 'content',
map: null,
});

const result = scriptTransformer.transform('/fruits/banana.js', {
collectCoverage: true,
mapCoverage: false,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

export function sum() {
throw new Error('throwing fn');
}
4 changes: 1 addition & 3 deletions packages/jest-runtime/src/script_transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,6 @@ export default class ScriptTransformer {
transformed.map = inlineSourceMap.toJSON();
}
}
} else {
transformed.map = null;
}

// That means that the transform has a custom instrumentation
Expand All @@ -252,7 +250,7 @@ export default class ScriptTransformer {
code = transformed.code;
}

if (instrument && transformed.map && mapCoverage) {
if (transformed.map) {
const sourceMapContent =
typeof transformed.map === 'string'
? transformed.map
Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5432,6 +5432,12 @@ source-map-support@^0.4.15:
dependencies:
source-map "^0.5.6"

source-map-support@^0.5.0:
version "0.5.0"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.0.tgz#2018a7ad2bdf8faf2691e5fddab26bed5a2bacab"
dependencies:
source-map "^0.6.0"

source-map@^0.4.4:
version "0.4.4"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.4.4.tgz#eba4f5da9c0dc999de68032d8b4f76173652036b"
Expand All @@ -5442,6 +5448,10 @@ source-map@^0.5.3, source-map@^0.5.6, source-map@~0.5.1, source-map@~0.5.3:
version "0.5.7"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.5.7.tgz#8a039d2d1021d22d1ea14c80d8ea468ba2ef3fcc"

source-map@^0.6.0:
version "0.6.1"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"

source-map@~0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.2.0.tgz#dab73fbcfc2ba819b4de03bd6f6eaa48164b3f9d"
Expand Down