Skip to content

Commit 7f2f96b

Browse files
authored
refactor(jest-environment)!: remove unnecessary defensive code (#15045)
1 parent 7bffeb5 commit 7f2f96b

File tree

3 files changed

+7
-95
lines changed

3 files changed

+7
-95
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- `[@jest/core]` Support `--outputFile` option for [`--listTests`](https://jestjs.io/docs/cli#--listtests) ([#14980](https://github.com/jestjs/jest/pull/14980))
1717
- `[@jest/core, @jest/test-sequencer]` [**BREAKING**] Exposes `globalConfig` & `contexts` to `TestSequencer` ([#14535](https://github.com/jestjs/jest/pull/14535), & [#14543](https://github.com/jestjs/jest/pull/14543))
1818
- `[@jest/environment]` [**BREAKING**] Remove deprecated `jest.genMockFromModule()` ([#15042](https://github.com/jestjs/jest/pull/15042))
19+
- `[@jest/environment]` [**BREAKING**] Remove unnecessary defensive code ([#15045](https://github.com/jestjs/jest/pull/15045))
1920
- `[jest-environment-jsdom]` [**BREAKING**] Upgrade JSDOM to v22 ([#13825](https://github.com/jestjs/jest/pull/13825))
2021
- `[@jest/environment-jsdom-abstract]` Introduce new package which abstracts over the `jsdom` environment, allowing usage of custom versions of JSDOM ([#14717](https://github.com/jestjs/jest/pull/14717))
2122
- `[jest-environment-node]` Update jest environment with dispose symbols `Symbol` ([#14888](https://github.com/jestjs/jest/pull/14888) & [#14909](https://github.com/jestjs/jest/pull/14909))

e2e/__tests__/testEnvironment.test.ts

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

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';
8+
import {json as runWithJson} from '../runJest';
139
import * as testFixturePackage from '../test-environment/package.json';
1410

15-
const DIR = path.resolve(tmpdir(), 'test-env-no-mocked');
16-
17-
beforeEach(() => cleanup(DIR));
18-
afterAll(() => cleanup(DIR));
19-
2011
it('respects testEnvironment docblock', () => {
2112
expect(testFixturePackage.jest.testEnvironment).toBe('node');
2213

@@ -25,40 +16,3 @@ it('respects testEnvironment docblock', () => {
2516
expect(result.success).toBe(true);
2617
expect(result.numTotalTests).toBe(4);
2718
});
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-
)}').default;
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(globalThis.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

Lines changed: 5 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2265,21 +2265,10 @@ export default class Runtime {
22652265
const isolateModulesAsync = this.isolateModulesAsync.bind(this);
22662266
const fn = this._moduleMocker.fn.bind(this._moduleMocker);
22672267
const spyOn = this._moduleMocker.spyOn.bind(this._moduleMocker);
2268-
const mocked =
2269-
this._moduleMocker.mocked?.bind(this._moduleMocker) ??
2270-
(() => {
2271-
throw new Error(
2272-
'Your test environment does not support `mocked`, please update it.',
2273-
);
2274-
});
2275-
const replaceProperty =
2276-
typeof this._moduleMocker.replaceProperty === 'function'
2277-
? this._moduleMocker.replaceProperty.bind(this._moduleMocker)
2278-
: () => {
2279-
throw new Error(
2280-
'Your test environment does not support `jest.replaceProperty` - please ensure its Jest dependencies are updated to version 29.4 or later',
2281-
);
2282-
};
2268+
const mocked = this._moduleMocker.mocked.bind(this._moduleMocker);
2269+
const replaceProperty = this._moduleMocker.replaceProperty.bind(
2270+
this._moduleMocker,
2271+
);
22832272

22842273
const setTimeout: Jest['setTimeout'] = timeout => {
22852274
this._environment.global[testTimeoutSymbol] = timeout;
@@ -2305,12 +2294,6 @@ export default class Runtime {
23052294
const fakeTimers = _getFakeTimers();
23062295

23072296
if (fakeTimers === this._environment.fakeTimersModern) {
2308-
// TODO: remove this check in Jest 30
2309-
if (typeof fakeTimers.advanceTimersByTimeAsync !== 'function') {
2310-
throw new TypeError(
2311-
'Your test environment does not support async fake timers - please ensure its Jest dependencies are updated to version 29.5 or later',
2312-
);
2313-
}
23142297
await fakeTimers.advanceTimersByTimeAsync(msToRun);
23152298
} else {
23162299
throw new TypeError(
@@ -2334,12 +2317,6 @@ export default class Runtime {
23342317
const fakeTimers = _getFakeTimers();
23352318

23362319
if (fakeTimers === this._environment.fakeTimersModern) {
2337-
// TODO: remove this check in Jest 30
2338-
if (typeof fakeTimers.advanceTimersToNextTimerAsync !== 'function') {
2339-
throw new TypeError(
2340-
'Your test environment does not support async fake timers - please ensure its Jest dependencies are updated to version 29.5 or later',
2341-
);
2342-
}
23432320
await fakeTimers.advanceTimersToNextTimerAsync(steps);
23442321
} else {
23452322
throw new TypeError(
@@ -2369,15 +2346,7 @@ export default class Runtime {
23692346
);
23702347
}
23712348
},
2372-
getSeed: () => {
2373-
// TODO: remove this check in Jest 30
2374-
if (this._globalConfig.seed === undefined) {
2375-
throw new Error(
2376-
'The seed value is not available. Likely you are using older versions of the jest dependencies.',
2377-
);
2378-
}
2379-
return this._globalConfig.seed;
2380-
},
2349+
getSeed: () => this._globalConfig.seed,
23812350
getTimerCount: () => _getFakeTimers().getTimerCount(),
23822351
isEnvironmentTornDown: () => this.isTornDown,
23832352
isMockFunction: this._moduleMocker.isMockFunction,
@@ -2410,12 +2379,6 @@ export default class Runtime {
24102379
const fakeTimers = _getFakeTimers();
24112380

24122381
if (fakeTimers === this._environment.fakeTimersModern) {
2413-
// TODO: remove this check in Jest 30
2414-
if (typeof fakeTimers.runAllTimersAsync !== 'function') {
2415-
throw new TypeError(
2416-
'Your test environment does not support async fake timers - please ensure its Jest dependencies are updated to version 29.5 or later',
2417-
);
2418-
}
24192382
await fakeTimers.runAllTimersAsync();
24202383
} else {
24212384
throw new TypeError(
@@ -2428,12 +2391,6 @@ export default class Runtime {
24282391
const fakeTimers = _getFakeTimers();
24292392

24302393
if (fakeTimers === this._environment.fakeTimersModern) {
2431-
// TODO: remove this check in Jest 30
2432-
if (typeof fakeTimers.runOnlyPendingTimersAsync !== 'function') {
2433-
throw new TypeError(
2434-
'Your test environment does not support async fake timers - please ensure its Jest dependencies are updated to version 29.5 or later',
2435-
);
2436-
}
24372394
await fakeTimers.runOnlyPendingTimersAsync();
24382395
} else {
24392396
throw new TypeError(

0 commit comments

Comments
 (0)