Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Mar 5, 2021
1 parent 11d9aee commit a999062
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`throw error if test env does not have getVmContext 1`] = `
"
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\\".
at ../../packages/jest-runner/build/index.js:157:45"
`;
17 changes: 17 additions & 0 deletions e2e/__tests__/testEnvironmentRunScript.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import {resolve} from 'path';
import runJest from '../runJest';

it('throw error if test env does not have getVmContext', () => {
const DIR = resolve(__dirname, '../test-environment-run-script');
const {exitCode, stderr} = runJest(DIR);

expect(stderr.replace(DIR, '<rootDir>')).toMatchSnapshot();
expect(exitCode).toBe(1);
});
14 changes: 14 additions & 0 deletions e2e/test-environment-run-script/EnvUsingRunScript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';

module.exports = class Env {
constructor() {
this.global = global;
this.moduleMocker = {};
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';

test('dummy', () => {
throw new Error('nothing to see here');
});
5 changes: 5 additions & 0 deletions e2e/test-environment-run-script/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "<rootDir>/EnvUsingRunScript.js"
}
}
19 changes: 17 additions & 2 deletions packages/jest-runner/src/runTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ import type {Config} from '@jest/types';
import {getTestEnvironment} from 'jest-config';
import * as docblock from 'jest-docblock';
import LeakDetector from 'jest-leak-detector';
import {formatExecError} from 'jest-message-util';
import {
formatExecError,
formatStackTrace,
separateMessageFromStack,
} from 'jest-message-util';
import type Resolver from 'jest-resolve';
import type RuntimeClass from 'jest-runtime';
import {ErrorWithStack, interopRequireDefault, setGlobal} from 'jest-util';
Expand Down Expand Up @@ -144,9 +148,20 @@ async function runTestInternal(
});

if (typeof environment.getVmContext !== 'function') {
throw new Error(
const originalStack = new TypeError(
`Test environment found at "${testEnvironment}" does not export a "getVmContext" method, which is mandatory from Jest 27. This method is a replacement for "runScript".`,
)
.stack!.split('\n')
// Remove this file from the stack (jest-message-utils will keep one line)
.filter(line => line.indexOf(__filename) === -1)
.join('\n');

const {message, stack} = separateMessageFromStack(originalStack);

console.error(
`\n${message}\n` + formatStackTrace(stack, config, {noStackTrace: false}),
);
process.exit(1);
}

const leakDetector = config.detectLeaks
Expand Down

0 comments on commit a999062

Please sign in to comment.