-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
tests.js
286 lines (244 loc) · 8.5 KB
/
tests.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
import assert from "assert";
import { Meteor } from "meteor/meteor";
function assertDeepEqual(a, b) {
const aWithoutDefault = Object.assign({}, a);
delete aWithoutDefault.default;
const bWithoutDefault = Object.assign({}, b);
delete bWithoutDefault.default;
assert.deepEqual(aWithoutDefault, bWithoutDefault);
}
describe("dynamic import(...)", function () {
maybeClearDynamicImportCache();
it("ignores bad __meteor__/dynamic-import/fetch requests (#10147)", function () {
return fetch(Meteor.absoluteUrl("/__meteor__/dynamic-import/fetch"), {
// POST request with empty body.
method: "POST"
}).then(async (res) => {
assert.strictEqual(res.status, 400);
if (Meteor.isProduction) {
assert.strictEqual(await res.json(), "bad request");
} else {
assert.strictEqual(await res.json(), "Unexpected end of JSON input");
}
});
});
it("import same module both statically and dynamically", function () {
import moment from "moment";
return import("./imports/date").then(date => {
assert.strictEqual(date.moment, moment);
});
});
it("import builtin stub dynamically", function () {
const stubId = "console";
let missing = false;
try {
require(stubId);
} catch (e) {
missing = true;
}
if (Meteor.isClient) {
assert.strictEqual(missing, true);
}
return import("console").then(console => {
assertDeepEqual(console, require(stubId));
assert.strictEqual(typeof console.log, "function");
});
});
it("static package.json, static package", function () {
import { name } from "acorn/package.json";
import acorn from "acorn";
assert.strictEqual(name, "acorn");
assert.strictEqual(typeof acorn.parse, "function");
});
it("static package.json, dynamic package", function () {
import { name } from "private/package.json";
return import("private").then(priv => {
assert.strictEqual(name, "private");
assert.strictEqual(typeof priv.makeAccessor, "function");
assertDeepEqual(priv, require("pri" + "vate"));
});
});
it("dynamic package.json, static package", function () {
import arson from "arson";
return import("arson/package.json").then(({ name }) => {
assert.strictEqual(name, "arson");
assert.strictEqual(typeof arson.encode, "function");
assertDeepEqual(arson, require("ar" + "son"));
});
});
it("dynamic package.json, dynamic package", function () {
return Promise.all([
import("react/package.json"),
import("react")
]).then(([{ name }, React]) => {
assert.strictEqual(name, "react");
assert.strictEqual(typeof React.createElement, "function");
assertDeepEqual(React, require("re" + "act"));
});
});
it("mutual dynamic imports", function () {
return import("./imports/mutual-a").then(a => {
assert.strictEqual(a.name, "/imports/mutual-a.js");
return a.promise;
}).then(b => {
assert.strictEqual(b.name, "/imports/mutual-b.js");
return b.promise;
});
});
it("imports from lazy packages", function () {
let missing = false;
const dynamicId = [
"meteor", "lazy-test-package", "dynamic"
].join("/");
try {
// Synchronous dynamic requires should fail if the module has not
// been fetched dynamically yet.
require(dynamicId);
} catch (e) {
missing = true;
}
if (Meteor.isClient) {
// Dynamic modules only exist on the client. On the server, modules
// imported via dynamic import(...) are treated the same as
// statically imported modules.
assert.strictEqual(missing, true);
}
return Promise.all([
import("meteor/lazy-test-package").then(lazy => {
const requiredName = require([
"meteor", "lazy-test-package"
].join("/")).name;
assert.strictEqual(
lazy.name,
"/node_modules/meteor/lazy-test-package/main.js"
);
assert.strictEqual(lazy.name, requiredName);
}),
import("meteor/lazy-test-package/dynamic").then(dynamic => {
assert.strictEqual(
dynamic.name,
"/node_modules/meteor/lazy-test-package/dynamic.js"
);
// Now the synchronous dynamic require succeeds because the module
// has been fetched dynamically.
assert.strictEqual(
require(dynamicId).name,
dynamic.name
);
})
]);
});
it("gives dynamic modules access to package variables", async function () {
const dynamic = await import("meteor/lazy-test-package/dynamic");
dynamic.checkHelper();
const a = await import("meteor/helper-package/dynamic/a");
const b = await import("meteor/helper-package/dynamic/b.coffee");
assert.strictEqual(a.shared, b.shared);
assertDeepEqual(a.shared, {
"/node_modules/meteor/helper-package/dynamic/a.js": true,
"/node_modules/meteor/helper-package/dynamic/b.coffee": true
});
assert.strictEqual(
(await import("meteor/helper-package")).Helper,
// Since these tests are defined in an application that uses the
// global scope for imported package variables, global.Helper should
// be identical to the Helper symbol exported by helper-package.
global.Helper
);
});
it("works with module.prefetch(id)", async function () {
import { shared } from "./imports/prefetch-child";
assertDeepEqual(shared, {});
const rejection = module.prefetch("./imports/nonexistent.js");
let threw = false;
try {
await rejection;
} catch (e) {
assert.ok(e instanceof Error);
assert.ok(e.message.startsWith("Cannot find module"));
threw = true;
}
assert.strictEqual(threw, true);
assert.strictEqual(
await module.prefetch("./tests"),
"/tests.js"
);
return module.prefetch("./imports/prefetch").then(() => {
assertDeepEqual(shared, {});
}).then(() => {
import { name } from "./imports/prefetch.js";
assert.strictEqual(name, "/imports/prefetch.js");
assertDeepEqual(shared, { [name]: true });
});
});
it("can import module.exports = {...}-style modules", () => {
return import("./imports/module-exports-esModule").then(m => {
assert.strictEqual(typeof m, "object");
assert.deepEqual(Object.keys(m), []);
});
});
it("should work for package names containing colons", () => {
const expectedAbsId =
"/node_modules/meteor/user:colon-name/dynamic.js";
const version = require(
"meteor/dynamic-import/dynamic-versions.js"
).get(expectedAbsId);
if (Meteor.isClient) {
assert.strictEqual(typeof version, "string");
} else {
assert.strictEqual(version, null);
}
return import("meteor/user:colon-name/dynamic.js").then(dynamic => {
assert.strictEqual(dynamic.name, expectedAbsId);
});
});
it("should track dynamic peer imports from packages (#9187)", () => {
const absId = require.resolve("optimism");
const version = require(
"meteor/dynamic-import/dynamic-versions.js"
).get(absId);
if (Meteor.isClient) {
assert.strictEqual(typeof version, "string");
} else {
assert.strictEqual(version, null);
}
return import("meteor/helper-package/import-peer.js").then(m => {
assert.strictEqual(typeof m.wrap, "function");
});
});
it('should support object-valued package.json "browser" fields', () => {
return import("uuid").then(({ v4: uuid }) => {
const id = uuid();
assert.strictEqual(typeof id, "string");
assert.strictEqual(id.split("-").length, 5);
if (Meteor.isClient) {
assert.strictEqual(
require.resolve("uuid/dist/esm-node/index.js"),
"/node_modules/uuid/dist/esm-browser/index.js"
);
const uuidPkgJsonId = ["uuid", "package.json"].join("/");
const { browser } = require(uuidPkgJsonId);
assert.strictEqual(typeof browser, "object");
}
});
});
});
function maybeClearDynamicImportCache() {
if (global.indexedDB) {
const saveCache = process.env.METEOR_SAVE_DYNAMIC_IMPORT_CACHE;
if (! (saveCache && JSON.parse(saveCache))) {
it("cleared the IndexedDB cache", function () {
return new Promise((resolve, reject) => {
const deleteRequest =
global.indexedDB.deleteDatabase("MeteorDynamicImportCache");
deleteRequest.onerror =
deleteRequest.onblocked =
deleteRequest.onsuccess = function (arg) {
resolve(arg);
return true;
};
});
});
}
}
}