Skip to content

Commit fcd626d

Browse files
authored
fix(runtime): handle missing mocked property (#12213)
1 parent 4eabd9d commit fcd626d

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
### Fixes
66

7-
- `[@jest/transform]` Update dependency package `pirates` to 4.0.4 ([#12136](https://github.com/facebook/jest/pull/12136))
87
- `[jest-environment-node]` Add `AbortSignal` ([#12157](https://github.com/facebook/jest/pull/12157))
98
- `[jest-environment-node]` Add Missing node global `performance` ([#12002](https://github.com/facebook/jest/pull/12002))
9+
- `[jest-runtime]` Handle missing `mocked` property ([#12002](https://github.com/facebook/jest/pull/12002))
10+
- `[@jest/transform]` Update dependency package `pirates` to 4.0.4 ([#12213](https://github.com/facebook/jest/pull/12213))
1011

1112
### Chore & Maintenance
1213

e2e/__tests__/testEnvironment.test.ts

+47-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,18 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import {json as runWithJson} from '../runJest';
8+
import {tmpdir} from 'os';
9+
import * as path from 'path';
10+
import slash = require('slash');
11+
import {cleanup, createEmptyPackage, writeFiles} from '../Utils';
12+
import runJest, {json as runWithJson} from '../runJest';
913
import * as testFixturePackage from '../test-environment/package.json';
1014

15+
const DIR = path.resolve(tmpdir(), 'test-env-no-mocked');
16+
17+
beforeEach(() => cleanup(DIR));
18+
afterAll(() => cleanup(DIR));
19+
1120
it('respects testEnvironment docblock', () => {
1221
expect(testFixturePackage.jest.testEnvironment).toEqual('node');
1322

@@ -16,3 +25,40 @@ it('respects testEnvironment docblock', () => {
1625
expect(result.success).toBe(true);
1726
expect(result.numTotalTests).toBe(3);
1827
});
28+
29+
it('handles missing `mocked` property', () => {
30+
createEmptyPackage(DIR);
31+
writeFiles(DIR, {
32+
'env.js': `
33+
const Node = require('${slash(
34+
require.resolve('jest-environment-node'),
35+
)}');
36+
37+
module.exports = class Thing extends Node {
38+
constructor(...args) {
39+
super(...args);
40+
41+
this.moduleMocker.mocked = undefined;
42+
}
43+
};
44+
`,
45+
'test.js': `
46+
/**
47+
* @jest-environment ./env.js
48+
*/
49+
50+
jest.mocked();
51+
52+
test('halla', () => {
53+
expect(global.thing).toBe('nope');
54+
});
55+
`,
56+
});
57+
58+
const {exitCode, stderr} = runJest(DIR);
59+
60+
expect(exitCode).toBe(1);
61+
expect(stderr).toContain(
62+
'Your test environment does not support `mocked`, please update it.',
63+
);
64+
});

packages/jest-runtime/src/index.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1924,7 +1924,13 @@ export default class Runtime {
19241924
};
19251925
const fn = this._moduleMocker.fn.bind(this._moduleMocker);
19261926
const spyOn = this._moduleMocker.spyOn.bind(this._moduleMocker);
1927-
const mocked = this._moduleMocker.mocked.bind(this._moduleMocker);
1927+
const mocked =
1928+
this._moduleMocker.mocked?.bind(this._moduleMocker) ??
1929+
(() => {
1930+
throw new Error(
1931+
'Your test environment does not support `mocked`, please update it.',
1932+
);
1933+
});
19281934

19291935
const setTimeout = (timeout: number) => {
19301936
if (this._environment.global.jasmine) {

0 commit comments

Comments
 (0)