/
splitAsync.mocha.test.js
324 lines (268 loc) · 8.14 KB
/
splitAsync.mocha.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
319
320
321
322
323
324
/* --------------------
* livepack module
* Extra tests for `splitAsync` run in mocha for `import()` support
* ------------------*/
/* eslint-disable import/order */
'use strict';
// Modules
require('../lib/init/index.js'); // livepack/lib/init
const {splitAsync} = require('../index.js'), // livepack
{isModuleNamespaceObject} = require('util').types,
expect = require('expect').default;
// Imports
const {createFixtures, cleanupFixtures} = require('./support/index.js');
// Constants
const NUM_FIXTURES = 5;
// Init
global.expect = expect;
require('@overlookmotel/jest-extended/all');
require('./support/expect.js');
// Create fixtures
const fixtureFiles = {};
for (let i = 0; i < NUM_FIXTURES; i++) {
fixtureFiles[`${i}.js`] = `module.exports = {x: ${i}};`;
}
const fixturesPaths = createFixtures(fixtureFiles);
// Tests
describe('splitAsync', () => {
runTests(
(val, index) => {
if (!val) val = {x: index || 0};
const importFn = splitAsync(val);
return {val, importFn};
},
false
);
describe('behaves same as `() => import()`', () => {
runTests(
(val, index) => {
const path = fixturesPaths[index || 0];
val = require(path);// eslint-disable-line global-require, import/no-dynamic-require
const importFn = (0, () => import(path));
return {val, importFn};
},
true
);
});
});
after(() => cleanupFixtures(fixturesPaths)); // eslint-disable-line no-undef
function runTests(createImport, isNativeImport) {
describe('1 call', () => {
let val, importFn, mod;
beforeEach(async () => {
({val, importFn} = createImport());
mod = await importFn();
});
describe('returns', () => {
it('a function', () => {
expect(importFn).toBeFunction();
});
it('a function with no name', () => {
expect(importFn.name).toBe('');
});
it('a function which returns a promise', async () => {
await expect(importFn()).resolves.toBeDefined();
});
describe('returns new promise on each call', () => {
it('when called synchronously', async () => {
const promises = [];
for (let i = 0; i < NUM_FIXTURES; i++) {
promises.push(importFn());
}
for (const promise of promises) {
await expect(promise).resolves.toBeDefined();
}
expectAllToBeDifferent(promises);
});
it('when called asynchronously', async () => {
const promises = [];
for (let i = 0; i < NUM_FIXTURES; i++) {
const promise = importFn();
promises.push(promise);
await expect(promise).resolves.toBeDefined();
await delay();
}
expectAllToBeDifferent(promises);
});
});
});
describe('promise resolves to a module', () => {
it('object', () => {
expect(mod).toBeObject();
});
it('module namespace object', () => {
expect(isModuleNamespaceObject(mod)).toBeTrue();
});
it('with only default and [Symbol.toStringTag] properties', () => {
expect(mod).toHaveOwnPropertyNames(['default']);
expect(mod).toHaveOwnPropertySymbols([Symbol.toStringTag]);
});
it('default property is original value', () => {
expect(mod.default).toBe(val);
});
it("with [Symbol.toStringTag] property 'Module'", () => {
expect(mod[Symbol.toStringTag]).toBe('Module');
});
it('default property is writable and enumerable, but not configurable', () => {
expect(mod).toHaveDescriptorModifiersFor('default', true, true, false);
});
it('[Symbol.toStringTag] property is not writable, enumerable or configurable', () => {
expect(mod).toHaveDescriptorModifiersFor(Symbol.toStringTag, false, false, false);
});
it('with null prototype', () => {
expect(mod).toHavePrototype(null);
});
it('non-extensible', () => {
expect(Object.isExtensible(mod)).toBeFalse();
});
it('sealed', () => {
expect(Object.isSealed(mod)).toBeTrue();
});
it('not frozen', () => {
expect(Object.isFrozen(mod)).toBeFalse();
});
it('default property cannot be written to', () => {
expect(() => {
mod.default = 1;
}).toThrow(
new Error("Cannot assign to read only property 'default' of object '[object Module]'")
);
});
});
});
describe('multiple calls with different values', () => {
let vals, importFns;
beforeEach(() => {
vals = [];
importFns = [];
for (let index = 0; index < NUM_FIXTURES; index++) {
const {val, importFn} = createImport(null, index);
vals[index] = val;
importFns[index] = importFn;
}
});
it('each return a different function', () => { // eslint-disable-line jest/expect-expect
expectAllToBeDifferent(importFns);
});
describe('when called synchronously', () => {
let promises, modules;
beforeEach(async () => {
promises = importFns.map(importFn => importFn());
modules = await Promise.all(promises);
});
it('each return different promises', () => { // eslint-disable-line jest/expect-expect
expectAllToBeDifferent(promises);
});
it('each promise resolves to different module', () => { // eslint-disable-line jest/expect-expect
expectAllToBeDifferent(modules);
});
it('each module exports correct value', () => {
modules.forEach((mod, index) => {
expect(mod.default).toBe(vals[index]);
});
});
});
describe('when called asynchronously', () => {
let promises, modules;
beforeEach(async () => {
promises = [];
modules = [];
for (const importFn of importFns) {
const promise = importFn();
const mod = await promise;
promises.push(promise);
modules.push(mod);
await delay();
}
});
it('each return different promises', () => { // eslint-disable-line jest/expect-expect
expectAllToBeDifferent(promises);
});
it('each promise resolves to different module', () => { // eslint-disable-line jest/expect-expect
expectAllToBeDifferent(modules);
});
it('each module exports correct value', () => {
modules.forEach((mod, index) => {
expect(mod.default).toBe(vals[index]);
});
});
});
});
describe('multiple calls with same value', () => {
let val, importFns;
beforeEach(() => {
const values = [];
val = {x: 0};
importFns = [];
for (let index = 0; index < NUM_FIXTURES; index++) {
const {val: value, importFn} = createImport(val, 0);
values[index] = value;
if (isNativeImport) val = value;
importFns[index] = importFn;
}
});
it('each return a different function', () => { // eslint-disable-line jest/expect-expect
expectAllToBeDifferent(importFns);
});
describe('when called synchronously', () => {
let promises, modules;
beforeEach(async () => {
promises = importFns.map(importFn => importFn());
modules = await Promise.all(promises);
});
it('each return different promises', () => { // eslint-disable-line jest/expect-expect
expectAllToBeDifferent(promises);
});
it('each promise resolves to same module', () => { // eslint-disable-line jest/expect-expect
expectAllToBeSame(modules);
});
it('each module exports correct value', () => {
for (const mod of modules) {
expect(mod.default).toBe(val);
}
});
});
describe('when called asynchronously', () => {
let promises, modules;
beforeEach(async () => {
promises = [];
modules = [];
for (const importFn of importFns) {
const promise = importFn();
const mod = await promise;
promises.push(promise);
modules.push(mod);
await delay();
}
});
it('each return different promises', () => { // eslint-disable-line jest/expect-expect
expectAllToBeDifferent(promises);
});
it('each promise resolves to same module', () => { // eslint-disable-line jest/expect-expect
expectAllToBeSame(modules);
});
it('each module exports correct value', () => {
for (const mod of modules) {
expect(mod.default).toBe(val);
}
});
});
});
}
function expectAllToBeDifferent(values) {
for (let i = 0; i < values.length; i++) {
for (let j = 0; j < values.length; j++) {
if (i !== j) expect(values[j]).not.toBe(values[i]);
}
}
}
function expectAllToBeSame(values) {
for (let i = 1; i < values.length; i++) {
expect(values[i]).toBe(values[0]);
}
}
function delay(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms || 20);
});
}