@rspack/test-tools 1.5.5 → 1.5.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/case/builtin.d.ts +2 -0
- package/dist/case/builtin.js +173 -13
- package/dist/case/cache.d.ts +1 -1
- package/dist/case/cache.js +210 -11
- package/dist/case/common.d.ts +10 -0
- package/dist/case/common.js +237 -0
- package/dist/case/compiler.d.ts +7 -4
- package/dist/case/compiler.js +35 -18
- package/dist/case/config.d.ts +5 -1
- package/dist/case/config.js +102 -16
- package/dist/case/defaults.d.ts +12 -4
- package/dist/case/defaults.js +67 -12
- package/dist/case/diagnostic.d.ts +6 -0
- package/dist/case/diagnostic.js +123 -14
- package/dist/case/diff.d.ts +17 -1
- package/dist/case/diff.js +170 -22
- package/dist/case/error.d.ts +14 -4
- package/dist/case/error.js +103 -7
- package/dist/case/hash.d.ts +1 -1
- package/dist/case/hash.js +68 -7
- package/dist/case/hook.d.ts +35 -4
- package/dist/case/hook.js +196 -14
- package/dist/case/hot-step.js +307 -9
- package/dist/case/hot.d.ts +8 -1
- package/dist/case/hot.js +235 -8
- package/dist/case/incremental.d.ts +1 -1
- package/dist/case/incremental.js +39 -41
- package/dist/case/index.d.ts +20 -20
- package/dist/case/index.js +44 -34
- package/dist/case/native-watcher.js +10 -23
- package/dist/case/normal.js +174 -12
- package/dist/case/runner.d.ts +18 -0
- package/dist/case/runner.js +108 -0
- package/dist/case/serial.d.ts +1 -1
- package/dist/case/serial.js +8 -12
- package/dist/case/stats-api.d.ts +0 -5
- package/dist/case/stats-api.js +57 -7
- package/dist/case/stats-output.js +197 -10
- package/dist/case/treeshaking.js +34 -7
- package/dist/case/watch.d.ts +27 -0
- package/dist/case/watch.js +321 -21
- package/dist/compare/format-code.d.ts +1 -1
- package/dist/compare/format-code.js +6 -1
- package/dist/helper/directory.js +1 -1
- package/dist/helper/legacy/checkArrayExpectation.d.ts +1 -1
- package/dist/helper/legacy/checkArrayExpectation.js +7 -7
- package/dist/helper/plugins/hot-update.d.ts +2 -2
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/runner/index.d.ts +2 -9
- package/dist/runner/index.js +2 -9
- package/dist/runner/node/index.d.ts +37 -0
- package/dist/runner/node/index.js +396 -0
- package/dist/runner/web/fake.d.ts +15 -0
- package/dist/runner/{runner/web → web}/fake.js +10 -28
- package/dist/runner/{runner/web.d.ts → web/index.d.ts} +4 -5
- package/dist/runner/{runner/web.js → web/index.js} +2 -2
- package/dist/runner/web/jsdom.d.ts +24 -0
- package/dist/runner/{runner/web → web}/jsdom.js +18 -8
- package/dist/test/context.d.ts +3 -5
- package/dist/test/context.js +22 -12
- package/dist/test/creator.d.ts +3 -3
- package/dist/test/creator.js +3 -2
- package/dist/test/simple.js +1 -0
- package/dist/test/tester.js +10 -0
- package/dist/type.d.ts +42 -12
- package/dist/type.js +7 -1
- package/package.json +6 -6
- package/dist/processor/basic.d.ts +0 -24
- package/dist/processor/basic.js +0 -146
- package/dist/processor/builtin.d.ts +0 -9
- package/dist/processor/builtin.js +0 -171
- package/dist/processor/cache.d.ts +0 -20
- package/dist/processor/cache.js +0 -129
- package/dist/processor/config.d.ts +0 -11
- package/dist/processor/config.js +0 -86
- package/dist/processor/defaults.d.ts +0 -30
- package/dist/processor/defaults.js +0 -72
- package/dist/processor/diagnostic.d.ts +0 -15
- package/dist/processor/diagnostic.js +0 -102
- package/dist/processor/diff.d.ts +0 -30
- package/dist/processor/diff.js +0 -140
- package/dist/processor/error.d.ts +0 -23
- package/dist/processor/error.js +0 -95
- package/dist/processor/hash.d.ts +0 -10
- package/dist/processor/hash.js +0 -63
- package/dist/processor/hook.d.ts +0 -44
- package/dist/processor/hook.js +0 -204
- package/dist/processor/hot-incremental.d.ts +0 -14
- package/dist/processor/hot-incremental.js +0 -43
- package/dist/processor/hot-step.d.ts +0 -18
- package/dist/processor/hot-step.js +0 -307
- package/dist/processor/hot.d.ts +0 -17
- package/dist/processor/hot.js +0 -145
- package/dist/processor/index.d.ts +0 -20
- package/dist/processor/index.js +0 -36
- package/dist/processor/multi.d.ts +0 -17
- package/dist/processor/multi.js +0 -73
- package/dist/processor/normal.d.ts +0 -12
- package/dist/processor/normal.js +0 -168
- package/dist/processor/simple.d.ts +0 -24
- package/dist/processor/simple.js +0 -51
- package/dist/processor/snapshot.d.ts +0 -12
- package/dist/processor/snapshot.js +0 -66
- package/dist/processor/stats-api.d.ts +0 -18
- package/dist/processor/stats-api.js +0 -48
- package/dist/processor/stats.d.ts +0 -18
- package/dist/processor/stats.js +0 -206
- package/dist/processor/treeshaking.d.ts +0 -10
- package/dist/processor/treeshaking.js +0 -33
- package/dist/processor/watch.d.ts +0 -30
- package/dist/processor/watch.js +0 -251
- package/dist/runner/basic.d.ts +0 -10
- package/dist/runner/basic.js +0 -63
- package/dist/runner/cache.d.ts +0 -5
- package/dist/runner/cache.js +0 -90
- package/dist/runner/hot-step.d.ts +0 -5
- package/dist/runner/hot-step.js +0 -87
- package/dist/runner/hot.d.ts +0 -5
- package/dist/runner/hot.js +0 -83
- package/dist/runner/multiple.d.ts +0 -11
- package/dist/runner/multiple.js +0 -52
- package/dist/runner/normal.d.ts +0 -5
- package/dist/runner/normal.js +0 -20
- package/dist/runner/runner/basic.d.ts +0 -30
- package/dist/runner/runner/basic.js +0 -119
- package/dist/runner/runner/cjs.d.ts +0 -16
- package/dist/runner/runner/cjs.js +0 -180
- package/dist/runner/runner/esm.d.ts +0 -7
- package/dist/runner/runner/esm.js +0 -128
- package/dist/runner/runner/index.d.ts +0 -8
- package/dist/runner/runner/index.js +0 -24
- package/dist/runner/runner/normal.d.ts +0 -9
- package/dist/runner/runner/normal.js +0 -26
- package/dist/runner/runner/watch.d.ts +0 -16
- package/dist/runner/runner/watch.js +0 -27
- package/dist/runner/runner/web/fake.d.ts +0 -18
- package/dist/runner/runner/web/jsdom.d.ts +0 -25
- package/dist/runner/type.d.ts +0 -42
- package/dist/runner/type.js +0 -9
- package/dist/runner/watch.d.ts +0 -7
- package/dist/runner/watch.js +0 -60
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.NodeRunner = void 0;
|
|
40
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
41
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
42
|
+
const node_url_1 = require("node:url");
|
|
43
|
+
const node_vm_1 = __importStar(require("node:vm"));
|
|
44
|
+
const asModule_1 = __importDefault(require("../../helper/legacy/asModule"));
|
|
45
|
+
const urlToRelativePath_1 = __importDefault(require("../../helper/legacy/urlToRelativePath"));
|
|
46
|
+
const type_1 = require("../../type");
|
|
47
|
+
const isRelativePath = (p) => /^\.\.?\//.test(p);
|
|
48
|
+
const getSubPath = (p) => {
|
|
49
|
+
const lastSlash = p.lastIndexOf("/");
|
|
50
|
+
let firstSlash = p.indexOf("/");
|
|
51
|
+
if (lastSlash !== -1 && firstSlash !== lastSlash) {
|
|
52
|
+
if (firstSlash !== -1) {
|
|
53
|
+
let next = p.indexOf("/", firstSlash + 1);
|
|
54
|
+
let dir = p.slice(firstSlash + 1, next);
|
|
55
|
+
while (dir === ".") {
|
|
56
|
+
firstSlash = next;
|
|
57
|
+
next = p.indexOf("/", firstSlash + 1);
|
|
58
|
+
dir = p.slice(firstSlash + 1, next);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return p.slice(firstSlash + 1, lastSlash + 1);
|
|
62
|
+
}
|
|
63
|
+
return "";
|
|
64
|
+
};
|
|
65
|
+
const cached = new Map();
|
|
66
|
+
class NodeRunner {
|
|
67
|
+
constructor(_options) {
|
|
68
|
+
this._options = _options;
|
|
69
|
+
this.requireCache = Object.create(null);
|
|
70
|
+
this.globalContext = null;
|
|
71
|
+
this.baseModuleScope = null;
|
|
72
|
+
this.requirers = new Map();
|
|
73
|
+
}
|
|
74
|
+
run(file) {
|
|
75
|
+
if (!this.globalContext) {
|
|
76
|
+
this.globalContext = this.createGlobalContext();
|
|
77
|
+
}
|
|
78
|
+
this.baseModuleScope = this.createBaseModuleScope();
|
|
79
|
+
if (typeof this._options.testConfig.moduleScope === "function") {
|
|
80
|
+
this._options.testConfig.moduleScope(this.baseModuleScope, this._options.stats, this._options.compilerOptions);
|
|
81
|
+
}
|
|
82
|
+
this.createRunner();
|
|
83
|
+
const res = this.getRequire()(this._options.dist, file.startsWith("./") || file.startsWith("https://test.cases/")
|
|
84
|
+
? file
|
|
85
|
+
: `./${file}`);
|
|
86
|
+
if (typeof res === "object" && "then" in res) {
|
|
87
|
+
return res;
|
|
88
|
+
}
|
|
89
|
+
return Promise.resolve(res);
|
|
90
|
+
}
|
|
91
|
+
getRequire() {
|
|
92
|
+
const entryRequire = this.requirers.get("entry");
|
|
93
|
+
return (currentDirectory, modulePath, context = {}) => {
|
|
94
|
+
const p = Array.isArray(modulePath)
|
|
95
|
+
? modulePath
|
|
96
|
+
: modulePath.split("?")[0];
|
|
97
|
+
return entryRequire(currentDirectory, p, context);
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
getGlobal(name) {
|
|
101
|
+
return (this.globalContext || {})[name];
|
|
102
|
+
}
|
|
103
|
+
createGlobalContext() {
|
|
104
|
+
return {
|
|
105
|
+
console: {
|
|
106
|
+
log: (...args) => {
|
|
107
|
+
if (printLogger) {
|
|
108
|
+
console.log(...args);
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
warn: (...args) => {
|
|
112
|
+
if (printLogger) {
|
|
113
|
+
console.warn(...args);
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
error: (...args) => {
|
|
117
|
+
console.error(...args);
|
|
118
|
+
},
|
|
119
|
+
info: (...args) => {
|
|
120
|
+
if (printLogger) {
|
|
121
|
+
console.info(...args);
|
|
122
|
+
}
|
|
123
|
+
},
|
|
124
|
+
debug: (...args) => {
|
|
125
|
+
if (printLogger) {
|
|
126
|
+
console.info(...args);
|
|
127
|
+
}
|
|
128
|
+
},
|
|
129
|
+
trace: (...args) => {
|
|
130
|
+
if (printLogger) {
|
|
131
|
+
console.info(...args);
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
assert: (...args) => {
|
|
135
|
+
console.assert(...args);
|
|
136
|
+
},
|
|
137
|
+
clear: () => {
|
|
138
|
+
console.clear();
|
|
139
|
+
}
|
|
140
|
+
},
|
|
141
|
+
setTimeout: ((cb, ms, ...args) => {
|
|
142
|
+
const timeout = setTimeout(cb, ms, ...args);
|
|
143
|
+
timeout.unref();
|
|
144
|
+
return timeout;
|
|
145
|
+
}),
|
|
146
|
+
clearTimeout: clearTimeout
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
createBaseModuleScope() {
|
|
150
|
+
const baseModuleScope = {
|
|
151
|
+
console: this.globalContext.console,
|
|
152
|
+
setTimeout: this.globalContext.setTimeout,
|
|
153
|
+
clearTimeout: this.globalContext.clearTimeout,
|
|
154
|
+
nsObj: (m) => {
|
|
155
|
+
Object.defineProperty(m, Symbol.toStringTag, {
|
|
156
|
+
value: "Module"
|
|
157
|
+
});
|
|
158
|
+
return m;
|
|
159
|
+
},
|
|
160
|
+
process,
|
|
161
|
+
URL,
|
|
162
|
+
Blob,
|
|
163
|
+
Symbol,
|
|
164
|
+
Buffer,
|
|
165
|
+
setImmediate,
|
|
166
|
+
__MODE__: this._options.compilerOptions.mode,
|
|
167
|
+
__SNAPSHOT__: node_path_1.default.join(this._options.source, "__snapshot__"),
|
|
168
|
+
...this._options.env
|
|
169
|
+
};
|
|
170
|
+
return baseModuleScope;
|
|
171
|
+
}
|
|
172
|
+
createModuleScope(requireFn, m, file) {
|
|
173
|
+
const requirer = requireFn.bind(null, node_path_1.default.dirname(file.path));
|
|
174
|
+
requirer.webpackTestSuiteRequire = true;
|
|
175
|
+
return {
|
|
176
|
+
...this.baseModuleScope,
|
|
177
|
+
require: requirer,
|
|
178
|
+
module: m,
|
|
179
|
+
exports: m.exports,
|
|
180
|
+
__dirname: node_path_1.default.dirname(file.path),
|
|
181
|
+
__filename: file.path,
|
|
182
|
+
_globalAssign: {
|
|
183
|
+
expect: this._options.env.expect
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
getFile(modulePath, currentDirectory) {
|
|
188
|
+
const cacheKey = `${currentDirectory}|${modulePath}`;
|
|
189
|
+
if (this._options.cachable && cached.has(cacheKey)) {
|
|
190
|
+
return cached.get(cacheKey);
|
|
191
|
+
}
|
|
192
|
+
let res = null;
|
|
193
|
+
if (Array.isArray(modulePath)) {
|
|
194
|
+
res = {
|
|
195
|
+
path: node_path_1.default.join(currentDirectory, ".array-require.js"),
|
|
196
|
+
content: `module.exports = (${modulePath
|
|
197
|
+
.map(arg => {
|
|
198
|
+
return `require(${JSON.stringify(`./${arg}`)})`;
|
|
199
|
+
})
|
|
200
|
+
.join(", ")});`,
|
|
201
|
+
subPath: ""
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
else if (modulePath.startsWith("https://test.cases/")) {
|
|
205
|
+
const relativePath = (0, urlToRelativePath_1.default)(modulePath);
|
|
206
|
+
const absPath = node_path_1.default.join(currentDirectory, relativePath);
|
|
207
|
+
res = {
|
|
208
|
+
path: absPath,
|
|
209
|
+
content: node_fs_1.default.readFileSync(absPath, "utf-8"),
|
|
210
|
+
subPath: ""
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
else if (isRelativePath(modulePath)) {
|
|
214
|
+
const p = node_path_1.default.join(currentDirectory, modulePath);
|
|
215
|
+
res = {
|
|
216
|
+
path: p,
|
|
217
|
+
content: node_fs_1.default.readFileSync(p, "utf-8"),
|
|
218
|
+
subPath: getSubPath(modulePath)
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
else if (node_path_1.default.isAbsolute(modulePath)) {
|
|
222
|
+
res = {
|
|
223
|
+
path: modulePath,
|
|
224
|
+
content: node_fs_1.default.readFileSync(modulePath, "utf-8"),
|
|
225
|
+
subPath: "absolute_path"
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
if (this._options.cachable && res) {
|
|
229
|
+
cached.set(cacheKey, res);
|
|
230
|
+
}
|
|
231
|
+
return res;
|
|
232
|
+
}
|
|
233
|
+
preExecute(code, file) { }
|
|
234
|
+
postExecute(m, file) { }
|
|
235
|
+
createRunner() {
|
|
236
|
+
this.requirers.set("cjs", this.createCjsRequirer());
|
|
237
|
+
this.requirers.set("esm", this.createEsmRequirer());
|
|
238
|
+
this.requirers.set("miss", this.createMissRequirer());
|
|
239
|
+
this.requirers.set("json", this.createJsonRequirer());
|
|
240
|
+
this.requirers.set("entry", (currentDirectory, modulePath, context) => {
|
|
241
|
+
const file = this.getFile(modulePath, currentDirectory);
|
|
242
|
+
if (!file) {
|
|
243
|
+
return this.requirers.get("miss")(currentDirectory, modulePath);
|
|
244
|
+
}
|
|
245
|
+
if (file.path.endsWith(".json")) {
|
|
246
|
+
return this.requirers.get("json")(currentDirectory, modulePath, {
|
|
247
|
+
...context,
|
|
248
|
+
file
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
if (file.path.endsWith(".mjs") &&
|
|
252
|
+
this._options.compilerOptions.experiments?.outputModule) {
|
|
253
|
+
return this.requirers.get("esm")(currentDirectory, modulePath, {
|
|
254
|
+
...context,
|
|
255
|
+
file
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
return this.requirers.get("cjs")(currentDirectory, modulePath, {
|
|
259
|
+
...context,
|
|
260
|
+
file
|
|
261
|
+
});
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
createMissRequirer() {
|
|
265
|
+
return (currentDirectory, modulePath, context = {}) => {
|
|
266
|
+
const modulePathStr = modulePath;
|
|
267
|
+
const modules = this._options.testConfig.modules;
|
|
268
|
+
if (modules && modulePathStr in modules) {
|
|
269
|
+
return modules[modulePathStr];
|
|
270
|
+
}
|
|
271
|
+
return require(modulePathStr.startsWith("node:")
|
|
272
|
+
? modulePathStr.slice(5)
|
|
273
|
+
: modulePathStr);
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
createJsonRequirer() {
|
|
277
|
+
return (currentDirectory, modulePath, context = {}) => {
|
|
278
|
+
if (Array.isArray(modulePath)) {
|
|
279
|
+
throw new Error("Array module path is not supported in hot cases");
|
|
280
|
+
}
|
|
281
|
+
const file = context.file || this.getFile(modulePath, currentDirectory);
|
|
282
|
+
if (!file) {
|
|
283
|
+
return this.requirers.get("miss")(currentDirectory, modulePath);
|
|
284
|
+
}
|
|
285
|
+
return JSON.parse(node_fs_1.default.readFileSync(node_path_1.default.join(this._options.dist, modulePath), "utf-8"));
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
createCjsRequirer() {
|
|
289
|
+
return (currentDirectory, modulePath, context = {}) => {
|
|
290
|
+
if (modulePath === "@rspack/test-tools") {
|
|
291
|
+
return require("@rspack/test-tools");
|
|
292
|
+
}
|
|
293
|
+
const file = context.file || this.getFile(modulePath, currentDirectory);
|
|
294
|
+
if (!file) {
|
|
295
|
+
return this.requirers.get("miss")(currentDirectory, modulePath);
|
|
296
|
+
}
|
|
297
|
+
if (file.path in this.requireCache) {
|
|
298
|
+
return this.requireCache[file.path].exports;
|
|
299
|
+
}
|
|
300
|
+
const m = {
|
|
301
|
+
exports: {},
|
|
302
|
+
webpackTestSuiteModule: true
|
|
303
|
+
};
|
|
304
|
+
this.requireCache[file.path] = m;
|
|
305
|
+
if (!this._options.runInNewContext) {
|
|
306
|
+
file.content = `Object.assign(global, _globalAssign);\n ${file.content}`;
|
|
307
|
+
}
|
|
308
|
+
const currentModuleScope = this.createModuleScope(this.getRequire(), m, file);
|
|
309
|
+
if (this._options.testConfig.moduleScope) {
|
|
310
|
+
this._options.testConfig.moduleScope(currentModuleScope, this._options.stats, this._options.compilerOptions);
|
|
311
|
+
}
|
|
312
|
+
if (file.content.includes("__STATS__") && this._options.stats) {
|
|
313
|
+
currentModuleScope.__STATS__ = this._options.stats();
|
|
314
|
+
}
|
|
315
|
+
if (file.content.includes("__STATS_I__")) {
|
|
316
|
+
const statsIndex = this._options.stats?.()?.__index__;
|
|
317
|
+
if (typeof statsIndex === "number") {
|
|
318
|
+
currentModuleScope.__STATS_I__ = statsIndex;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
const args = Object.keys(currentModuleScope);
|
|
322
|
+
const argValues = args.map(arg => currentModuleScope[arg]);
|
|
323
|
+
const code = `(function(${args.join(", ")}) {
|
|
324
|
+
${file.content}
|
|
325
|
+
})`;
|
|
326
|
+
this.preExecute(code, file);
|
|
327
|
+
const fn = this._options.runInNewContext
|
|
328
|
+
? node_vm_1.default.runInNewContext(code, this.globalContext, file.path)
|
|
329
|
+
: node_vm_1.default.runInThisContext(code, file.path);
|
|
330
|
+
fn.call(this._options.testConfig.nonEsmThis
|
|
331
|
+
? this._options.testConfig.nonEsmThis(modulePath)
|
|
332
|
+
: m.exports, ...argValues);
|
|
333
|
+
this.postExecute(m, file);
|
|
334
|
+
return m.exports;
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
createEsmRequirer() {
|
|
338
|
+
const esmContext = node_vm_1.default.createContext(this.baseModuleScope, {
|
|
339
|
+
name: "context for esm"
|
|
340
|
+
});
|
|
341
|
+
const esmCache = new Map();
|
|
342
|
+
const esmIdentifier = this._options.name;
|
|
343
|
+
return (currentDirectory, modulePath, context = {}) => {
|
|
344
|
+
if (!node_vm_1.SourceTextModule) {
|
|
345
|
+
throw new Error("Running this test requires '--experimental-vm-modules'.\nRun with 'node --experimental-vm-modules node_modules/jest-cli/bin/jest'.");
|
|
346
|
+
}
|
|
347
|
+
const _require = this.getRequire();
|
|
348
|
+
const file = context.file || this.getFile(modulePath, currentDirectory);
|
|
349
|
+
if (!file) {
|
|
350
|
+
return this.requirers.get("miss")(currentDirectory, modulePath);
|
|
351
|
+
}
|
|
352
|
+
if (file.content.includes("__STATS__")) {
|
|
353
|
+
esmContext.__STATS__ = this._options.stats?.();
|
|
354
|
+
}
|
|
355
|
+
let esm = esmCache.get(file.path);
|
|
356
|
+
if (!esm) {
|
|
357
|
+
esm = new node_vm_1.SourceTextModule(file.content, {
|
|
358
|
+
identifier: `${esmIdentifier}-${file.path}`,
|
|
359
|
+
// no attribute
|
|
360
|
+
url: `${(0, node_url_1.pathToFileURL)(file.path).href}?${esmIdentifier}`,
|
|
361
|
+
context: esmContext,
|
|
362
|
+
initializeImportMeta: (meta, _) => {
|
|
363
|
+
meta.url = (0, node_url_1.pathToFileURL)(file.path).href;
|
|
364
|
+
},
|
|
365
|
+
importModuleDynamically: async (specifier, module) => {
|
|
366
|
+
const result = await _require(node_path_1.default.dirname(file.path), specifier, {
|
|
367
|
+
esmMode: type_1.EEsmMode.Evaluated
|
|
368
|
+
});
|
|
369
|
+
return await (0, asModule_1.default)(result, module.context);
|
|
370
|
+
}
|
|
371
|
+
});
|
|
372
|
+
esmCache.set(file.path, esm);
|
|
373
|
+
}
|
|
374
|
+
if (context.esmMode === type_1.EEsmMode.Unlinked)
|
|
375
|
+
return esm;
|
|
376
|
+
return (async () => {
|
|
377
|
+
await esm.link(async (specifier, referencingModule) => {
|
|
378
|
+
return await (0, asModule_1.default)(await _require(node_path_1.default.dirname(referencingModule.identifier
|
|
379
|
+
? referencingModule.identifier.slice(esmIdentifier.length + 1)
|
|
380
|
+
: (0, node_url_1.fileURLToPath)(referencingModule.url)), specifier, {
|
|
381
|
+
esmMode: type_1.EEsmMode.Unlinked
|
|
382
|
+
}), referencingModule.context, true);
|
|
383
|
+
});
|
|
384
|
+
if (esm.instantiate)
|
|
385
|
+
esm.instantiate();
|
|
386
|
+
await esm.evaluate();
|
|
387
|
+
if (context.esmMode === type_1.EEsmMode.Evaluated) {
|
|
388
|
+
return esm;
|
|
389
|
+
}
|
|
390
|
+
const ns = esm.namespace;
|
|
391
|
+
return ns.default && ns.default instanceof Promise ? ns.default : ns;
|
|
392
|
+
})();
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
exports.NodeRunner = NodeRunner;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type ECompilerType, type TRunnerFile, type TRunnerRequirer } from "../../type";
|
|
2
|
+
import { type INodeRunnerOptions, NodeRunner } from "../node";
|
|
3
|
+
export declare class FakeDocumentWebRunner<T extends ECompilerType = ECompilerType.Rspack> extends NodeRunner<T> {
|
|
4
|
+
protected _webOptions: INodeRunnerOptions<T>;
|
|
5
|
+
private document;
|
|
6
|
+
private oldCurrentScript;
|
|
7
|
+
constructor(_webOptions: INodeRunnerOptions<T>);
|
|
8
|
+
run(file: string): Promise<unknown>;
|
|
9
|
+
protected createGlobalContext(): import("../../type").IGlobalContext;
|
|
10
|
+
protected createModuleScope(requireFn: TRunnerRequirer, m: any, file: TRunnerFile): import("../../type").IModuleScope;
|
|
11
|
+
protected createBaseModuleScope(): import("../../type").IModuleScope;
|
|
12
|
+
protected preExecute(_: string, file: TRunnerFile): void;
|
|
13
|
+
protected postExecute(_: Object, file: TRunnerFile): void;
|
|
14
|
+
protected createEsmRequirer(): TRunnerRequirer;
|
|
15
|
+
}
|
|
@@ -41,16 +41,16 @@ const node_fs_1 = __importDefault(require("node:fs"));
|
|
|
41
41
|
const node_path_1 = __importDefault(require("node:path"));
|
|
42
42
|
const node_url_1 = require("node:url");
|
|
43
43
|
const node_vm_1 = __importStar(require("node:vm"));
|
|
44
|
-
const helper_1 = require("
|
|
45
|
-
const asModule_1 = __importDefault(require("
|
|
46
|
-
const createFakeWorker_1 = __importDefault(require("
|
|
47
|
-
const currentScript_1 = __importDefault(require("
|
|
48
|
-
const EventSourceForNode_1 = __importDefault(require("
|
|
49
|
-
const FakeDocument_1 = __importDefault(require("
|
|
50
|
-
const urlToRelativePath_1 = __importDefault(require("
|
|
44
|
+
const helper_1 = require("../../helper");
|
|
45
|
+
const asModule_1 = __importDefault(require("../../helper/legacy/asModule"));
|
|
46
|
+
const createFakeWorker_1 = __importDefault(require("../../helper/legacy/createFakeWorker"));
|
|
47
|
+
const currentScript_1 = __importDefault(require("../../helper/legacy/currentScript"));
|
|
48
|
+
const EventSourceForNode_1 = __importDefault(require("../../helper/legacy/EventSourceForNode"));
|
|
49
|
+
const FakeDocument_1 = __importDefault(require("../../helper/legacy/FakeDocument"));
|
|
50
|
+
const urlToRelativePath_1 = __importDefault(require("../../helper/legacy/urlToRelativePath"));
|
|
51
51
|
const type_1 = require("../../type");
|
|
52
|
-
const
|
|
53
|
-
class FakeDocumentWebRunner extends
|
|
52
|
+
const node_1 = require("../node");
|
|
53
|
+
class FakeDocumentWebRunner extends node_1.NodeRunner {
|
|
54
54
|
constructor(_webOptions) {
|
|
55
55
|
super(_webOptions);
|
|
56
56
|
this._webOptions = _webOptions;
|
|
@@ -141,24 +141,6 @@ class FakeDocumentWebRunner extends cjs_1.CommonJsRunner {
|
|
|
141
141
|
moduleScope.__dirname = this._options.dist;
|
|
142
142
|
return moduleScope;
|
|
143
143
|
}
|
|
144
|
-
createRunner() {
|
|
145
|
-
super.createRunner();
|
|
146
|
-
this.requirers.set("cjs", this.getRequire());
|
|
147
|
-
this.requirers.set("mjs", this.createESMRequirer());
|
|
148
|
-
this.requirers.set("json", this.createJsonRequirer());
|
|
149
|
-
this.requirers.set("entry", (_, modulePath, context) => {
|
|
150
|
-
if (Array.isArray(modulePath)) {
|
|
151
|
-
throw new Error("Array module path is not supported in web runner");
|
|
152
|
-
}
|
|
153
|
-
if (modulePath.endsWith(".json")) {
|
|
154
|
-
return this.requirers.get("json")(this._options.dist, modulePath, context);
|
|
155
|
-
}
|
|
156
|
-
else if (modulePath.endsWith(".mjs")) {
|
|
157
|
-
return this.requirers.get("mjs")(this._options.dist, modulePath, context);
|
|
158
|
-
}
|
|
159
|
-
return this.requirers.get("cjs")(this._options.dist, modulePath, context);
|
|
160
|
-
});
|
|
161
|
-
}
|
|
162
144
|
preExecute(_, file) {
|
|
163
145
|
this.oldCurrentScript = this.document.currentScript;
|
|
164
146
|
this.document.currentScript = new currentScript_1.default(file.subPath);
|
|
@@ -169,7 +151,7 @@ class FakeDocumentWebRunner extends cjs_1.CommonJsRunner {
|
|
|
169
151
|
this.document.currentScript = this.oldCurrentScript;
|
|
170
152
|
this.oldCurrentScript = null;
|
|
171
153
|
}
|
|
172
|
-
|
|
154
|
+
createEsmRequirer() {
|
|
173
155
|
const esmContext = node_vm_1.default.createContext({
|
|
174
156
|
...this.baseModuleScope,
|
|
175
157
|
...this.globalContext
|
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
import { type ECompilerType, EDocumentType, type ITestRunner } from "../../type";
|
|
2
|
-
import type {
|
|
3
|
-
|
|
4
|
-
export interface IWebRunnerOptions<T extends ECompilerType = ECompilerType.Rspack> extends IBasicRunnerOptions<T> {
|
|
2
|
+
import type { INodeRunnerOptions, NodeRunner } from "../node";
|
|
3
|
+
export interface IWebRunnerOptions<T extends ECompilerType = ECompilerType.Rspack> extends INodeRunnerOptions<T> {
|
|
5
4
|
dom: EDocumentType;
|
|
6
5
|
}
|
|
7
6
|
export declare class WebRunner<T extends ECompilerType = ECompilerType.Rspack> implements ITestRunner {
|
|
8
7
|
protected _webOptions: IWebRunnerOptions<T>;
|
|
9
|
-
protected originMethods: Partial<
|
|
8
|
+
protected originMethods: Partial<NodeRunner<T>>;
|
|
10
9
|
private implement;
|
|
11
10
|
constructor(_webOptions: IWebRunnerOptions<T>);
|
|
12
11
|
run(file: string): Promise<unknown>;
|
|
13
|
-
getRequire(): import("
|
|
12
|
+
getRequire(): import("../../type").TRunnerRequirer;
|
|
14
13
|
getGlobal(name: string): unknown;
|
|
15
14
|
}
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.WebRunner = void 0;
|
|
4
4
|
const type_1 = require("../../type");
|
|
5
|
-
const fake_1 = require("./
|
|
6
|
-
const jsdom_1 = require("./
|
|
5
|
+
const fake_1 = require("./fake");
|
|
6
|
+
const jsdom_1 = require("./jsdom");
|
|
7
7
|
class WebRunner {
|
|
8
8
|
constructor(_webOptions) {
|
|
9
9
|
this._webOptions = _webOptions;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { ECompilerType, TRunnerFile, TRunnerRequirer } from "../../type";
|
|
2
|
+
import { type INodeRunnerOptions, NodeRunner } from "../node";
|
|
3
|
+
export declare class JSDOMWebRunner<T extends ECompilerType = ECompilerType.Rspack> extends NodeRunner<T> {
|
|
4
|
+
protected _webOptions: INodeRunnerOptions<T>;
|
|
5
|
+
private dom;
|
|
6
|
+
constructor(_webOptions: INodeRunnerOptions<T>);
|
|
7
|
+
run(file: string): Promise<unknown>;
|
|
8
|
+
getGlobal(name: string): unknown;
|
|
9
|
+
protected createResourceLoader(): {
|
|
10
|
+
fetch(url: string, _: {
|
|
11
|
+
element: HTMLScriptElement;
|
|
12
|
+
}): any;
|
|
13
|
+
};
|
|
14
|
+
protected createBaseModuleScope(): import("../../type").IModuleScope;
|
|
15
|
+
protected getModuleContent(file: TRunnerFile): [
|
|
16
|
+
Record<string, unknown>,
|
|
17
|
+
{
|
|
18
|
+
exports: Record<string, unknown>;
|
|
19
|
+
},
|
|
20
|
+
string
|
|
21
|
+
];
|
|
22
|
+
protected createJSDOMRequirer(): TRunnerRequirer;
|
|
23
|
+
protected createRunner(): void;
|
|
24
|
+
}
|
|
@@ -7,14 +7,14 @@ exports.JSDOMWebRunner = void 0;
|
|
|
7
7
|
const node_fs_1 = __importDefault(require("node:fs"));
|
|
8
8
|
const node_path_1 = __importDefault(require("node:path"));
|
|
9
9
|
const jsdom_1 = require("jsdom");
|
|
10
|
-
const helper_1 = require("
|
|
11
|
-
const createFakeWorker_1 = __importDefault(require("
|
|
12
|
-
const EventSourceForNode_1 = __importDefault(require("
|
|
13
|
-
const urlToRelativePath_1 = __importDefault(require("
|
|
14
|
-
const
|
|
10
|
+
const helper_1 = require("../../helper");
|
|
11
|
+
const createFakeWorker_1 = __importDefault(require("../../helper/legacy/createFakeWorker"));
|
|
12
|
+
const EventSourceForNode_1 = __importDefault(require("../../helper/legacy/EventSourceForNode"));
|
|
13
|
+
const urlToRelativePath_1 = __importDefault(require("../../helper/legacy/urlToRelativePath"));
|
|
14
|
+
const node_1 = require("../node");
|
|
15
15
|
// Compatibility code to suppress iconv-lite warnings
|
|
16
16
|
require("iconv-lite").skipDecodeWarning = true;
|
|
17
|
-
class JSDOMWebRunner extends
|
|
17
|
+
class JSDOMWebRunner extends node_1.NodeRunner {
|
|
18
18
|
constructor(_webOptions) {
|
|
19
19
|
super(_webOptions);
|
|
20
20
|
this._webOptions = _webOptions;
|
|
@@ -87,7 +87,7 @@ class JSDOMWebRunner extends cjs_1.CommonJsRunner {
|
|
|
87
87
|
if (!file) {
|
|
88
88
|
throw new Error(`File not found: ${filePath}`);
|
|
89
89
|
}
|
|
90
|
-
const [_m, code] = that.getModuleContent(file);
|
|
90
|
+
const [_scope, _m, code] = that.getModuleContent(file);
|
|
91
91
|
finalCode = code;
|
|
92
92
|
}
|
|
93
93
|
else {
|
|
@@ -167,6 +167,7 @@ class JSDOMWebRunner extends cjs_1.CommonJsRunner {
|
|
|
167
167
|
.join(", ");
|
|
168
168
|
this.dom.window[scopeKey] = currentModuleScope;
|
|
169
169
|
return [
|
|
170
|
+
this.dom.window[scopeKey],
|
|
170
171
|
m,
|
|
171
172
|
`
|
|
172
173
|
// hijack document.currentScript for auto public path
|
|
@@ -202,7 +203,16 @@ class JSDOMWebRunner extends cjs_1.CommonJsRunner {
|
|
|
202
203
|
if (file.path in this.requireCache) {
|
|
203
204
|
return this.requireCache[file.path].exports;
|
|
204
205
|
}
|
|
205
|
-
const [m, code] = this.getModuleContent(file);
|
|
206
|
+
const [scope, m, code] = this.getModuleContent(file);
|
|
207
|
+
if (code.includes("__STATS__")) {
|
|
208
|
+
scope.__STATS__ = this._options.stats?.();
|
|
209
|
+
}
|
|
210
|
+
if (code.includes("__STATS_I__")) {
|
|
211
|
+
const statsIndex = this._options.stats?.()?.__index__;
|
|
212
|
+
if (typeof statsIndex === "number") {
|
|
213
|
+
scope.__STATS_I__ = statsIndex;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
206
216
|
this.preExecute(code, file);
|
|
207
217
|
this.dom.window.eval(code);
|
|
208
218
|
this.postExecute(m, file);
|
package/dist/test/context.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { ECompilerType, type ITestCompilerManager, type ITestContext, type ITestEnv, type ITesterConfig, type ITestRunner, type TTestConfig } from "../type";
|
|
2
2
|
export type TTestContextOptions = Omit<ITesterConfig, "name" | "steps">;
|
|
3
3
|
export declare class TestContext implements ITestContext {
|
|
4
4
|
private config;
|
|
@@ -6,15 +6,12 @@ export declare class TestContext implements ITestContext {
|
|
|
6
6
|
protected compilers: Map<string, ITestCompilerManager<ECompilerType>>;
|
|
7
7
|
protected store: Map<string, Record<string, unknown>>;
|
|
8
8
|
protected runners: Map<string, ITestRunner>;
|
|
9
|
-
protected runnerFactory: TRunnerFactory<ECompilerType> | null;
|
|
10
9
|
constructor(config: TTestContextOptions);
|
|
11
10
|
getSource(sub?: string): string;
|
|
12
11
|
getDist(sub?: string): string;
|
|
13
12
|
getTemp(sub?: string): string | null;
|
|
14
13
|
getCompiler<T extends ECompilerType>(name: string, type: T | void): ITestCompilerManager<T>;
|
|
15
|
-
|
|
16
|
-
getRunner(key: string): ITestRunner | null;
|
|
17
|
-
setRunner(key: string, runner: ITestRunner): void;
|
|
14
|
+
getRunner(name: string, file: string, env: ITestEnv): ITestRunner;
|
|
18
15
|
getTestConfig<T extends ECompilerType>(): TTestConfig<T>;
|
|
19
16
|
setValue<T>(name: string, key: string, value: T): void;
|
|
20
17
|
getValue<T>(name: string, key: string): T | void;
|
|
@@ -23,4 +20,5 @@ export declare class TestContext implements ITestContext {
|
|
|
23
20
|
getNames(): string[];
|
|
24
21
|
getError(name?: string): Error[];
|
|
25
22
|
clearError(name?: string): void;
|
|
23
|
+
closeCompiler(name: string): Promise<void>;
|
|
26
24
|
}
|
package/dist/test/context.js
CHANGED
|
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.TestContext = void 0;
|
|
7
7
|
const node_path_1 = __importDefault(require("node:path"));
|
|
8
8
|
const compiler_1 = require("../compiler");
|
|
9
|
+
const type_1 = require("../type");
|
|
9
10
|
class TestContext {
|
|
10
11
|
constructor(config) {
|
|
11
12
|
this.config = config;
|
|
@@ -13,7 +14,6 @@ class TestContext {
|
|
|
13
14
|
this.compilers = new Map();
|
|
14
15
|
this.store = new Map();
|
|
15
16
|
this.runners = new Map();
|
|
16
|
-
this.runnerFactory = null;
|
|
17
17
|
}
|
|
18
18
|
getSource(sub) {
|
|
19
19
|
if (sub) {
|
|
@@ -46,18 +46,18 @@ class TestContext {
|
|
|
46
46
|
}
|
|
47
47
|
return compiler;
|
|
48
48
|
}
|
|
49
|
-
|
|
50
|
-
if (!this.
|
|
51
|
-
|
|
52
|
-
this.runnerFactory = new this.config.runnerFactory(name, this);
|
|
49
|
+
getRunner(name, file, env) {
|
|
50
|
+
if (!this.config.runnerCreator) {
|
|
51
|
+
throw new Error("TestContext: Runner creator not found");
|
|
53
52
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
this.runners.set(
|
|
53
|
+
const runnerKey = this.config.runnerCreator.key(this, name, file);
|
|
54
|
+
let runner = this.runners.get(runnerKey);
|
|
55
|
+
if (runner) {
|
|
56
|
+
return runner;
|
|
57
|
+
}
|
|
58
|
+
runner = this.config.runnerCreator.runner(this, name, file, env);
|
|
59
|
+
this.runners.set(runnerKey, runner);
|
|
60
|
+
return runner;
|
|
61
61
|
}
|
|
62
62
|
getTestConfig() {
|
|
63
63
|
return this.config.testConfig || {};
|
|
@@ -104,5 +104,15 @@ class TestContext {
|
|
|
104
104
|
this.errors.clear();
|
|
105
105
|
}
|
|
106
106
|
}
|
|
107
|
+
async closeCompiler(name) {
|
|
108
|
+
const rspackCompiler = this.getCompiler(name, type_1.ECompilerType.Rspack);
|
|
109
|
+
if (rspackCompiler) {
|
|
110
|
+
await rspackCompiler.close();
|
|
111
|
+
}
|
|
112
|
+
const webpackCompiler = this.getCompiler(name, type_1.ECompilerType.Webpack);
|
|
113
|
+
if (webpackCompiler) {
|
|
114
|
+
await webpackCompiler.close();
|
|
115
|
+
}
|
|
116
|
+
}
|
|
107
117
|
}
|
|
108
118
|
exports.TestContext = TestContext;
|