Skip to content

Commit 1be8d73

Browse files
authored
feat(jest-core): add support for globalSetup and globalTeardown written in ESM (#11267)
1 parent 7d7f937 commit 1be8d73

File tree

12 files changed

+174
-10
lines changed

12 files changed

+174
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- `[jest-core]` Run failed tests interactively the same way we do with snapshots ([#10858](https://github.com/facebook/jest/pull/10858))
1717
- `[jest-core]` more `TestSequencer` methods can be async ([#10980](https://github.com/facebook/jest/pull/10980))
1818
- `[jest-core]` Add support for `testSequencer` written in ESM ([#11207](https://github.com/facebook/jest/pull/11207))
19+
- `[jest-core]` Add support for `globalSetup` and `globalTeardown` written in ESM ([#11267](https://github.com/facebook/jest/pull/11267))
1920
- `[jest-environment-node]` Add AbortController to globals ([#11182](https://github.com/facebook/jest/pull/11182))
2021
- `[@jest/fake-timers]` Update to `@sinonjs/fake-timers` to v7 ([#11198](https://github.com/facebook/jest/pull/11198))
2122
- `[jest-haste-map]` Handle injected scm clocks ([#10966](https://github.com/facebook/jest/pull/10966))

e2e/__tests__/globalSetup.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import {tmpdir} from 'os';
99
import * as path from 'path';
1010
import * as fs from 'graceful-fs';
11+
import {onNodeVersions} from '@jest/test-utils';
1112
import {
1213
cleanup,
1314
createEmptyPackage,
@@ -192,3 +193,13 @@ test('properly handle rejections', () => {
192193
expect(stderr).toContain('Error: Jest: Got error running globalSetup');
193194
expect(stderr).toContain('reason: undefined');
194195
});
196+
197+
onNodeVersions('^12.17.0 || >=13.2.0', () => {
198+
test('globalSetup works with ESM modules', () => {
199+
const {exitCode} = runJest('global-setup-esm', [`--no-cache`], {
200+
nodeOptions: '--experimental-vm-modules --no-warnings',
201+
});
202+
203+
expect(exitCode).toBe(0);
204+
});
205+
});

e2e/__tests__/globalTeardown.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import {tmpdir} from 'os';
99
import * as path from 'path';
1010
import * as fs from 'graceful-fs';
11+
import {onNodeVersions} from '@jest/test-utils';
1112
import {createDirectory} from 'jest-util';
1213
import {cleanup, runYarnInstall} from '../Utils';
1314
import runJest, {json as runWithJson} from '../runJest';
@@ -131,3 +132,13 @@ test('globalTeardown throws with named export', () => {
131132
`globalTeardown file must export a function at ${teardownPath}`,
132133
);
133134
});
135+
136+
onNodeVersions('^12.17.0 || >=13.2.0', () => {
137+
test('globalTeardown works with ESM modules', () => {
138+
const {exitCode} = runJest('global-teardown-esm', [`--no-cache`], {
139+
nodeOptions: '--experimental-vm-modules --no-warnings',
140+
});
141+
142+
expect(exitCode).toBe(0);
143+
});
144+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import os from 'os';
9+
import path from 'path';
10+
import fs from 'graceful-fs';
11+
import greeting from '../';
12+
13+
const DIR = path.join(os.tmpdir(), 'jest-global-setup-esm');
14+
15+
test('should exist setup file', () => {
16+
const files = fs.readdirSync(DIR);
17+
expect(files).toHaveLength(1);
18+
const setup = fs.readFileSync(path.join(DIR, files[0]), 'utf8');
19+
expect(setup).toBe('setup');
20+
});

e2e/global-setup-esm/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
export default 'hello!';

e2e/global-setup-esm/package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"type": "module",
3+
"jest": {
4+
"testEnvironment": "node",
5+
"globalSetup": "<rootDir>/setup.js",
6+
"transform": {}
7+
}
8+
}

e2e/global-setup-esm/setup.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
import crypto from 'crypto';
8+
import os from 'os';
9+
import path from 'path';
10+
import fs from 'graceful-fs';
11+
import jestUtil from 'jest-util';
12+
13+
const {createDirectory} = jestUtil;
14+
15+
const DIR = path.join(os.tmpdir(), 'jest-global-setup-esm');
16+
17+
export default function () {
18+
return new Promise(resolve => {
19+
createDirectory(DIR);
20+
const fileId = crypto.randomBytes(20).toString('hex');
21+
fs.writeFileSync(path.join(DIR, fileId), 'setup');
22+
resolve();
23+
});
24+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import os from 'os';
9+
import path from 'path';
10+
import fs from 'graceful-fs';
11+
import greeting from '../';
12+
13+
const DIR = path.join(os.tmpdir(), 'jest-global-teardown-esm');
14+
15+
test('should not exist teardown file', () => {
16+
expect(fs.existsSync(DIR)).toBe(false);
17+
});

e2e/global-teardown-esm/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
export default 'hello!';

e2e/global-teardown-esm/package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"type": "module",
3+
"jest": {
4+
"testEnvironment": "node",
5+
"globalTeardown": "<rootDir>/teardown.js",
6+
"transform": {}
7+
}
8+
}

0 commit comments

Comments
 (0)