@rspack-canary/test-tools 1.5.6-canary-95c56d3d-20250917183900 → 1.5.6-canary-54864f9e-20250919065011

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.
Files changed (42) hide show
  1. package/dist/case/hot-step.js +1 -1
  2. package/dist/case/normal.js +1 -1
  3. package/dist/case/stats-output.js +1 -0
  4. package/dist/helper/directory.js +1 -1
  5. package/dist/processor/stats.d.ts +2 -2
  6. package/dist/processor/stats.js +4 -6
  7. package/dist/runner/basic.js +4 -3
  8. package/dist/runner/hot.js +8 -1
  9. package/dist/runner/index.d.ts +1 -3
  10. package/dist/runner/index.js +1 -3
  11. package/dist/runner/runner/index.d.ts +1 -7
  12. package/dist/runner/runner/index.js +1 -7
  13. package/dist/runner/runner/node/index.d.ts +38 -0
  14. package/dist/runner/runner/node/index.js +396 -0
  15. package/dist/runner/runner/web/fake.d.ts +11 -13
  16. package/dist/runner/runner/web/fake.js +3 -21
  17. package/dist/runner/runner/{web.d.ts → web/index.d.ts} +5 -6
  18. package/dist/runner/runner/{web.js → web/index.js} +3 -3
  19. package/dist/runner/runner/web/jsdom.d.ts +8 -8
  20. package/dist/runner/runner/web/jsdom.js +14 -4
  21. package/dist/runner/type.d.ts +4 -4
  22. package/dist/runner/watch.d.ts +1 -1
  23. package/dist/runner/watch.js +17 -6
  24. package/dist/test/creator.d.ts +1 -1
  25. package/dist/test/creator.js +1 -0
  26. package/dist/test/tester.js +9 -0
  27. package/dist/type.d.ts +2 -2
  28. package/package.json +4 -4
  29. package/dist/runner/hot-step.d.ts +0 -5
  30. package/dist/runner/hot-step.js +0 -87
  31. package/dist/runner/normal.d.ts +0 -5
  32. package/dist/runner/normal.js +0 -20
  33. package/dist/runner/runner/basic.d.ts +0 -30
  34. package/dist/runner/runner/basic.js +0 -119
  35. package/dist/runner/runner/cjs.d.ts +0 -16
  36. package/dist/runner/runner/cjs.js +0 -180
  37. package/dist/runner/runner/esm.d.ts +0 -7
  38. package/dist/runner/runner/esm.js +0 -128
  39. package/dist/runner/runner/normal.d.ts +0 -9
  40. package/dist/runner/runner/normal.js +0 -26
  41. package/dist/runner/runner/watch.d.ts +0 -16
  42. package/dist/runner/runner/watch.js +0 -27
@@ -20,7 +20,7 @@ function getCreator(target) {
20
20
  configFiles: ["rspack.config.js", "webpack.config.js"]
21
21
  })
22
22
  ],
23
- runner: runner_1.HotStepRunnerFactory,
23
+ runner: runner_1.HotRunnerFactory,
24
24
  concurrent: true
25
25
  }));
26
26
  }
@@ -27,7 +27,7 @@ const createCaseOptions = (hot) => {
27
27
  compilerType: type_1.ECompilerType.Rspack
28
28
  })
29
29
  ],
30
- runner: runner_1.NormalRunnerFactory,
30
+ runner: runner_1.BasicRunnerFactory,
31
31
  concurrent: true
32
32
  };
33
33
  };
