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

Set up sourcemaps in jest-runner rather than jest-jasmine #6176

Merged
merged 12 commits into from
May 22, 2018
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@
([#5888](https://github.com/facebook/jest/pull/5888))
* `[jest-mock]` [**BREAKING**] Replace timestamps with `invocationCallOrder`
([#5867](https://github.com/facebook/jest/pull/5867))
* `[jest-jasmine2]` Install `sourcemap-support` into normal runtime to catch
runtime errors ([#5945](https://github.com/facebook/jest/pull/5945))
* `[jest-jasmine2]` Added assertion error handling inside `afterAll hook`
([#5884](https://github.com/facebook/jest/pull/5884))
* `[jest-cli]` Remove the notifier actions in case of failure when not in watch
Expand Down Expand Up @@ -167,6 +169,8 @@

### Chore & Maintenance

* `[jest-runner]` Move sourcemap installation from `jest-jasmine2` to
`jest-runner` ([#6176](https://github.com/facebook/jest/pull/6176))
* `[jest-cli]` Use yargs's built-in `version` instead of rolling our own
([#6215](https://github.com/facebook/jest/pull/6215))
* `[docs]` Add explanation on how to mock methods not implemented in JSDOM
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ exports[`prints out info about open handlers 1`] = `
8 |

at Object.<anonymous> (server.js:7:5)
at Object.<anonymous> (__tests__/test.js:3:1)"
at Object.<anonymous> (__tests__/test.js:1:1)"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ exports[`cannot test with no implementation 1`] = `
1 |
2 | it('it', () => {});
> 3 | it('it, no implementation');
| ^
| ^
SimenB marked this conversation as resolved.
Show resolved Hide resolved
4 | test('test, no implementation');
5 |

Expand Down Expand Up @@ -56,7 +56,7 @@ exports[`cannot test with no implementation with expand arg 1`] = `
1 |
2 | it('it', () => {});
> 3 | it('it, no implementation');
| ^
| ^
4 | test('test, no implementation');
5 |

Expand Down
14 changes: 4 additions & 10 deletions integration-tests/__tests__/globals.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ beforeEach(() => {

afterAll(() => cleanup(DIR));

// This function is needed due to differences between Node 6 and 8-9-10 when
// returning the result of the error.
function clean(text) {
return text.replace(/([\r\n])\s+(?=[\r\n])/g, '$1').replace(/\s+\^/g, ' ^');
}

test('basic test constructs', () => {
const filename = 'basic.test-constructs.test.js';
const content = `
Expand Down Expand Up @@ -129,8 +123,8 @@ test('cannot test with no implementation', () => {
expect(status).toBe(1);

const {summary, rest} = extractSummary(stderr, {stripLocation: true});
expect(clean(rest)).toMatchSnapshot();
expect(clean(summary)).toMatchSnapshot();
expect(rest).toMatchSnapshot();
expect(summary).toMatchSnapshot();
});

test('skips with expand arg', () => {
Expand Down Expand Up @@ -207,8 +201,8 @@ test('cannot test with no implementation with expand arg', () => {
expect(status).toBe(1);

const {summary, rest} = extractSummary(stderr, {stripLocation: true});
expect(clean(rest)).toMatchSnapshot();
expect(clean(summary)).toMatchSnapshot();
expect(rest).toMatchSnapshot();
expect(summary).toMatchSnapshot();
});

test('function as descriptor', () => {
Expand Down
1 change: 1 addition & 0 deletions integration-tests/__tests__/stack_trace.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('Stack Trace', () => {
expect(stderr).toMatch(
/ReferenceError: thisIsARuntimeError is not defined/,
);
expect(stderr).toMatch(/> 10 \| thisIsARuntimeError\(\);/);
expect(stderr).toMatch(
/\s+at\s(?:.+?)\s\(__tests__\/runtime_error.test\.js/,
);
Expand Down
4 changes: 1 addition & 3 deletions packages/jest-jasmine2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@
"chalk": "^2.0.1",
"co": "^4.6.0",
"expect": "^22.4.0",
"graceful-fs": "^4.1.11",
"is-generator-fn": "^1.0.0",
"jest-diff": "^22.4.0",
"jest-matcher-utils": "^22.4.0",
"jest-message-util": "^22.4.0",
"jest-snapshot": "^22.4.0",
"jest-util": "^22.4.1",
"pretty-format": "^22.4.0",
"source-map-support": "^0.5.0"
"pretty-format": "^22.4.0"
},
"devDependencies": {
"jest-runtime": "^22.4.2"
Expand Down
32 changes: 4 additions & 28 deletions packages/jest-jasmine2/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import type {TestResult} from 'types/TestResult';
import type Runtime from 'jest-runtime';

import path from 'path';
import fs from 'graceful-fs';
import installEach from './each';
import {getCallsite} from 'jest-util';
import JasmineReporter from './reporter';
Expand Down Expand Up @@ -119,30 +118,6 @@ async function jasmine2(
runtime.requireModule(config.setupTestFrameworkScriptFile);
}

runtime
.requireInternalModule(
require.resolve('source-map-support'),
'source-map-support',
)
.install({
environment: 'node',
handleUncaughtExceptions: false,
retrieveSourceMap: source => {
const sourceMaps = runtime.getSourceMaps();
const sourceMapSource = sourceMaps && sourceMaps[source];

if (sourceMapSource) {
try {
return {
map: JSON.parse(fs.readFileSync(sourceMapSource)),
url: source,
};
} catch (e) {}
}
return null;
},
});

if (globalConfig.enabledTestsMap) {
env.specFilter = spec => {
const suiteMap =
Expand All @@ -157,9 +132,10 @@ async function jasmine2(

runtime.requireModule(testPath);
await env.execute();
SimenB marked this conversation as resolved.
Show resolved Hide resolved
return reporter
.getResults()
.then(results => addSnapshotData(results, snapshotState));

const results = await reporter.getResults();

return addSnapshotData(results, snapshotState);
}

const addSnapshotData = (results, snapshotState) => {
Expand Down
2 changes: 2 additions & 0 deletions packages/jest-runner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"main": "build/index.js",
"dependencies": {
"exit": "^0.1.2",
"graceful-fs": "^4.1.11",
"jest-config": "^22.4.2",
"jest-docblock": "^22.4.0",
"jest-haste-map": "^22.4.2",
Expand All @@ -18,6 +19,7 @@
"jest-runtime": "^22.4.2",
"jest-util": "^22.4.1",
"jest-worker": "^22.2.2",
"source-map-support": "^0.5.6",
"throat": "^4.0.0"
}
}
63 changes: 54 additions & 9 deletions packages/jest-runner/src/run_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type {TestFramework} from 'types/TestRunner';
import type {TestResult} from 'types/TestResult';
import type RuntimeClass from 'jest-runtime';

import fs from 'fs';
import fs from 'graceful-fs';
import {
BufferedConsole,
Console,
Expand All @@ -26,6 +26,7 @@ import jasmine2 from 'jest-jasmine2';
import LeakDetector from 'jest-leak-detector';
import {getTestEnvironment} from 'jest-config';
import * as docblock from 'jest-docblock';
import sourcemapSupport from 'source-map-support';

type RunTestInternalResult = {
leakDetector: ?LeakDetector,
Expand Down Expand Up @@ -116,15 +117,57 @@ async function runTestInternal(
});

const start = Date.now();
await environment.setup();

const sourcemapOptions = {
environment: 'node',
handleUncaughtExceptions: false,
retrieveSourceMap: source => {
const sourceMaps = runtime && runtime.getSourceMaps();
const sourceMapSource = sourceMaps && sourceMaps[source];

if (sourceMapSource) {
try {
return {
map: JSON.parse(fs.readFileSync(sourceMapSource)),
url: source,
};
} catch (e) {}
}
return null;
},
};

// For tests
runtime
.requireInternalModule(
require.resolve('source-map-support'),
'source-map-support',
)
.install(sourcemapOptions);

// For runtime errors
sourcemapSupport.install(sourcemapOptions);

try {
const result: TestResult = await testFramework(
globalConfig,
config,
environment,
runtime,
path,
);
await environment.setup();

let result: TestResult;

try {
result = await testFramework(
globalConfig,
config,
environment,
runtime,
path,
);
} catch (err) {
// Access stack before uninstalling sourcemaps
err.stack;

throw err;
}

const testCount =
result.numPassingTests + result.numFailingTests + result.numPendingTests;

Expand All @@ -151,6 +194,8 @@ async function runTestInternal(
});
} finally {
await environment.teardown();

sourcemapSupport.resetRetrieveHandlers();
SimenB marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8711,9 +8711,9 @@ source-map-support@^0.4.15:
dependencies:
source-map "^0.5.6"

source-map-support@^0.5.0:
version "0.5.5"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.5.tgz#0d4af9e00493e855402e8ec36ebed2d266fceb90"
source-map-support@^0.5.6:
version "0.5.6"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.6.tgz#4435cee46b1aab62b8e8610ce60f788091c51c13"
dependencies:
buffer-from "^1.0.0"
source-map "^0.6.0"
Expand Down