diff --git a/CHANGELOG.md b/CHANGELOG.md index a2425108780d..4792df3f3195 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,14 +2,15 @@ ### Features -- `[jest-environment-jsdom]` [**BREAKING**] Add default `browser` condtion to `exportConditions` for `jsdom` environment ([#11924](https://github.com/facebook/jest/pull/11924)) +- `[jest-environment-jsdom]` [**BREAKING**] Add default `browser` condition to `exportConditions` for `jsdom` environment ([#11924](https://github.com/facebook/jest/pull/11924)) +- `[jest-environment-jsdom]` [**BREAKING**] Migrate to ESM ([#12340](https://github.com/facebook/jest/pull/12340)) - `[jest-environment-node]` [**BREAKING**] Add default `node` and `node-addon` conditions to `exportConditions` for `node` environment ([#11924](https://github.com/facebook/jest/pull/11924)) +- `[jest-environment-node]` [**BREAKING**] Migrate to ESM ([#12340](https://github.com/facebook/jest/pull/12340)) - `[@jest/expect-utils]` New module exporting utils for `expect` ([#12323](https://github.com/facebook/jest/pull/12323)) ### Fixes - `[jest-phabricator]` [**BREAKING**] Convert to ESM ([#12341](https://github.com/facebook/jest/pull/12341)) - ### Chore & Maintenance - `[*]` [**BREAKING**] Drop support for Node v10 and v15 and target first LTS `16.13.0` ([#12220](https://github.com/facebook/jest/pull/12220)) diff --git a/packages/jest-environment-jsdom/src/__tests__/jsdom_environment.test.ts b/packages/jest-environment-jsdom/src/__tests__/jsdom_environment.test.ts index f10d330a7168..5bb9238c3cd9 100644 --- a/packages/jest-environment-jsdom/src/__tests__/jsdom_environment.test.ts +++ b/packages/jest-environment-jsdom/src/__tests__/jsdom_environment.test.ts @@ -6,7 +6,7 @@ */ import {makeProjectConfig} from '@jest/test-utils'; -import JSDomEnvironment = require('../'); +import JSDomEnvironment from '../'; describe('JSDomEnvironment', () => { it('should configure setTimeout/setInterval to use the browser api', () => { diff --git a/packages/jest-environment-jsdom/src/index.ts b/packages/jest-environment-jsdom/src/index.ts index 7f89cbfe45d0..1b8bd723ec90 100644 --- a/packages/jest-environment-jsdom/src/index.ts +++ b/packages/jest-environment-jsdom/src/index.ts @@ -22,7 +22,7 @@ type Win = Window & }; }; -class JSDOMEnvironment implements JestEnvironment { +export default class JSDOMEnvironment implements JestEnvironment { private dom: JSDOM | null; fakeTimers: LegacyFakeTimers | null; fakeTimersModern: ModernFakeTimers | null; @@ -161,4 +161,4 @@ class JSDOMEnvironment implements JestEnvironment { } } -export = JSDOMEnvironment; +export const TestEnvironment = JSDOMEnvironment; diff --git a/packages/jest-environment-node/src/__tests__/node_environment.test.ts b/packages/jest-environment-node/src/__tests__/node_environment.test.ts index 8f4b8f0b935b..ffe4e7c3e5ba 100644 --- a/packages/jest-environment-node/src/__tests__/node_environment.test.ts +++ b/packages/jest-environment-node/src/__tests__/node_environment.test.ts @@ -6,9 +6,7 @@ */ import {makeProjectConfig} from '@jest/test-utils'; -import NodeEnvironment = require('../'); - -const isTextEncoderDefined = typeof TextEncoder === 'function'; +import NodeEnvironment from '../'; describe('NodeEnvironment', () => { it('uses a copy of the process object', () => { @@ -39,7 +37,6 @@ describe('NodeEnvironment', () => { const timer2 = env1.global.setInterval(() => {}, 0); [timer1, timer2].forEach(timer => { - // @ts-expect-error expect(timer.id).not.toBeUndefined(); expect(typeof timer.ref).toBe('function'); expect(typeof timer.unref).toBe('function'); @@ -52,9 +49,7 @@ describe('NodeEnvironment', () => { expect(env.fakeTimersModern).toBeDefined(); }); - if (isTextEncoderDefined) { - test('TextEncoder references the same global Uint8Array constructor', () => { - expect(new TextEncoder().encode('abc')).toBeInstanceOf(Uint8Array); - }); - } + test('TextEncoder references the same global Uint8Array constructor', () => { + expect(new TextEncoder().encode('abc')).toBeInstanceOf(Uint8Array); + }); }); diff --git a/packages/jest-environment-node/src/index.ts b/packages/jest-environment-node/src/index.ts index 218997c17e0a..7689502a1ef1 100644 --- a/packages/jest-environment-node/src/index.ts +++ b/packages/jest-environment-node/src/index.ts @@ -18,7 +18,7 @@ type Timer = { unref: () => Timer; }; -class NodeEnvironment implements JestEnvironment { +export default class NodeEnvironment implements JestEnvironment { context: Context | null; fakeTimers: LegacyFakeTimers | null; fakeTimersModern: ModernFakeTimers | null; @@ -46,22 +46,16 @@ class NodeEnvironment implements JestEnvironment { global.Uint8Array = Uint8Array; // URL and URLSearchParams are global in Node >= 10 - if (typeof URL !== 'undefined' && typeof URLSearchParams !== 'undefined') { - global.URL = URL; - global.URLSearchParams = URLSearchParams; - } + global.URL = URL; + global.URLSearchParams = URLSearchParams; + // TextDecoder and TextDecoder are global in Node >= 11 - if ( - typeof TextEncoder !== 'undefined' && - typeof TextDecoder !== 'undefined' - ) { - global.TextEncoder = TextEncoder; - global.TextDecoder = TextDecoder; - } + global.TextEncoder = TextEncoder; + global.TextDecoder = TextDecoder; + // queueMicrotask is global in Node >= 11 - if (typeof queueMicrotask !== 'undefined') { - global.queueMicrotask = queueMicrotask; - } + global.queueMicrotask = queueMicrotask; + // AbortController is global in Node >= 15 if (typeof AbortController !== 'undefined') { global.AbortController = AbortController; @@ -142,4 +136,4 @@ class NodeEnvironment implements JestEnvironment { } } -export = NodeEnvironment; +export const TestEnvironment = NodeEnvironment;