-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
Copy pathnative-esm.test.js
318 lines (258 loc) · 9.9 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/**
* 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 dns from 'dns';
// the point here is that it's the node core module
// eslint-disable-next-line no-restricted-imports
import {readFileSync} from 'fs';
import {createRequire} from 'module';
import prefixDns from 'node:dns';
import {dirname, resolve} from 'path';
import {fileURLToPath} from 'url';
import {jest as jestObject} from '@jest/globals';
import staticImportedStatefulFromCjs from '../fromCjs.mjs';
import {double} from '../index';
import defaultFromCjs, {half, namedFunction} from '../namedExport.cjs';
import {bag} from '../namespaceExport.js';
import staticImportedStateful from '../stateful.mjs';
import staticImportedStatefulWithQuery from '../stateful.mjs?query=1';
import staticImportedStatefulWithAnotherQuery from '../stateful.mjs?query=2';
test('should have correct import.meta', () => {
expect(typeof require).toBe('undefined');
expect(typeof jest).toBe('undefined');
expect(import.meta).toEqual({
jest: expect.anything(),
url: expect.any(String),
});
expect(import.meta.jest).toBe(jestObject);
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({
devDependencies: {
'discord.js': '14.3.0',
yargs: '^17.5.1',
},
jest: {
testEnvironment: 'node',
transform: {},
},
type: 'module',
});
});
test('should support importing node core modules dynamically', async () => {
// it's important that this module has _not_ been imported at the top level
const assert = await import('assert');
expect(typeof assert.strictEqual).toBe('function');
});
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('import esm from cjs', async () => {
const {default: halfPromise} = await import('../fromEsm.cjs');
expect(await halfPromise(1)).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);
});
test('varies module cache by query', () => {
expect(staticImportedStatefulWithQuery).not.toBe(
staticImportedStatefulWithAnotherQuery,
);
expect(staticImportedStatefulWithQuery()).toBe(1);
expect(staticImportedStatefulWithQuery()).toBe(2);
expect(staticImportedStatefulWithAnotherQuery()).toBe(1);
expect(staticImportedStatefulWithQuery()).toBe(3);
expect(staticImportedStatefulWithAnotherQuery()).toBe(2);
expect(staticImportedStatefulWithAnotherQuery()).toBe(3);
});
test('supports named imports from CJS', () => {
expect(half(4)).toBe(2);
expect(namedFunction()).toBe('hello from a named CJS function!');
expect(defaultFromCjs.default()).toBe('"default" export');
expect(Object.keys(defaultFromCjs)).toEqual([
'half',
'namedFunction',
'default',
]);
});
test('supports file urls as imports', async () => {
const dynamic = await import(new URL('../stateful.mjs', import.meta.url));
expect(dynamic.default).toBe(staticImportedStateful);
});
test('namespace export', () => {
expect(bag.double).toBe(double);
});
test('handle circular dependency', async () => {
const moduleA = (await import('../circularDependentA.mjs')).default;
expect(moduleA.id).toBe('circularDependentA');
expect(moduleA.moduleB.id).toBe('circularDependentB');
expect(moduleA.moduleB.moduleA).toBe(moduleA);
});
test('require of ESM should throw correct error', () => {
const require = createRequire(import.meta.url);
expect(() => require('../fromCjs.mjs')).toThrow(
expect.objectContaining({
code: 'ERR_REQUIRE_ESM',
message: expect.stringContaining('Must use import to load ES Module'),
}),
);
});
test('can mock module', async () => {
jestObject.unstable_mockModule('../mockedModule.mjs', () => ({foo: 'bar'}), {
virtual: true,
});
const importedMock = await import('../mockedModule.mjs');
expect(Object.keys(importedMock)).toEqual(['foo']);
expect(importedMock.foo).toBe('bar');
});
test('can mock transitive module', async () => {
jestObject.unstable_mockModule('../index.js', () => ({foo: 'bar'}));
const importedMock = await import('../reexport.js');
expect(Object.keys(importedMock)).toEqual(['foo']);
expect(importedMock.foo).toBe('bar');
});
test('supports imports using "node:" prefix', () => {
expect(dns).toBe(prefixDns);
});
test('supports imports from "data:text/javascript" URI with charset=utf-8 encoding', async () => {
const code = 'export const something = "some value"';
const importedEncoded = await import(
`data:text/javascript;charset=utf-8,${encodeURIComponent(code)}`
);
expect(importedEncoded.something).toBe('some value');
});
test('supports imports from "data:text/javascript" URI with base64 encoding', async () => {
const code = 'export const something = "some value"';
const importedBase64 = await import(
`data:text/javascript;base64,${Buffer.from(code).toString('base64')}`
);
expect(importedBase64.something).toBe('some value');
});
test('supports imports from "data:text/javascript" URI without explicit encoding', async () => {
const code = 'export const something = "some value"';
const importedEncoded = await import(
`data:text/javascript,${encodeURIComponent(code)}`
);
expect(importedEncoded.something).toBe('some value');
});
test('imports from "data:text/javascript" URI with invalid encoding fail', async () => {
const code = 'export const something = "some value"';
await expect(() =>
import(
`data:text/javascript;charset=badEncoding,${encodeURIComponent(code)}`
),
).rejects.toThrow('Invalid data URI');
});
test('imports from "data:" URI with invalid mime type fail', async () => {
const code = 'export const something = "some value"';
await expect(() =>
import(`data:something/else,${encodeURIComponent(code)}`),
).rejects.toThrow('Invalid data URI');
});
test('imports from "data:text/javascript" URI with invalid data fail', async () => {
await expect(() =>
import('data:text/javascript;charset=utf-8,so(me)+.-gibberish'),
).rejects.toThrow("Unexpected token '.'");
});
test('supports imports from "data:application/json" URI', async () => {
const data = await import('data:application/json,{"foo": "bar"}');
expect(data.default).toEqual({foo: 'bar'});
});
test('supports static "data:" URI import', async () => {
const module = await import('../staticDataImport.js');
expect(module.value()).toEqual({bar: {obj: 456}, foo: '123'});
});
test('imports from "data:" URI is properly cached', async () => {
const code =
'export const wrapper = {value: 123}\nexport const set = (value) => wrapper.value = value';
const data1 = await import(
`data:text/javascript;charset=utf-8,${encodeURIComponent(code)}`
);
expect(data1.wrapper.value).toBe(123);
data1.set(234);
expect(data1.wrapper.value).toBe(234);
const data2 = await import(
`data:text/javascript;base64,${Buffer.from(code).toString('base64')}`
);
expect(data2.wrapper.value).toBe(123);
const data3 = await import(
`data:text/javascript;charset=utf-8,${encodeURIComponent(code)}`
);
expect(data3.wrapper.value).toBe(234);
});
test('can mock "data:" URI module', async () => {
const code = 'export const something = "some value"';
const dataModule = `data:text/javascript;base64,${Buffer.from(code).toString(
'base64',
)}`;
jestObject.unstable_mockModule(dataModule, () => ({foo: 'bar'}), {
virtual: true,
});
const mocked = await import(dataModule);
expect(mocked.foo).toBe('bar');
});
test('can import with module reset', async () => {
const {default: yargs} = await import('yargs');
const {default: yargsAgain} = await import('yargs');
expect(yargs).toBe(yargsAgain);
let args = yargs().parse([]);
expect(args._).toEqual([]);
jestObject.resetModules();
const {default: yargsYetAgain} = await import('yargs');
expect(yargs).not.toBe(yargsYetAgain);
args = yargsYetAgain().parse([]);
expect(args._).toEqual([]);
});