Skip to content

Commit 51824da

Browse files
committed
fix: add support for ESM test environments with TypeScript (.ts, .mts)
1 parent 580d4b7 commit 51824da

File tree

16 files changed

+453
-15
lines changed

16 files changed

+453
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- `[jest-reporters]` Fix issue where console output not displayed for GHA reporter even with `silent: false` option ([#15864](https://github.com/jestjs/jest/pull/15864))
1111
- `[jest-runtime]` Fix issue where user cannot utilize dynamic import despite specifying `--experimental-vm-modules` Node option ([#15842](https://github.com/jestjs/jest/pull/15842))
1212
- `[jest-test-sequencer]` Fix issue where failed tests due to compilation errors not getting re-executed even with `--onlyFailures` CLI option ([#15851](https://github.com/jestjs/jest/pull/15851))
13+
- `[jest-runner, jest-transformer]` support custom test environment written in ESM with `TypeScript` extensions ([#15885](https://github.com/jestjs/jest/pull/15885)), fixes downstream [issue](https://github.com/kulshekhar/ts-jest/issues/4195)
1314

1415
### Chore & Maintenance
1516

e2e/__tests__/testEnvironmentEsm.ts

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,46 @@
66
*/
77

88
import {resolve} from 'path';
9-
import runJest from '../runJest';
9+
import {json as runJestJson} from '../runJest';
1010

11-
it('support test environment written in ESM', () => {
12-
const DIR = resolve(__dirname, '../test-environment-esm');
13-
const {exitCode} = runJest(DIR);
11+
const DIR = resolve(__dirname, '../test-environment-esm');
12+
13+
it('support test environment written in ESM with `.ts` extension', () => {
14+
const {exitCode, json} = runJestJson(DIR, ['testUsingTsEnv.test.js'], {
15+
nodeOptions: '--experimental-vm-modules --no-warnings',
16+
});
17+
18+
expect(exitCode).toBe(0);
19+
expect(json.numTotalTests).toBe(1);
20+
expect(json.numPassedTests).toBe(1);
21+
});
22+
23+
it('support test environment written in ESM with `.mts` extension', () => {
24+
const {exitCode, json} = runJestJson(DIR, ['testUsingMtsEnv.test.js'], {
25+
nodeOptions: '--experimental-vm-modules --no-warnings',
26+
});
27+
28+
expect(exitCode).toBe(0);
29+
expect(json.numTotalTests).toBe(1);
30+
expect(json.numPassedTests).toBe(1);
31+
});
32+
33+
it('support test environment written in ESM with `.js` extension', () => {
34+
const {exitCode, json} = runJestJson(DIR, ['testUsingJsEnv.test.js'], {
35+
nodeOptions: '--experimental-vm-modules --no-warnings',
36+
});
37+
38+
expect(exitCode).toBe(0);
39+
expect(json.numTotalTests).toBe(1);
40+
expect(json.numPassedTests).toBe(1);
41+
});
42+
43+
it('support test environment written in ESM with `.mjs` extension', () => {
44+
const {exitCode, json} = runJestJson(DIR, ['testUsingMjsEnv.test.js'], {
45+
nodeOptions: '--experimental-vm-modules --no-warnings',
46+
});
1447

1548
expect(exitCode).toBe(0);
49+
expect(json.numTotalTests).toBe(1);
50+
expect(json.numPassedTests).toBe(1);
1651
});

e2e/test-environment-esm/__tests__/testUsingESMTestEnv.test.js renamed to e2e/test-environment-esm/__tests__/testUsingJsEnv.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
*
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @jest-environment <rootDir>/env-3.js
68
*/
79
'use strict';
810

9-
test('dummy', () => {
11+
test('should pass', () => {
1012
expect(globalThis.someVar).toBe(42);
1113
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
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+
* @jest-environment <rootDir>/env-4.mjs
8+
*/
9+
'use strict';
10+
11+
test('should pass', () => {
12+
expect(globalThis.someVar).toBe(42);
13+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
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+
* @jest-environment <rootDir>/env-1.mts
8+
*/
9+
'use strict';
10+
11+
test('should pass', () => {
12+
expect(globalThis.someVar).toBe(42);
13+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
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+
* @jest-environment <rootDir>/env-2.ts
8+
*/
9+
'use strict';
10+
11+
test('should pass', () => {
12+
expect(globalThis.someVar).toBe(42);
13+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
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+
module.exports = {
9+
presets: [
10+
['@babel/preset-env', {targets: {node: 'current'}}],
11+
'@babel/preset-typescript',
12+
],
13+
};

e2e/test-environment-esm/env-1.mts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
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 type {
9+
EnvironmentContext,
10+
JestEnvironmentConfig,
11+
} from '@jest/environment';
12+
import {TestEnvironment} from 'jest-environment-node';
13+
14+
declare global {
15+
interface ImportMeta {
16+
someVar: number;
17+
}
18+
}
19+
20+
export default class Env extends TestEnvironment {
21+
constructor(config: JestEnvironmentConfig, context: EnvironmentContext) {
22+
super(config, context);
23+
import.meta.someVar = 42;
24+
this.global.someVar = import.meta.someVar;
25+
}
26+
}

e2e/test-environment-esm/env-2.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
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 type {
9+
EnvironmentContext,
10+
JestEnvironmentConfig,
11+
} from '@jest/environment';
12+
import {TestEnvironment} from 'jest-environment-node';
13+
14+
declare global {
15+
interface ImportMeta {
16+
someVar: number;
17+
}
18+
}
19+
20+
export default class Env extends TestEnvironment {
21+
constructor(config: JestEnvironmentConfig, context: EnvironmentContext) {
22+
super(config, context);
23+
import.meta.someVar = 42;
24+
this.global.someVar = import.meta.someVar;
25+
}
26+
}

e2e/test-environment-esm/EnvESM.js renamed to e2e/test-environment-esm/env-3.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
import {TestEnvironment} from 'jest-environment-node';
99

1010
export default class Env extends TestEnvironment {
11-
constructor(...args) {
12-
super(...args);
13-
this.global.someVar = 42;
11+
constructor(config, context) {
12+
super(config, context);
13+
import.meta.someVar = 42;
14+
this.global.someVar = import.meta.someVar;
1415
}
1516
}

0 commit comments

Comments
 (0)