Skip to content

Commit

Permalink
feat: export test environments as ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Feb 9, 2022
1 parent 5fdbf84 commit 5d10408
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 30 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-environment-jsdom/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Win = Window &
};
};

class JSDOMEnvironment implements JestEnvironment<number> {
export default class JSDOMEnvironment implements JestEnvironment<number> {
private dom: JSDOM | null;
fakeTimers: LegacyFakeTimers<number> | null;
fakeTimersModern: ModernFakeTimers | null;
Expand Down Expand Up @@ -161,4 +161,4 @@ class JSDOMEnvironment implements JestEnvironment<number> {
}
}

export = JSDOMEnvironment;
export const TestEnvironment = JSDOMEnvironment;
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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');
Expand All @@ -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);
});
});
26 changes: 10 additions & 16 deletions packages/jest-environment-node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Timer = {
unref: () => Timer;
};

class NodeEnvironment implements JestEnvironment<Timer> {
export default class NodeEnvironment implements JestEnvironment<Timer> {
context: Context | null;
fakeTimers: LegacyFakeTimers<Timer> | null;
fakeTimersModern: ModernFakeTimers | null;
Expand Down Expand Up @@ -46,22 +46,16 @@ class NodeEnvironment implements JestEnvironment<Timer> {
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;
Expand Down Expand Up @@ -142,4 +136,4 @@ class NodeEnvironment implements JestEnvironment<Timer> {
}
}

export = NodeEnvironment;
export const TestEnvironment = NodeEnvironment;

0 comments on commit 5d10408

Please sign in to comment.