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

feat: export test environments as ESM #12340

Merged
merged 5 commits into from
Feb 9, 2022
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
### Features

- `[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))
- `[jest-snapshot]` [**BREAKING**] Migrate to ESM ([#12342](https://github.com/facebook/jest/pull/12342))
- `[jest-worker]` [**BREAKING**] Allow only absolute `workerPath` ([#12343](https://github.com/facebook/jest/pull/12343))
Expand Down
2 changes: 1 addition & 1 deletion docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion docs/Puppeteer.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/testEnvironment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion e2e/resolve-conditions/deno-env.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion e2e/test-environment-async/TestEnvironment.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions e2e/test-environment-esm/EnvESM.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion e2e/test-environment/DocblockPragmasEnvironment.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion e2e/test-environment/EsmDefaultEnvironment.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion examples/mongodb/mongo-environment.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// mongo-environment.js
const NodeEnvironment = require('jest-environment-node');
const NodeEnvironment = require('jest-environment-node').default;

const path = require('path');

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;
Copy link
Member Author

Choose a reason for hiding this comment

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

for ease of use for consumers who don't wanna use default

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;