forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 3
/
native-esm.test.js
107 lines (86 loc) 路 3.08 KB
/
native-esm.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {readFileSync} from 'fs';
import {createRequire} from 'module';
import {dirname, resolve} from 'path';
import {fileURLToPath} from 'url';
import {jest as jestObject} from '@jest/globals';
import staticImportedStateful from '../stateful.mjs';
import staticImportedStatefulFromCjs from '../fromCjs.mjs';
import {double} from '../index';
test('should have correct import.meta', () => {
expect(typeof require).toBe('undefined');
expect(typeof jest).toBe('undefined');
expect(import.meta).toEqual({
url: expect.any(String),
});
expect(
import.meta.url.endsWith('/e2e/native-esm/__tests__/native-esm.test.js'),
).toBe(true);
});
test('should double stuff', () => {
expect(double(1)).toBe(2);
});
test('should support importing node core modules', () => {
const dir = dirname(fileURLToPath(import.meta.url));
const packageJsonPath = resolve(dir, '../package.json');
expect(JSON.parse(readFileSync(packageJsonPath, 'utf8'))).toEqual({
jest: {
testEnvironment: 'node',
transform: {},
},
type: 'module',
});
});
test('dynamic import should work', async () => {
const {double: importedDouble} = await import('../index');
expect(importedDouble).toBe(double);
expect(importedDouble(1)).toBe(2);
});
test('import cjs', async () => {
const {default: half} = await import('../commonjs.cjs');
expect(half(4)).toBe(2);
});
test('require(cjs) and import(cjs) should share caches', async () => {
const require = createRequire(import.meta.url);
const {default: importedStateful} = await import('../stateful.cjs');
const requiredStateful = require('../stateful.cjs');
expect(importedStateful()).toBe(1);
expect(importedStateful()).toBe(2);
expect(requiredStateful()).toBe(3);
expect(importedStateful()).toBe(4);
expect(requiredStateful()).toBe(5);
expect(requiredStateful()).toBe(6);
});
test('import from mjs and import(mjs) should share caches', async () => {
const {default: importedStateful} = await import('../stateful.mjs');
expect(importedStateful()).toBe(1);
expect(importedStateful()).toBe(2);
expect(staticImportedStateful()).toBe(3);
expect(importedStateful()).toBe(4);
expect(staticImportedStateful()).toBe(5);
expect(staticImportedStateful()).toBe(6);
});
test('import cjs via import statement', () => {
expect(staticImportedStatefulFromCjs(4)).toBe(2);
});
test('handle unlinked dynamic imports', async () => {
const {double: deepDouble} = await import('../dynamicImport');
expect(deepDouble).toBe(double);
expect(deepDouble(4)).toBe(8);
});
test('can import `jest` object', () => {
expect(jestObject).toBeDefined();
});
test('handle dynamic imports of the same module in parallel', async () => {
const [{double: first}, {double: second}] = await Promise.all([
import('../anotherDynamicImport.js'),
import('../anotherDynamicImport.js'),
]);
expect(first).toBe(second);
expect(first(2)).toBe(4);
});