From 5d1040814fa78907ccee1f2b197aa6865194d7fc Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 9 Feb 2022 14:19:16 +0100 Subject: [PATCH 1/4] feat: export test environments as ESM --- CHANGELOG.md | 5 ++-- .../src/__tests__/jsdom_environment.test.ts | 2 +- packages/jest-environment-jsdom/src/index.ts | 4 +-- .../src/__tests__/node_environment.test.ts | 13 +++------- packages/jest-environment-node/src/index.ts | 26 +++++++------------ 5 files changed, 20 insertions(+), 30 deletions(-) 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; From c629a3f4f23edf02c71bad15b60a8f2dcce91021 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 9 Feb 2022 14:59:46 +0100 Subject: [PATCH 2/4] docs and e2e --- docs/Configuration.md | 2 +- docs/Puppeteer.md | 2 +- e2e/resolve-conditions/deno-env.js | 2 +- e2e/test-environment-async/TestEnvironment.js | 2 +- .../CircusAsyncHandleTestEventEnvironment.js | 2 +- e2e/test-environment-circus/CircusHandleTestEventEnvironment.js | 2 +- e2e/test-environment/DocblockPragmasEnvironment.js | 2 +- e2e/test-environment/EsmDefaultEnvironment.js | 2 +- examples/mongodb/mongo-environment.js | 2 +- 9 files changed, 9 insertions(+), 9 deletions(-) diff --git a/docs/Configuration.md b/docs/Configuration.md index 7d6e47370040..5080992aed65 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -1103,7 +1103,7 @@ Example: ```js // my-custom-environment -const NodeEnvironment = require('jest-environment-node'); +const NodeEnvironment = require('jest-environment-node').default; class CustomEnvironment extends NodeEnvironment { constructor(config, context) { diff --git a/docs/Puppeteer.md b/docs/Puppeteer.md index 16ab669a9ab2..52ff00431a51 100644 --- a/docs/Puppeteer.md +++ b/docs/Puppeteer.md @@ -80,7 +80,7 @@ const {readFile} = require('fs').promises; const os = require('os'); const path = require('path'); const puppeteer = require('puppeteer'); -const NodeEnvironment = require('jest-environment-node'); +const NodeEnvironment = require('jest-environment-node').default; const DIR = path.join(os.tmpdir(), 'jest_puppeteer_global_setup'); diff --git a/e2e/resolve-conditions/deno-env.js b/e2e/resolve-conditions/deno-env.js index a0e81fbbb436..8468ad463dda 100644 --- a/e2e/resolve-conditions/deno-env.js +++ b/e2e/resolve-conditions/deno-env.js @@ -7,7 +7,7 @@ 'use strict'; -const NodeEnv = require('jest-environment-node'); +const NodeEnv = require('jest-environment-node').default; module.exports = class DenoEnvWithConditions extends NodeEnv { exportConditions() { diff --git a/e2e/test-environment-async/TestEnvironment.js b/e2e/test-environment-async/TestEnvironment.js index 62097b44e614..8908625da9a8 100644 --- a/e2e/test-environment-async/TestEnvironment.js +++ b/e2e/test-environment-async/TestEnvironment.js @@ -9,7 +9,7 @@ const fs = require('fs'); const os = require('os'); -const JSDOMEnvironment = require('jest-environment-jsdom'); +const JSDOMEnvironment = require('jest-environment-jsdom').default; const {createDirectory} = require('jest-util'); const DIR = os.tmpdir() + '/jest-test-environment'; diff --git a/e2e/test-environment-circus-async/CircusAsyncHandleTestEventEnvironment.js b/e2e/test-environment-circus-async/CircusAsyncHandleTestEventEnvironment.js index 52a121cdab4c..ba3a2672ccb3 100644 --- a/e2e/test-environment-circus-async/CircusAsyncHandleTestEventEnvironment.js +++ b/e2e/test-environment-circus-async/CircusAsyncHandleTestEventEnvironment.js @@ -7,7 +7,7 @@ 'use strict'; -const JSDOMEnvironment = require('jest-environment-jsdom'); +const JSDOMEnvironment = require('jest-environment-jsdom').default; const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); diff --git a/e2e/test-environment-circus/CircusHandleTestEventEnvironment.js b/e2e/test-environment-circus/CircusHandleTestEventEnvironment.js index 6bd6f2191adf..99f5a5699e14 100644 --- a/e2e/test-environment-circus/CircusHandleTestEventEnvironment.js +++ b/e2e/test-environment-circus/CircusHandleTestEventEnvironment.js @@ -7,7 +7,7 @@ 'use strict'; -const JSDOMEnvironment = require('jest-environment-jsdom'); +const JSDOMEnvironment = require('jest-environment-jsdom').default; class TestEnvironment extends JSDOMEnvironment { handleTestEvent(event) { diff --git a/e2e/test-environment/DocblockPragmasEnvironment.js b/e2e/test-environment/DocblockPragmasEnvironment.js index d32bbacba041..2f6b1329f4d2 100644 --- a/e2e/test-environment/DocblockPragmasEnvironment.js +++ b/e2e/test-environment/DocblockPragmasEnvironment.js @@ -7,7 +7,7 @@ 'use strict'; -const JSDOMEnvironment = require('jest-environment-jsdom'); +const JSDOMEnvironment = require('jest-environment-jsdom').default; class TestEnvironment extends JSDOMEnvironment { constructor(config, context) { diff --git a/e2e/test-environment/EsmDefaultEnvironment.js b/e2e/test-environment/EsmDefaultEnvironment.js index 6c3e9ad5b769..696b9722995e 100644 --- a/e2e/test-environment/EsmDefaultEnvironment.js +++ b/e2e/test-environment/EsmDefaultEnvironment.js @@ -8,7 +8,7 @@ exports.__esModule = true; -const NodeEnvironment = require('jest-environment-node'); +const NodeEnvironment = require('jest-environment-node').default; class Env extends NodeEnvironment { constructor(config, options) { diff --git a/examples/mongodb/mongo-environment.js b/examples/mongodb/mongo-environment.js index 7f4f8618eb19..60e5e8f14b72 100644 --- a/examples/mongodb/mongo-environment.js +++ b/examples/mongodb/mongo-environment.js @@ -1,5 +1,5 @@ // mongo-environment.js -const NodeEnvironment = require('jest-environment-node'); +const NodeEnvironment = require('jest-environment-node').default; const path = require('path'); From 477b3aa434281438943eb8122d152f886dc4c24c Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 9 Feb 2022 15:05:58 +0100 Subject: [PATCH 3/4] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4792df3f3195..323162dcd3f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ ### 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)) From 95c7782fe32349e865e3a3bce3f80c226b1cc1f1 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 9 Feb 2022 15:25:45 +0100 Subject: [PATCH 4/4] test --- e2e/__tests__/testEnvironment.test.ts | 2 +- e2e/test-environment-esm/EnvESM.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/e2e/__tests__/testEnvironment.test.ts b/e2e/__tests__/testEnvironment.test.ts index 9c9a7e825d2e..32f29cc968a2 100644 --- a/e2e/__tests__/testEnvironment.test.ts +++ b/e2e/__tests__/testEnvironment.test.ts @@ -32,7 +32,7 @@ it('handles missing `mocked` property', () => { 'env.js': ` const Node = require('${slash( require.resolve('jest-environment-node'), - )}'); + )}').default; module.exports = class Thing extends Node { constructor(...args) { diff --git a/e2e/test-environment-esm/EnvESM.js b/e2e/test-environment-esm/EnvESM.js index daf46b97fd90..e34477900f3e 100644 --- a/e2e/test-environment-esm/EnvESM.js +++ b/e2e/test-environment-esm/EnvESM.js @@ -5,9 +5,9 @@ * LICENSE file in the root directory of this source tree. */ -import NodeEnvironment from 'jest-environment-node'; +import {TestEnvironment} from 'jest-environment-node'; -export default class Env extends NodeEnvironment { +export default class Env extends TestEnvironment { constructor(...args) { super(...args); this.global.someVar = 42;