@rspack-canary/test-tools 1.5.6-canary-95c56d3d-20250917183900 → 1.5.6-canary-9360f4d0-20250918174212

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 +5 -5
  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
@@ -49,8 +49,8 @@ const EventSourceForNode_1 = __importDefault(require("../../../helper/legacy/Eve
49
49
  const FakeDocument_1 = __importDefault(require("../../../helper/legacy/FakeDocument"));
50
50
  const urlToRelativePath_1 = __importDefault(require("../../../helper/legacy/urlToRelativePath"));
51
51
  const type_1 = require("../../type");
52
- const cjs_1 = require("../cjs");
53
- class FakeDocumentWebRunner extends cjs_1.CommonJsRunner {
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
- createESMRequirer() {
154
+ createEsmRequirer() {
173
155
  const esmContext = node_vm_1.default.createContext({
174
156
  ...this.baseModuleScope,
175
157
  ...this.globalContext
@@ -1,15 +1,14 @@
1
- import { type ECompilerType, EDocumentType, type ITestRunner } from "../../type";
2
- import type { IBasicRunnerOptions } from "./basic";
3
- import type { CommonJsRunner } from "./cjs";
4
- export interface IWebRunnerOptions<T extends ECompilerType = ECompilerType.Rspack> extends IBasicRunnerOptions<T> {
1
+ import { type ECompilerType, EDocumentType, type ITestRunner } from "../../../type";
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<CommonJsRunner>;
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("..").TRunnerRequirer;
12
+ getRequire(): import("../..").TRunnerRequirer;
14
13
  getGlobal(name: string): unknown;
15
14
  }
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.WebRunner = void 0;
4
- const type_1 = require("../../type");
5
- const fake_1 = require("./web/fake");
6
- const jsdom_1 = require("./web/jsdom");
4
+ const type_1 = require("../../../type");
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;
@@ -1,11 +1,10 @@
1
1
  import type { ECompilerType } from "../../../type";
2
- import type { TBasicRunnerFile, TRunnerRequirer } from "../../type";
3
- import type { IBasicRunnerOptions } from "../basic";
4
- import { CommonJsRunner } from "../cjs";
5
- export declare class JSDOMWebRunner<T extends ECompilerType = ECompilerType.Rspack> extends CommonJsRunner<T> {
6
- protected _webOptions: IBasicRunnerOptions<T>;
2
+ import type { TRunnerFile, TRunnerRequirer } from "../../type";
3
+ import { type INodeRunnerOptions, NodeRunner } from "../node";
4
+ export declare class JSDOMWebRunner<T extends ECompilerType = ECompilerType.Rspack> extends NodeRunner<T> {
5
+ protected _webOptions: INodeRunnerOptions<T>;
7
6
  private dom;
8
- constructor(_webOptions: IBasicRunnerOptions<T>);
7
+ constructor(_webOptions: INodeRunnerOptions<T>);
9
8
  run(file: string): Promise<unknown>;
10
9
  getGlobal(name: string): unknown;
11
10
  protected createResourceLoader(): {
@@ -13,8 +12,9 @@ export declare class JSDOMWebRunner<T extends ECompilerType = ECompilerType.Rspa
13
12
  element: HTMLScriptElement;
14
13
  }): any;
15
14
  };
16
- protected createBaseModuleScope(): import("../../type").IBasicModuleScope;
17
- protected getModuleContent(file: TBasicRunnerFile): [
15
+ protected createBaseModuleScope(): import("../../type").IModuleScope;
16
+ protected getModuleContent(file: TRunnerFile): [
17
+ Record<string, unknown>,
18
18
  {
19
19
  exports: Record<string, unknown>;
20
20
  },
@@ -11,10 +11,10 @@ const helper_1 = require("../../../helper");
11
11
  const createFakeWorker_1 = __importDefault(require("../../../helper/legacy/createFakeWorker"));
12
12
  const EventSourceForNode_1 = __importDefault(require("../../../helper/legacy/EventSourceForNode"));
13
13
  const urlToRelativePath_1 = __importDefault(require("../../../helper/legacy/urlToRelativePath"));
14
- const cjs_1 = require("../cjs");
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 cjs_1.CommonJsRunner {
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);
@@ -1,9 +1,9 @@
1
1
  import type { ITestEnv } from "../type";
2
2
  export type TRunnerRequirer = (currentDirectory: string, modulePath: string[] | string, context?: {
3
- file?: TBasicRunnerFile;
3
+ file?: TRunnerFile;
4
4
  esmMode?: EEsmMode;
5
5
  }) => Object | Promise<Object>;
6
- export type TBasicRunnerFile = {
6
+ export type TRunnerFile = {
7
7
  path: string;
8
8
  content: string;
9
9
  subPath: string;
@@ -13,12 +13,12 @@ export declare enum EEsmMode {
13
13
  Evaluated = 1,
14
14
  Unlinked = 2
15
15
  }
16
- export interface IBasicModuleScope extends ITestEnv {
16
+ export interface IModuleScope extends ITestEnv {
17
17
  console: Record<string, (...args: any[]) => void>;
18
18
  expect: jest.Expect;
19
19
  [key: string]: any;
20
20
  }
21
- export interface IBasicGlobalContext {
21
+ export interface IGlobalContext {
22
22
  console: Record<string, (...args: any[]) => void>;
23
23
  setTimeout: typeof setTimeout;
24
24
  clearTimeout: typeof clearTimeout;
@@ -1,4 +1,4 @@
1
- import type { ECompilerType, ITestEnv, ITestRunner, TCompilerOptions, TCompilerStatsCompilation } from "../type";
1
+ import { type ECompilerType, type ITestEnv, type ITestRunner, type TCompilerOptions, type TCompilerStatsCompilation } from "../type";
2
2
  import { BasicRunnerFactory } from "./basic";
3
3
  export declare class WatchRunnerFactory<T extends ECompilerType> extends BasicRunnerFactory<T> {
4
4
  protected getRunnerKey(file: string): string;
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.WatchRunnerFactory = void 0;
4
+ const type_1 = require("../type");
4
5
  const basic_1 = require("./basic");
5
- const watch_1 = require("./runner/watch");
6
+ const runner_1 = require("./runner");
6
7
  class WatchRunnerFactory extends basic_1.BasicRunnerFactory {
7
8
  getRunnerKey(file) {
8
9
  const stepName = this.context.getValue(this.name, "watchStepName");
@@ -41,16 +42,26 @@ class WatchRunnerFactory extends basic_1.BasicRunnerFactory {
41
42
  })
42
43
  : compilerOptions.target === "web" ||
43
44
  compilerOptions.target === "webworker";
44
- return new watch_1.WatchRunner({
45
+ const testConfig = this.context.getTestConfig();
46
+ const documentType = this.context.getValue(this.name, "documentType") || type_1.EDocumentType.Fake;
47
+ return new runner_1.WebRunner({
48
+ dom: documentType,
45
49
  env,
46
50
  stats,
47
51
  name: this.name,
48
- state,
49
- stepName,
50
52
  runInNewContext: isWeb,
51
- isWeb,
52
53
  cachable: false,
53
- testConfig: this.context.getTestConfig(),
54
+ testConfig: {
55
+ ...(testConfig || {}),
56
+ moduleScope: (ms, stats, options) => {
57
+ ms.STATE = state;
58
+ ms.WATCH_STEP = stepName;
59
+ if (typeof testConfig.moduleScope === "function") {
60
+ return testConfig.moduleScope(ms, stats, options);
61
+ }
62
+ return ms;
63
+ }
64
+ },
54
65
  source: this.context.getSource(),
55
66
  dist: this.context.getDist(),
56
67
  compilerOptions
@@ -38,7 +38,7 @@ export declare class BasicCaseCreator<T extends ECompilerType> {
38
38
  protected skip(name: string, reason: string | boolean): void;
39
39
  protected readTestConfig(src: string): TTestConfig<T>;
40
40
  protected checkSkipped(src: string, testConfig: TTestConfig<T>): boolean | string;
41
- protected createTester(name: string, src: string, dist: string, temp: string | void, testConfig: TTestConfig<T>): ITester;
41
+ protected createTester(name: string, src: string, dist: string, temp: string | undefined, testConfig: TTestConfig<T>): ITester;
42
42
  protected tryRunTask(): void;
43
43
  protected getMaxConcurrent(): number;
44
44
  protected registerConcurrentTask(name: string, starter: () => void): () => void;
@@ -275,6 +275,7 @@ class BasicCaseCreator {
275
275
  name,
276
276
  src,
277
277
  dist,
278
+ temp,
278
279
  testConfig,
279
280
  contextValue: this._options.contextValue,
280
281
  runnerFactory: this._options.runner,
@@ -1,6 +1,10 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.Tester = void 0;
7
+ const fs_extra_1 = __importDefault(require("fs-extra"));
4
8
  const context_1 = require("./context");
5
9
  class Tester {
6
10
  constructor(config) {
@@ -22,6 +26,11 @@ class Tester {
22
26
  return this.context;
23
27
  }
24
28
  async prepare() {
29
+ fs_extra_1.default.mkdirSync(this.context.getDist(), { recursive: true });
30
+ const tempDir = this.context.getTemp();
31
+ if (tempDir) {
32
+ fs_extra_1.default.mkdirSync(tempDir, { recursive: true });
33
+ }
25
34
  for (const i of this.steps) {
26
35
  if (typeof i.beforeAll === "function") {
27
36
  await i.beforeAll(this.context);
package/dist/type.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import type EventEmitter from "node:events";
2
2
  import type { Compilation as RspackCompilation, Compiler as RspackCompiler, MultiStats as RspackMultiStats, RspackOptions, Stats as RspackStats, StatsCompilation as RspackStatsCompilation } from "@rspack/core";
3
3
  import type { Compilation as WebpackCompilation, Compiler as WebpackCompiler, MultiStats as WebpackMultiStats, Configuration as WebpackOptions, Stats as WebpackStats, StatsCompilation as WebpackStatsCompilation } from "webpack";
4
- import type { IBasicModuleScope, TRunnerRequirer } from "./runner/type";
4
+ import type { IModuleScope, TRunnerRequirer } from "./runner/type";
5
5
  export interface ITestContext {
6
6
  getSource(sub?: string): string;
7
7
  getDist(sub?: string): string;
@@ -142,7 +142,7 @@ export type TTestConfig<T extends ECompilerType> = {
142
142
  writeStatsJson?: boolean;
143
143
  beforeExecute?: (options: TCompilerOptions<T>) => void;
144
144
  afterExecute?: (options: TCompilerOptions<T>) => void;
145
- moduleScope?: (ms: IBasicModuleScope, stats?: TCompilerStatsCompilation<T>, options?: TCompilerOptions<T>) => IBasicModuleScope;
145
+ moduleScope?: (ms: IModuleScope, stats?: TCompilerStatsCompilation<T>, options?: TCompilerOptions<T>) => IModuleScope;
146
146
  checkStats?: (stepName: string, jsonStats: TCompilerStatsCompilation<T> | undefined, stringStats: String) => boolean;
147
147
  findBundle?: (index: number, options: TCompilerOptions<T>, stepName?: string) => string | string[];
148
148
  bundlePath?: string[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rspack-canary/test-tools",
3
- "version": "1.5.6-canary-95c56d3d-20250917183900",
3
+ "version": "1.5.6-canary-9360f4d0-20250918174212",
4
4
  "license": "MIT",
5
5
  "description": "Test tools for rspack",
6
6
  "main": "dist/index.js",
@@ -100,10 +100,10 @@
100
100
  "wast-loader": "^1.14.1",
101
101
  "worker-rspack-loader": "^3.1.2",
102
102
  "exports-loader": "^5.0.0",
103
- "@rspack/cli": "npm:@rspack-canary/cli@1.5.6-canary-95c56d3d-20250917183900",
104
- "@rspack/core": "npm:@rspack-canary/core@1.5.6-canary-95c56d3d-20250917183900",
105
- "@rspack/binding-testing": "1.4.1",
106
- "@rspack/test-tools": "npm:@rspack-canary/test-tools@1.5.6-canary-95c56d3d-20250917183900"
103
+ "@rspack/cli": "npm:@rspack-canary/cli@1.5.6-canary-9360f4d0-20250918174212",
104
+ "@rspack/core": "npm:@rspack-canary/core@1.5.6-canary-9360f4d0-20250918174212",
105
+ "@rspack/test-tools": "npm:@rspack-canary/test-tools@1.5.6-canary-9360f4d0-20250918174212",
106
+ "@rspack/binding-testing": "1.4.1"
107
107
  },
108
108
  "peerDependencies": {
109
109
  "@rspack/core": ">=1.0.0"
@@ -1,5 +0,0 @@
1
- import { type ECompilerType, type ITestEnv, type ITestRunner, type TCompilerOptions, type TCompilerStatsCompilation } from "../type";
2
- import { HotRunnerFactory } from "./hot";
3
- export declare class HotStepRunnerFactory<T extends ECompilerType> extends HotRunnerFactory<T> {
4
- protected createRunner(file: string, stats: TCompilerStatsCompilation<T>, compilerOptions: TCompilerOptions<T>, env: ITestEnv): ITestRunner;
5
- }
@@ -1,87 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.HotStepRunnerFactory = void 0;
7
- const checkArrayExpectation_1 = __importDefault(require("../helper/legacy/checkArrayExpectation"));
8
- const type_1 = require("../type");
9
- const hot_1 = require("./hot");
10
- const web_1 = require("./runner/web");
11
- class HotStepRunnerFactory extends hot_1.HotRunnerFactory {
12
- createRunner(file, stats, compilerOptions, env) {
13
- const compiler = this.context.getCompiler(this.name);
14
- const testConfig = this.context.getTestConfig();
15
- const source = this.context.getSource();
16
- const dist = this.context.getDist();
17
- const hotUpdateContext = this.context.getValue(this.name, "hotUpdateContext");
18
- const next = async (callback) => {
19
- const usePromise = typeof callback === "function";
20
- try {
21
- hotUpdateContext.updateIndex++;
22
- const stats = await compiler.build();
23
- if (!stats) {
24
- throw new Error("Should generate stats during build");
25
- }
26
- const jsonStats = stats.toJson({
27
- // errorDetails: true
28
- });
29
- const checker = this.context.getValue(this.name, jsonStats.errors?.length
30
- ? "hotUpdateStepErrorChecker"
31
- : "hotUpdateStepChecker");
32
- checker(hotUpdateContext, stats, runner.getGlobal("__HMR_UPDATED_RUNTIME__"));
33
- await (0, checkArrayExpectation_1.default)(source, jsonStats, "error", `errors${hotUpdateContext.updateIndex}`, "Error");
34
- await (0, checkArrayExpectation_1.default)(source, jsonStats, "warning", `warnings${hotUpdateContext.updateIndex}`, "Warning");
35
- if (usePromise) {
36
- // old callback style hmr cases
37
- callback(null, jsonStats);
38
- }
39
- else {
40
- // new promise style hmr cases
41
- return jsonStats;
42
- }
43
- }
44
- catch (e) {
45
- if (usePromise) {
46
- callback(e);
47
- }
48
- else {
49
- throw e;
50
- }
51
- }
52
- };
53
- const nextHMR = async (m, options) => {
54
- const jsonStats = await next();
55
- const updatedModules = await m.hot.check(options || true);
56
- if (!updatedModules) {
57
- throw new Error("No update available");
58
- }
59
- return jsonStats;
60
- };
61
- const runner = new web_1.WebRunner({
62
- dom: this.context.getValue(this.name, "documentType") || type_1.EDocumentType.JSDOM,
63
- env,
64
- stats: this.createStatsGetter(),
65
- name: this.name,
66
- runInNewContext: false,
67
- testConfig: {
68
- ...testConfig,
69
- moduleScope(ms, stats) {
70
- const moduleScope = ms;
71
- if (typeof testConfig.moduleScope === "function") {
72
- testConfig.moduleScope(moduleScope, stats, compilerOptions);
73
- }
74
- moduleScope.NEXT = next;
75
- moduleScope.NEXT_HMR = nextHMR;
76
- return moduleScope;
77
- }
78
- },
79
- cachable: true,
80
- source,
81
- dist,
82
- compilerOptions
83
- });
84
- return runner;
85
- }
86
- }
87
- exports.HotStepRunnerFactory = HotStepRunnerFactory;
@@ -1,5 +0,0 @@
1
- import type { ECompilerType, ITestEnv, ITestRunner, TCompilerOptions, TCompilerStatsCompilation } from "../type";
2
- import { BasicRunnerFactory } from "./basic";
3
- export declare class NormalRunnerFactory<T extends ECompilerType> extends BasicRunnerFactory<T> {
4
- protected createRunner(file: string, stats: () => TCompilerStatsCompilation<T>, compilerOptions: TCompilerOptions<T>, env: ITestEnv): ITestRunner;
5
- }
@@ -1,20 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.NormalRunnerFactory = void 0;
4
- const basic_1 = require("./basic");
5
- const normal_1 = require("./runner/normal");
6
- class NormalRunnerFactory extends basic_1.BasicRunnerFactory {
7
- createRunner(file, stats, compilerOptions, env) {
8
- return new normal_1.NormalRunner({
9
- env,
10
- cachable: true,
11
- name: this.name,
12
- runInNewContext: false,
13
- testConfig: this.context.getTestConfig(),
14
- source: this.context.getSource(),
15
- dist: this.context.getDist(),
16
- compilerOptions: compilerOptions
17
- });
18
- }
19
- }
20
- exports.NormalRunnerFactory = NormalRunnerFactory;
@@ -1,30 +0,0 @@
1
- import type { ECompilerType, ITestEnv, ITestRunner, TCompilerOptions, TCompilerStatsCompilation, TTestConfig } from "../../type";
2
- import type { IBasicGlobalContext, IBasicModuleScope, TBasicRunnerFile, TModuleObject, TRunnerRequirer } from "../type";
3
- export interface IBasicRunnerOptions<T extends ECompilerType> {
4
- env: ITestEnv;
5
- stats?: () => TCompilerStatsCompilation<T>;
6
- name: string;
7
- runInNewContext?: boolean;
8
- testConfig: TTestConfig<T>;
9
- source: string;
10
- dist: string;
11
- compilerOptions: TCompilerOptions<T>;
12
- cachable?: boolean;
13
- }
14
- export declare abstract class BasicRunner<T extends ECompilerType = ECompilerType.Rspack> implements ITestRunner {
15
- protected _options: IBasicRunnerOptions<T>;
16
- protected globalContext: IBasicGlobalContext | null;
17
- protected baseModuleScope: IBasicModuleScope | null;
18
- protected requirers: Map<string, TRunnerRequirer>;
19
- constructor(_options: IBasicRunnerOptions<T>);
20
- run(file: string): Promise<unknown>;
21
- getRequire(): TRunnerRequirer;
22
- getGlobal(name: string): unknown;
23
- protected abstract createGlobalContext(): IBasicGlobalContext;
24
- protected abstract createBaseModuleScope(): IBasicModuleScope;
25
- protected abstract createModuleScope(requireFn: TRunnerRequirer, m: TModuleObject, file: TBasicRunnerFile): IBasicModuleScope;
26
- protected getFile(modulePath: string[] | string, currentDirectory: string): TBasicRunnerFile | null;
27
- protected preExecute(code: string, file: TBasicRunnerFile): void;
28
- protected postExecute(m: Object, file: TBasicRunnerFile): void;
29
- protected createRunner(): void;
30
- }
@@ -1,119 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.BasicRunner = void 0;
7
- const node_fs_1 = __importDefault(require("node:fs"));
8
- const node_path_1 = __importDefault(require("node:path"));
9
- const urlToRelativePath_1 = __importDefault(require("../../helper/legacy/urlToRelativePath"));
10
- const isRelativePath = (p) => /^\.\.?\//.test(p);
11
- const getSubPath = (p) => {
12
- const lastSlash = p.lastIndexOf("/");
13
- let firstSlash = p.indexOf("/");
14
- if (lastSlash !== -1 && firstSlash !== lastSlash) {
15
- if (firstSlash !== -1) {
16
- let next = p.indexOf("/", firstSlash + 1);
17
- let dir = p.slice(firstSlash + 1, next);
18
- while (dir === ".") {
19
- firstSlash = next;
20
- next = p.indexOf("/", firstSlash + 1);
21
- dir = p.slice(firstSlash + 1, next);
22
- }
23
- }
24
- return p.slice(firstSlash + 1, lastSlash + 1);
25
- }
26
- return "";
27
- };
28
- const cached = new Map();
29
- class BasicRunner {
30
- constructor(_options) {
31
- this._options = _options;
32
- this.globalContext = null;
33
- this.baseModuleScope = null;
34
- this.requirers = new Map();
35
- }
36
- run(file) {
37
- if (!this.globalContext) {
38
- this.globalContext = this.createGlobalContext();
39
- }
40
- this.baseModuleScope = this.createBaseModuleScope();
41
- if (typeof this._options.testConfig.moduleScope === "function") {
42
- this._options.testConfig.moduleScope(this.baseModuleScope, this._options.stats, this._options.compilerOptions);
43
- }
44
- this.createRunner();
45
- const res = this.getRequire()(this._options.dist, file.startsWith("./") || file.startsWith("https://test.cases/")
46
- ? file
47
- : `./${file}`);
48
- if (typeof res === "object" && "then" in res) {
49
- return res;
50
- }
51
- return Promise.resolve(res);
52
- }
53
- getRequire() {
54
- const entryRequire = this.requirers.get("entry");
55
- return (currentDirectory, modulePath, context = {}) => {
56
- const p = Array.isArray(modulePath)
57
- ? modulePath
58
- : modulePath.split("?")[0];
59
- return entryRequire(currentDirectory, p, context);
60
- };
61
- }
62
- getGlobal(name) {
63
- return (this.globalContext || {})[name];
64
- }
65
- getFile(modulePath, currentDirectory) {
66
- const cacheKey = `${currentDirectory}|${modulePath}`;
67
- if (this._options.cachable && cached.has(cacheKey)) {
68
- return cached.get(cacheKey);
69
- }
70
- let res = null;
71
- if (Array.isArray(modulePath)) {
72
- res = {
73
- path: node_path_1.default.join(currentDirectory, ".array-require.js"),
74
- content: `module.exports = (${modulePath
75
- .map(arg => {
76
- return `require(${JSON.stringify(`./${arg}`)})`;
77
- })
78
- .join(", ")});`,
79
- subPath: ""
80
- };
81
- }
82
- else if (modulePath.startsWith("https://test.cases/")) {
83
- const relativePath = (0, urlToRelativePath_1.default)(modulePath);
84
- const absPath = node_path_1.default.join(currentDirectory, relativePath);
85
- res = {
86
- path: absPath,
87
- content: node_fs_1.default.readFileSync(absPath, "utf-8"),
88
- subPath: ""
89
- };
90
- }
91
- else if (isRelativePath(modulePath)) {
92
- const p = node_path_1.default.join(currentDirectory, modulePath);
93
- res = {
94
- path: p,
95
- content: node_fs_1.default.readFileSync(p, "utf-8"),
96
- subPath: getSubPath(modulePath)
97
- };
98
- }
99
- else if (node_path_1.default.isAbsolute(modulePath)) {
100
- res = {
101
- path: modulePath,
102
- content: node_fs_1.default.readFileSync(modulePath, "utf-8"),
103
- subPath: "absolute_path"
104
- };
105
- }
106
- if (this._options.cachable && res) {
107
- cached.set(cacheKey, res);
108
- }
109
- return res;
110
- }
111
- preExecute(code, file) { }
112
- postExecute(m, file) { }
113
- createRunner() {
114
- this.requirers.set("entry", (currentDirectory, modulePath, context = {}) => {
115
- throw new Error("Not implement");
116
- });
117
- }
118
- }
119
- exports.BasicRunner = BasicRunner;
@@ -1,16 +0,0 @@
1
- import type { ECompilerType } from "../../type";
2
- import type { IBasicGlobalContext, IBasicModuleScope, TBasicRunnerFile, TModuleObject, TRunnerRequirer } from "../type";
3
- import { BasicRunner } from "./basic";
4
- declare global {
5
- var printLogger: boolean;
6
- }
7
- export declare class CommonJsRunner<T extends ECompilerType = ECompilerType.Rspack> extends BasicRunner<T> {
8
- protected requireCache: any;
9
- protected createGlobalContext(): IBasicGlobalContext;
10
- protected createBaseModuleScope(): IBasicModuleScope;
11
- protected createModuleScope(requireFn: TRunnerRequirer, m: TModuleObject, file: TBasicRunnerFile): IBasicModuleScope;
12
- protected createRunner(): void;
13
- protected createMissRequirer(): TRunnerRequirer;
14
- protected createJsonRequirer(): TRunnerRequirer;
15
- protected createCjsRequirer(): TRunnerRequirer;
16
- }