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

refactor(jest-environment)!: remove unnecessary defensive code #15045

Merged
merged 2 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- `[@jest/core]` Support `--outputFile` option for [`--listTests`](https://jestjs.io/docs/cli#--listtests) ([#14980](https://github.com/jestjs/jest/pull/14980))
- `[@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))
- `[@jest/environment]` [**BREAKING**] Remove deprecated `jest.genMockFromModule()` ([#15042](https://github.com/jestjs/jest/pull/15042))
- `[@jest/environment]` [**BREAKING**] Remove unnecessary defensive code ([#15045](https://github.com/jestjs/jest/pull/15045))
- `[jest-environment-jsdom]` [**BREAKING**] Upgrade JSDOM to v22 ([#13825](https://github.com/jestjs/jest/pull/13825))
- `[@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))
- `[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))
Expand Down
48 changes: 1 addition & 47 deletions e2e/__tests__/testEnvironment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,9 @@
* LICENSE file in the root directory of this source tree.
*/

import {tmpdir} from 'os';
import * as path from 'path';
import slash = require('slash');
import {cleanup, createEmptyPackage, writeFiles} from '../Utils';
import runJest, {json as runWithJson} from '../runJest';
import {json as runWithJson} from '../runJest';
import * as testFixturePackage from '../test-environment/package.json';

const DIR = path.resolve(tmpdir(), 'test-env-no-mocked');

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

it('respects testEnvironment docblock', () => {
expect(testFixturePackage.jest.testEnvironment).toBe('node');

Expand All @@ -25,40 +16,3 @@ it('respects testEnvironment docblock', () => {
expect(result.success).toBe(true);
expect(result.numTotalTests).toBe(4);
});

it('handles missing `mocked` property', () => {
createEmptyPackage(DIR);
writeFiles(DIR, {
'env.js': `
const Node = require('${slash(
require.resolve('jest-environment-node'),
)}').default;

module.exports = class Thing extends Node {
constructor(...args) {
super(...args);

this.moduleMocker.mocked = undefined;
}
};
`,
'test.js': `
/**
* @jest-environment ./env.js
*/

jest.mocked();

test('halla', () => {
expect(globalThis.thing).toBe('nope');
});
`,
});

const {exitCode, stderr} = runJest(DIR);

expect(exitCode).toBe(1);
expect(stderr).toContain(
'Your test environment does not support `mocked`, please update it.',
);
});
53 changes: 5 additions & 48 deletions packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2265,21 +2265,10 @@ export default class Runtime {
const isolateModulesAsync = this.isolateModulesAsync.bind(this);
const fn = this._moduleMocker.fn.bind(this._moduleMocker);
const spyOn = this._moduleMocker.spyOn.bind(this._moduleMocker);
const mocked =
this._moduleMocker.mocked?.bind(this._moduleMocker) ??
(() => {
throw new Error(
'Your test environment does not support `mocked`, please update it.',
Comment on lines -2270 to -2272
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and following cases are not marked as to be removed in Jest 30, but seem to be unnecessary as well.

);
});
const replaceProperty =
typeof this._moduleMocker.replaceProperty === 'function'
? this._moduleMocker.replaceProperty.bind(this._moduleMocker)
: () => {
throw new Error(
'Your test environment does not support `jest.replaceProperty` - please ensure its Jest dependencies are updated to version 29.4 or later',
);
};
const mocked = this._moduleMocker.mocked.bind(this._moduleMocker);
const replaceProperty = this._moduleMocker.replaceProperty.bind(
this._moduleMocker,
);

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

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

if (fakeTimers === this._environment.fakeTimersModern) {
// TODO: remove this check in Jest 30
if (typeof fakeTimers.advanceTimersToNextTimerAsync !== 'function') {
throw new TypeError(
'Your test environment does not support async fake timers - please ensure its Jest dependencies are updated to version 29.5 or later',
);
}
await fakeTimers.advanceTimersToNextTimerAsync(steps);
} else {
throw new TypeError(
Expand Down Expand Up @@ -2369,15 +2346,7 @@ export default class Runtime {
);
}
},
getSeed: () => {
// TODO: remove this check in Jest 30
if (this._globalConfig.seed === undefined) {
throw new Error(
'The seed value is not available. Likely you are using older versions of the jest dependencies.',
);
}
return this._globalConfig.seed;
},
getSeed: () => this._globalConfig.seed,
getTimerCount: () => _getFakeTimers().getTimerCount(),
isEnvironmentTornDown: () => this.isTornDown,
isMockFunction: this._moduleMocker.isMockFunction,
Expand Down Expand Up @@ -2410,12 +2379,6 @@ export default class Runtime {
const fakeTimers = _getFakeTimers();

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

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