@@ -10,6 +10,7 @@ const creator = new creator_1.BasicCaseCreator({
10
10
  steps: ({ name }) => [
11
11
  new stats_1.StatsProcessor({
12
12
  name,
13
+ snapshotName: "stats.txt",
13
14
  writeStatsOuptut: false,
14
15
  compilerType: type_1.ECompilerType.Rspack,
15
16
  configFiles: ["rspack.config.js", "webpack.config.js"]
@@ -54,7 +54,7 @@ function describeByWalk(testFile, createCase, options = {}) {
54
54
  describeDirectory(caseName, currentLevel - 1);
55
55
  }
56
56
  else {
57
- const name = (0, _1.escapeSep)(node_path_1.default.join(testId, caseName).split(".").shift());
57
+ const name = (0, _1.escapeSep)(node_path_1.default.join(`${testId}Cases`, caseName).split(".").shift());
58
58
  describeFn(name, () => {
59
59
  const source = node_path_1.default.join(sourceBase, caseName);
60
60
  let dist = "";
@@ -1,12 +1,12 @@
1
1
  import type { ECompilerType, ITestContext, ITestEnv, TCompilerOptions } from "../type";
2
2
  import { type IMultiTaskProcessorOptions, MultiTaskProcessor } from "./multi";
3
3
  export interface IStatsProcessorOptions<T extends ECompilerType> extends Omit<IMultiTaskProcessorOptions<T>, "runable"> {
4
- snapshotName?: string;
4
+ snapshotName: string;
5
5
  writeStatsOuptut?: boolean;
6
6
  }
7
7
  export declare class StatsProcessor<T extends ECompilerType> extends MultiTaskProcessor<T> {
8
8
  private stderr;
9
- private snapshotName?;
9
+ private snapshotName;
10
10
  private writeStatsOuptut?;
11
11
  constructor(_statsOptions: IStatsProcessorOptions<T>);
12
12
  before(context: ITestContext): Promise<void>;
@@ -137,12 +137,10 @@ class StatsProcessor extends multi_1.MultiTaskProcessor {
137
137
  // CHANGE: Replace bundle size, since bundle sizes may differ between platforms
138
138
  .replace(/[0-9]+\.?[0-9]+ KiB/g, "xx KiB");
139
139
  }
140
- if (this.snapshotName) {
141
- env.expect(new RspackStats(actual)).toMatchSnapshot(this.snapshotName);
142
- }
143
- else {
144
- env.expect(new RspackStats(actual)).toMatchSnapshot();
145
- }
140
+ const snapshotPath = node_path_1.default.isAbsolute(this.snapshotName)
141
+ ? this.snapshotName
142
+ : node_path_1.default.resolve(context.getSource(), `./__snapshots__/${this.snapshotName}`);
143
+ env.expect(new RspackStats(actual)).toMatchFileSnapshot(snapshotPath);
146
144
  const testConfig = context.getTestConfig();
147
145
  if (typeof testConfig?.validate === "function") {
148
146
  testConfig.validate(stats, this.stderr.toString());
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BasicRunnerFactory = void 0;
4
4
  const type_1 = require("../type");
5
- const esm_1 = require("./runner/esm");
5
+ const node_1 = require("./runner/node");
6
6
  const web_1 = require("./runner/web");
7
7
  class BasicRunnerFactory {
8
8
  constructor(name, context) {
@@ -40,6 +40,8 @@ class BasicRunnerFactory {
40
40
  }
41
41
  createRunner(file, stats, compilerOptions, env) {
42
42
  const runnerOptions = {
43
+ runInNewContext: false,
44
+ cachable: true,
43
45
  env,
44
46
  stats,
45
47
  name: this.name,
@@ -53,11 +55,10 @@ class BasicRunnerFactory {
53
55
  return new web_1.WebRunner({
54
56
  ...runnerOptions,
55
57
  runInNewContext: true,
56
- cachable: true,
57
58
  dom: this.context.getValue(this.name, "documentType") || type_1.EDocumentType.Fake
58
59
  });
59
60
  }
60
- return new esm_1.EsmRunner(runnerOptions);
61
+ return new node_1.NodeRunner(runnerOptions);
61
62
  }
62
63
  }
63
64
  exports.BasicRunnerFactory = BasicRunnerFactory;
@@ -26,6 +26,12 @@ class HotRunnerFactory extends basic_1.BasicRunnerFactory {
26
26
  const jsonStats = stats.toJson({
27
27
  // errorDetails: true
28
28
  });
29
+ const checker = this.context.getValue(this.name, jsonStats.errors?.length
30
+ ? "hotUpdateStepErrorChecker"
31
+ : "hotUpdateStepChecker");
32
+ if (checker) {
33
+ checker(hotUpdateContext, stats, runner.getGlobal("__HMR_UPDATED_RUNTIME__"));
34
+ }
29
35
  await (0, checkArrayExpectation_1.default)(source, jsonStats, "error", `errors${hotUpdateContext.updateIndex}`, "Error");
30
36
  await (0, checkArrayExpectation_1.default)(source, jsonStats, "warning", `warnings${hotUpdateContext.updateIndex}`, "Warning");
31
37
  if (usePromise) {
@@ -54,7 +60,7 @@ class HotRunnerFactory extends basic_1.BasicRunnerFactory {
54
60
  }
55
61
  return jsonStats;
56
62
  };
57
- return new web_1.WebRunner({
63
+ const runner = new web_1.WebRunner({
58
64
  dom: this.context.getValue(this.name, "documentType") || type_1.EDocumentType.JSDOM,
59
65
  env,
60
66
  stats: this.createStatsGetter(),
@@ -78,6 +84,7 @@ class HotRunnerFactory extends basic_1.BasicRunnerFactory {
78
84
  dist,
79
85
  compilerOptions
80
86
  });
87
+ return runner;
81
88
  }
82
89
  }
83
90
  exports.HotRunnerFactory = HotRunnerFactory;
@@ -1,9 +1,7 @@
1
1
  export * from "./basic";
2
2
  export * from "./cache";
3
3
  export * from "./hot";
4
- export * from "./hot-step";
5
4
  export * from "./multiple";
6
- export * from "./normal";
7
- export * from "./runner";
5
+ export * from "./runner/node";
8
6
  export * from "./type";
9
7
  export * from "./watch";
@@ -17,9 +17,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./basic"), exports);
18
18
  __exportStar(require("./cache"), exports);
19
19
  __exportStar(require("./hot"), exports);
20
- __exportStar(require("./hot-step"), exports);
21
20
  __exportStar(require("./multiple"), exports);
22
- __exportStar(require("./normal"), exports);
23
- __exportStar(require("./runner"), exports);
21
+ __exportStar(require("./runner/node"), exports);
24
22
  __exportStar(require("./type"), exports);
25
23
  __exportStar(require("./watch"), exports);
@@ -1,8 +1,2 @@
1
- export * from "./basic";
2
- export * from "./cjs";
3
- export * from "./esm";
4
- export * from "./normal";
5
- export * from "./watch";
1
+ export * from "./node";
6
2
  export * from "./web";
7
- export * from "./web/fake";
8
- export * from "./web/jsdom";
@@ -14,11 +14,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./basic"), exports);
18
- __exportStar(require("./cjs"), exports);
19
- __exportStar(require("./esm"), exports);
20
- __exportStar(require("./normal"), exports);
21
- __exportStar(require("./watch"), exports);
17
+ __exportStar(require("./node"), exports);
22
18
  __exportStar(require("./web"), exports);
23
- __exportStar(require("./web/fake"), exports);
24
- __exportStar(require("./web/jsdom"), exports);
@@ -0,0 +1,38 @@
1
+ import type { ECompilerType, ITestEnv, ITestRunner, TCompilerOptions, TCompilerStatsCompilation, TTestConfig } from "../../../type";
2
+ import { type IGlobalContext, type IModuleScope, type TModuleObject, type TRunnerFile, type TRunnerRequirer } from "../../type";
3
+ declare global {
4
+ var printLogger: boolean;
5
+ }
6
+ export interface INodeRunnerOptions<T extends ECompilerType> {
7
+ env: ITestEnv;
8
+ stats?: () => TCompilerStatsCompilation<T>;
9
+ name: string;
10
+ runInNewContext?: boolean;
11
+ testConfig: TTestConfig<T>;
12
+ source: string;
13
+ dist: string;
14
+ compilerOptions: TCompilerOptions<T>;
15
+ cachable?: boolean;
16
+ }
17
+ export declare class NodeRunner<T extends ECompilerType = ECompilerType.Rspack> implements ITestRunner {
18
+ protected _options: INodeRunnerOptions<T>;
19
+ protected requireCache: any;
20
+ protected globalContext: IGlobalContext | null;
21
+ protected baseModuleScope: IModuleScope | null;
22
+ protected requirers: Map<string, TRunnerRequirer>;
23
+ constructor(_options: INodeRunnerOptions<T>);
24
+ run(file: string): Promise<unknown>;
25
+ getRequire(): TRunnerRequirer;
26
+ getGlobal(name: string): unknown;
27
+ protected createGlobalContext(): IGlobalContext;
28
+ protected createBaseModuleScope(): IModuleScope;
29
+ protected createModuleScope(requireFn: TRunnerRequirer, m: TModuleObject, file: TRunnerFile): IModuleScope;
30
+ protected getFile(modulePath: string[] | string, currentDirectory: string): TRunnerFile | null;
31
+ protected preExecute(code: string, file: TRunnerFile): void;
32
+ protected postExecute(m: Object, file: TRunnerFile): void;
33
+ protected createRunner(): void;
34
+ protected createMissRequirer(): TRunnerRequirer;
35
+ protected createJsonRequirer(): TRunnerRequirer;
36
+ protected createCjsRequirer(): TRunnerRequirer;
37
+ protected createEsmRequirer(): TRunnerRequirer;
38
+ }
@@ -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;
@@ -1,18 +1,16 @@
1
1
  import type { ECompilerType } from "../../../type";
2
- import { type TBasicRunnerFile, type TRunnerRequirer } from "../../type";
3
- import type { IBasicRunnerOptions } from "../basic";
4
- import { CommonJsRunner } from "../cjs";
5
- export declare class FakeDocumentWebRunner<T extends ECompilerType = ECompilerType.Rspack> extends CommonJsRunner<T> {
6
- protected _webOptions: IBasicRunnerOptions<T>;
2
+ import { type TRunnerFile, type TRunnerRequirer } from "../../type";
3
+ import { type INodeRunnerOptions, NodeRunner } from "../node";
4
+ export declare class FakeDocumentWebRunner<T extends ECompilerType = ECompilerType.Rspack> extends NodeRunner<T> {
5
+ protected _webOptions: INodeRunnerOptions<T>;
7
6
  private document;
8
7
  private oldCurrentScript;
9
- constructor(_webOptions: IBasicRunnerOptions<T>);
8
+ constructor(_webOptions: INodeRunnerOptions<T>);
10
9
  run(file: string): Promise<unknown>;
11
- protected createGlobalContext(): import("../../type").IBasicGlobalContext;
12
- protected createModuleScope(requireFn: TRunnerRequirer, m: any, file: TBasicRunnerFile): import("../../type").IBasicModuleScope;
13
- protected createBaseModuleScope(): import("../../type").IBasicModuleScope;
14
- protected createRunner(): void;
15
- protected preExecute(_: string, file: TBasicRunnerFile): void;
16
- protected postExecute(_: Object, file: TBasicRunnerFile): void;
17
- private createESMRequirer;
10
+ protected createGlobalContext(): import("../../type").IGlobalContext;
11
+ protected createModuleScope(requireFn: TRunnerRequirer, m: any, file: TRunnerFile): import("../../type").IModuleScope;
12
+ protected createBaseModuleScope(): import("../../type").IModuleScope;
13
+ protected preExecute(_: string, file: TRunnerFile): void;
14
+ protected postExecute(_: Object, file: TRunnerFile): void;
15
+ protected createEsmRequirer(): TRunnerRequirer;
18
16
  }