Skip to content

Commit a999062

Browse files
committed
test
1 parent 11d9aee commit a999062

6 files changed

Lines changed: 72 additions & 2 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`throw error if test env does not have getVmContext 1`] = `
4+
"
5+
TypeError: Test environment found at \\"<rootDir>/EnvUsingRunScript.js\\" does not export a \\"getVmContext\\" method, which is mandatory from Jest 27. This method is a replacement for \\"runScript\\".
6+
7+
at ../../packages/jest-runner/build/index.js:157:45"
8+
`;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import {resolve} from 'path';
9+
import runJest from '../runJest';
10+
11+
it('throw error if test env does not have getVmContext', () => {
12+
const DIR = resolve(__dirname, '../test-environment-run-script');
13+
const {exitCode, stderr} = runJest(DIR);
14+
15+
expect(stderr.replace(DIR, '<rootDir>')).toMatchSnapshot();
16+
expect(exitCode).toBe(1);
17+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
'use strict';
8+
9+
module.exports = class Env {
10+
constructor() {
11+
this.global = global;
12+
this.moduleMocker = {};
13+
}
14+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
'use strict';
8+
9+
test('dummy', () => {
10+
throw new Error('nothing to see here');
11+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"jest": {
3+
"testEnvironment": "<rootDir>/EnvUsingRunScript.js"
4+
}
5+
}

packages/jest-runner/src/runTest.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ import type {Config} from '@jest/types';
2424
import {getTestEnvironment} from 'jest-config';
2525
import * as docblock from 'jest-docblock';
2626
import LeakDetector from 'jest-leak-detector';
27-
import {formatExecError} from 'jest-message-util';
27+
import {
28+
formatExecError,
29+
formatStackTrace,
30+
separateMessageFromStack,
31+
} from 'jest-message-util';
2832
import type Resolver from 'jest-resolve';
2933
import type RuntimeClass from 'jest-runtime';
3034
import {ErrorWithStack, interopRequireDefault, setGlobal} from 'jest-util';
@@ -144,9 +148,20 @@ async function runTestInternal(
144148
});
145149

146150
if (typeof environment.getVmContext !== 'function') {
147-
throw new Error(
151+
const originalStack = new TypeError(
148152
`Test environment found at "${testEnvironment}" does not export a "getVmContext" method, which is mandatory from Jest 27. This method is a replacement for "runScript".`,
153+
)
154+
.stack!.split('\n')
155+
// Remove this file from the stack (jest-message-utils will keep one line)
156+
.filter(line => line.indexOf(__filename) === -1)
157+
.join('\n');
158+
159+
const {message, stack} = separateMessageFromStack(originalStack);
160+
161+
console.error(
162+
`\n${message}\n` + formatStackTrace(stack, config, {noStackTrace: false}),
149163
);
164+
process.exit(1);
150165
}
151166

152167
const leakDetector = config.detectLeaks

0 commit comments

Comments
 (0)