diff --git a/CHANGELOG.md b/CHANGELOG.md index 070258d7a0b3..ae495aaca133 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/e2e/__tests__/testEnvironment.test.ts b/e2e/__tests__/testEnvironment.test.ts index 29b8e2e875c7..3f4a31a91c11 100644 --- a/e2e/__tests__/testEnvironment.test.ts +++ b/e2e/__tests__/testEnvironment.test.ts @@ -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'); @@ -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.', - ); -}); diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index b8f0a29e1fbf..346104f24b82 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -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.', - ); - }); - 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; @@ -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( @@ -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( @@ -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, @@ -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( @@ -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(