@rspack/test-tools 0.6.2 → 0.6.3-canary-e0ce3b5-20240423115333

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 (37) hide show
  1. package/dist/helper/legacy/FakeDocument.d.ts +8 -5
  2. package/dist/helper/legacy/FakeDocument.js +41 -3
  3. package/dist/plugin/webpack-module-placeholder-plugin.js +9 -16
  4. package/dist/processor/hot-step.js +22 -6
  5. package/dist/runner/basic.d.ts +2 -2
  6. package/dist/runner/basic.js +12 -6
  7. package/dist/runner/hot-step.d.ts +2 -2
  8. package/dist/runner/hot-step.js +18 -10
  9. package/dist/runner/hot.d.ts +2 -2
  10. package/dist/runner/hot.js +15 -7
  11. package/dist/runner/multiple.d.ts +2 -2
  12. package/dist/runner/multiple.js +2 -2
  13. package/dist/runner/normal.d.ts +2 -2
  14. package/dist/runner/normal.js +1 -1
  15. package/dist/runner/runner/basic.d.ts +16 -10
  16. package/dist/runner/runner/basic.js +3 -103
  17. package/dist/runner/runner/cjs.d.ts +11 -0
  18. package/dist/runner/runner/cjs.js +117 -0
  19. package/dist/runner/runner/esm.d.ts +3 -3
  20. package/dist/runner/runner/esm.js +27 -27
  21. package/dist/runner/runner/normal.js +0 -1
  22. package/dist/runner/runner/watch.d.ts +4 -3
  23. package/dist/runner/runner/watch.js +2 -5
  24. package/dist/runner/runner/web/fake.d.ts +18 -0
  25. package/dist/runner/runner/{hot.js → web/fake.js} +53 -23
  26. package/dist/runner/runner/web/jsdom.d.ts +18 -0
  27. package/dist/runner/runner/web/jsdom.js +183 -0
  28. package/dist/runner/runner/web.d.ts +13 -13
  29. package/dist/runner/runner/web.js +18 -55
  30. package/dist/runner/type.d.ts +4 -11
  31. package/dist/runner/watch.d.ts +2 -2
  32. package/dist/runner/watch.js +2 -3
  33. package/dist/type.d.ts +3 -2
  34. package/package.json +3 -3
  35. package/dist/helper/legacy/createHotDocument.d.ts +0 -28
  36. package/dist/helper/legacy/createHotDocument.js +0 -81
  37. package/dist/runner/runner/hot.d.ts +0 -18
@@ -1,12 +1,12 @@
1
- export = FakeDocument;
2
- declare class FakeDocument {
3
- constructor(basePath: any);
1
+ export default class FakeDocument {
2
+ constructor(basePath: any, options?: {});
4
3
  head: FakeElement;
5
4
  body: FakeElement;
6
5
  baseURI: string;
7
6
  _elementsByTagName: Map<string, FakeElement[]>;
8
7
  _basePath: any;
9
8
  currentScript: any;
9
+ _options: {};
10
10
  createElement(type: any): FakeElement;
11
11
  _onElementAttached(element: any): void;
12
12
  _onElementRemoved(element: any): void;
@@ -15,7 +15,7 @@ declare class FakeDocument {
15
15
  getPropertyValue: (property: any) => any;
16
16
  };
17
17
  }
18
- declare class FakeElement {
18
+ export class FakeElement {
19
19
  constructor(document: any, type: any, basePath: any);
20
20
  _document: any;
21
21
  _type: any;
@@ -23,8 +23,10 @@ declare class FakeElement {
23
23
  _attributes: any;
24
24
  _src: any;
25
25
  _href: any;
26
+ rel: any;
26
27
  parentNode: any;
27
28
  sheet: FakeSheet | undefined;
29
+ insertBefore(node: any, before: any): void;
28
30
  appendChild(node: any): void;
29
31
  removeChild(node: any): void;
30
32
  setAttribute(name: any, value: any): void;
@@ -33,10 +35,11 @@ declare class FakeElement {
33
35
  _toRealUrl(value: any): any;
34
36
  set src(arg: any);
35
37
  get src(): any;
38
+ get children(): any[];
36
39
  set href(arg: any);
37
40
  get href(): any;
38
41
  }
39
- declare class FakeSheet {
42
+ export class FakeSheet {
40
43
  constructor(element: any, basePath: any);
41
44
  _element: any;
42
45
  _basePath: any;
@@ -1,12 +1,14 @@
1
1
  "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FakeSheet = exports.FakeElement = void 0;
2
4
  // @ts-nocheck
3
5
  const fs = require("fs");
4
6
  const path = require("path");
5
7
  const getPropertyValue = function (property) {
6
8
  return this[property];
7
9
  };
8
- module.exports = class FakeDocument {
9
- constructor(basePath) {
10
+ class FakeDocument {
11
+ constructor(basePath, options = {}) {
10
12
  this.head = this.createElement("head");
11
13
  this.body = this.createElement("body");
12
14
  this.baseURI = "https://test.cases/path/index.html";
@@ -16,6 +18,7 @@ module.exports = class FakeDocument {
16
18
  ]);
17
19
  this._basePath = basePath;
18
20
  this.currentScript = undefined;
21
+ this._options = options || {};
19
22
  }
20
23
  createElement(type) {
21
24
  return new FakeElement(this, type, this._basePath);
@@ -50,7 +53,8 @@ module.exports = class FakeDocument {
50
53
  }
51
54
  return style;
52
55
  }
53
- };
56
+ }
57
+ exports.default = FakeDocument;
54
58
  class FakeElement {
55
59
  constructor(document, type, basePath) {
56
60
  this._document = document;
@@ -59,9 +63,20 @@ class FakeElement {
59
63
  this._attributes = Object.create(null);
60
64
  this._src = undefined;
61
65
  this._href = undefined;
66
+ this.rel = undefined;
62
67
  this.parentNode = undefined;
63
68
  this.sheet = type === "link" ? new FakeSheet(this, basePath) : undefined;
64
69
  }
70
+ insertBefore(node, before) {
71
+ this._document._onElementAttached(node);
72
+ node.parentNode = this;
73
+ this._children.unshift(node);
74
+ Promise.resolve().then(() => {
75
+ if (node.onload) {
76
+ node.onload({ type: "load", target: node });
77
+ }
78
+ });
79
+ }
65
80
  appendChild(node) {
66
81
  this._document._onElementAttached(node);
67
82
  this._children.push(node);
@@ -72,6 +87,24 @@ class FakeElement {
72
87
  node.onload({ type: "load", target: node });
73
88
  }, 100);
74
89
  }
90
+ else if (node._type === "script") {
91
+ Promise.resolve().then(() => {
92
+ if (typeof this._document._options.onScript === "function") {
93
+ this._document._options.onScript(node);
94
+ }
95
+ if (node.onload) {
96
+ node.onload({
97
+ type: "load",
98
+ target: node
99
+ });
100
+ }
101
+ });
102
+ }
103
+ else {
104
+ if (node.onload) {
105
+ node.onload({ type: "load", target: node });
106
+ }
107
+ }
75
108
  }
76
109
  removeChild(node) {
77
110
  const idx = this._children.indexOf(node);
@@ -125,6 +158,9 @@ class FakeElement {
125
158
  this._src = this._toRealUrl(value);
126
159
  }
127
160
  }
161
+ get children() {
162
+ return this._children;
163
+ }
128
164
  get src() {
129
165
  return this._src;
130
166
  }
@@ -137,6 +173,7 @@ class FakeElement {
137
173
  return this._href;
138
174
  }
139
175
  }
176
+ exports.FakeElement = FakeElement;
140
177
  class FakeSheet {
141
178
  constructor(element, basePath) {
142
179
  this._element = element;
@@ -211,3 +248,4 @@ class FakeSheet {
211
248
  return rules;
212
249
  }
213
250
  }
251
+ exports.FakeSheet = FakeSheet;
@@ -4,7 +4,6 @@ exports.WebpackModulePlaceholderPlugin = void 0;
4
4
  // @ts-nocheck
5
5
  const { ConcatSource, RawSource, CachedSource, PrefixSource } = require("webpack-sources");
6
6
  const path = require("path");
7
- const which = require("which-module");
8
7
  function createRenderRuntimeModulesFn(Template) {
9
8
  return function renderRuntimeModules(runtimeModules, renderContext) {
10
9
  const source = new ConcatSource();
@@ -54,19 +53,17 @@ const caches = new WeakMap();
54
53
  class WebpackModulePlaceholderPlugin {
55
54
  constructor() { }
56
55
  apply(compiler) {
57
- const webpackLibPath = which(compiler.constructor).path;
58
- const Template = require(path.join(webpackLibPath, "Template.js"));
56
+ const { webpack } = compiler;
57
+ const { Template, javascript: { JavascriptModulesPlugin } } = webpack;
59
58
  Template.renderRuntimeModules = createRenderRuntimeModulesFn(Template);
60
- const JavascriptModulesPlugin = require(path.join(webpackLibPath, "javascript/JavascriptModulesPlugin.js"));
61
59
  compiler.hooks.compilation.tap("RuntimeDiffPlugin", compilation => {
62
60
  const hooks = JavascriptModulesPlugin.getCompilationHooks(compilation);
63
61
  hooks.inlineInRuntimeBailout.tap("RuntimeDiffPlugin", () => "not allow inline startup");
64
- hooks.renderModulePackage.tap("RuntimeDiffPlugin", (moduleSource, module, { chunk, chunkGraph, moduleGraph, runtimeTemplate }) => {
65
- const { requestShortener } = runtimeTemplate;
62
+ hooks.renderModulePackage.tap("RuntimeDiffPlugin", (moduleSource, module) => {
66
63
  let cacheEntry;
67
- let cache = caches.get(requestShortener);
64
+ let cache = caches.get(compilation);
68
65
  if (cache === undefined) {
69
- caches.set(requestShortener, (cache = new WeakMap()));
66
+ caches.set(compilation, (cache = new WeakMap()));
70
67
  cache.set(module, (cacheEntry = {
71
68
  header: undefined,
72
69
  footer: undefined,
@@ -92,14 +89,10 @@ class WebpackModulePlaceholderPlugin {
92
89
  let header = cacheEntry.header;
93
90
  let footer = cacheEntry.footer;
94
91
  if (header === undefined) {
95
- const req = module.readableIdentifier(requestShortener);
96
- let reqStr = req.replace(/\*\//g, "*_/");
97
- // handle css module identifier
98
- if (reqStr.startsWith("css ")) {
99
- reqStr = reqStr.replace(/^css[\s]+/, "").trim();
100
- }
101
- header = new RawSource(`\n/* start::${reqStr} */\n`);
102
- footer = new RawSource(`\n/* end::${reqStr} */\n`);
92
+ const identifier = module.identifier();
93
+ const moduleId = compilation.chunkGraph.getModuleId(module);
94
+ header = new RawSource(`\n${Template.toNormalComment(`start::${moduleId}::${identifier}`)}\n`);
95
+ footer = new RawSource(`\n${Template.toNormalComment(`end::${moduleId}::${identifier}`)}\n`);
103
96
  cacheEntry.header = header;
104
97
  cacheEntry.footer = footer;
105
98
  }
@@ -65,9 +65,20 @@ class RspackHotStepProcessor extends hot_1.RspackHotProcessor {
65
65
  for (let entry of ((stats === null || stats === void 0 ? void 0 : stats.compilation.chunks) || []).filter(i => i.hasRuntime())) {
66
66
  this.entries[entry.id] = entry.runtime;
67
67
  }
68
- this.matchStepSnapshot(context, 0, statsJson);
68
+ let matchFailed = null;
69
+ try {
70
+ this.matchStepSnapshot(context, 0, statsJson);
71
+ }
72
+ catch (e) {
73
+ matchFailed = e;
74
+ }
69
75
  this.hashes.push(stats.hash);
70
- await super.check(env, context);
76
+ if (matchFailed) {
77
+ throw matchFailed;
78
+ }
79
+ else {
80
+ await super.check(env, context);
81
+ }
71
82
  }
72
83
  matchStepSnapshot(context, step, stats) {
73
84
  const compiler = this.getCompiler(context);
@@ -111,6 +122,7 @@ class RspackHotStepProcessor extends hot_1.RspackHotProcessor {
111
122
  };
112
123
  const fileList = stats
113
124
  .assets.map(i => {
125
+ var _a, _b, _c;
114
126
  const fileName = i.name;
115
127
  const renderName = replaceFileName(fileName);
116
128
  const content = replaceContent(fs_extra_1.default.readFileSync(context.getDist(fileName), "utf-8"));
@@ -131,20 +143,24 @@ class RspackHotStepProcessor extends hot_1.RspackHotProcessor {
131
143
  return `- Update: ${renderName}, size: ${i.size}`;
132
144
  }
133
145
  else if (fileName.endsWith("hot-update.json")) {
146
+ const manifest = JSON.parse(content);
147
+ (_a = manifest.c) === null || _a === void 0 ? void 0 : _a.sort();
148
+ (_b = manifest.r) === null || _b === void 0 ? void 0 : _b.sort();
149
+ (_c = manifest.m) === null || _c === void 0 ? void 0 : _c.sort();
134
150
  hotUpdateManifest.push({
135
151
  name: renderName,
136
- content
152
+ content: JSON.stringify(manifest)
137
153
  });
138
154
  return `- Manifest: ${renderName}, size: ${i.size}`;
139
155
  }
140
156
  else if (fileName.endsWith(".js")) {
141
- return `- Bundle: ${renderName}, size: ${i.size}`;
157
+ return `- Bundle: ${renderName}`;
142
158
  }
143
159
  })
144
160
  .filter(Boolean);
145
161
  fileList.sort();
146
- hotUpdateManifest.sort();
147
- hotUpdateFile.sort();
162
+ hotUpdateManifest.sort((a, b) => (a.name > b.name ? 1 : -1));
163
+ hotUpdateFile.sort((a, b) => (a.name > b.name ? 1 : -1));
148
164
  let content = `
149
165
  # ${title}
150
166
 
@@ -1,9 +1,9 @@
1
- import { ECompilerType, ITestContext, ITestEnv, ITestRunner, TCompilerOptions, TRunnerFactory } from "../type";
1
+ import { ECompilerType, ITestContext, ITestEnv, ITestRunner, TCompilerOptions, TCompilerStatsCompilation, TRunnerFactory } from "../type";
2
2
  export declare class BasicRunnerFactory<T extends ECompilerType> implements TRunnerFactory<T> {
3
3
  protected name: string;
4
4
  protected context: ITestContext;
5
5
  constructor(name: string, context: ITestContext);
6
6
  create(file: string, compilerOptions: TCompilerOptions<T>, env: ITestEnv): ITestRunner;
7
7
  protected getRunnerKey(file: string): string;
8
- protected createRunner(file: string, compilerOptions: TCompilerOptions<T>, env: ITestEnv): ITestRunner;
8
+ protected createRunner(file: string, stats: TCompilerStatsCompilation<T>, compilerOptions: TCompilerOptions<T>, env: ITestEnv): ITestRunner;
9
9
  }
@@ -14,19 +14,21 @@ class BasicRunnerFactory {
14
14
  if (exists) {
15
15
  return exists;
16
16
  }
17
- const runner = this.createRunner(file, compilerOptions, env);
17
+ const compiler = this.context.getCompiler(this.name);
18
+ const stats = compiler.getStats().toJson({
19
+ errorDetails: true
20
+ });
21
+ const runner = this.createRunner(file, stats, compilerOptions, env);
18
22
  this.context.setRunner(key, runner);
19
23
  return runner;
20
24
  }
21
25
  getRunnerKey(file) {
22
26
  return this.name;
23
27
  }
24
- createRunner(file, compilerOptions, env) {
25
- const compiler = this.context.getCompiler(this.name);
26
- const stats = compiler.getStats();
28
+ createRunner(file, stats, compilerOptions, env) {
27
29
  const runnerOptions = {
28
30
  env,
29
- stats: stats,
31
+ stats,
30
32
  name: this.name,
31
33
  testConfig: this.context.getTestConfig(),
32
34
  source: this.context.getSource(),
@@ -35,7 +37,11 @@ class BasicRunnerFactory {
35
37
  };
36
38
  if (compilerOptions.target === "web" ||
37
39
  compilerOptions.target === "webworker") {
38
- return new web_1.WebRunner(runnerOptions);
40
+ return new web_1.WebRunner({
41
+ ...runnerOptions,
42
+ runInNewContext: true,
43
+ dom: "fake"
44
+ });
39
45
  }
40
46
  else {
41
47
  return new esm_1.EsmRunner(runnerOptions);
@@ -1,5 +1,5 @@
1
- import { ECompilerType, ITestEnv, ITestRunner, TCompilerOptions } from "../type";
1
+ import { ECompilerType, ITestEnv, ITestRunner, TCompilerOptions, TCompilerStatsCompilation } from "../type";
2
2
  import { HotRunnerFactory } from "./hot";
3
3
  export declare class HotStepRunnerFactory<T extends ECompilerType> extends HotRunnerFactory<T> {
4
- protected createRunner(file: string, compilerOptions: TCompilerOptions<T>, env: ITestEnv): ITestRunner;
4
+ protected createRunner(file: string, stats: TCompilerStatsCompilation<T>, compilerOptions: TCompilerOptions<T>, env: ITestEnv): ITestRunner;
5
5
  }
@@ -1,13 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.HotStepRunnerFactory = void 0;
4
- const hot_1 = require("./runner/hot");
5
- const hot_2 = require("./hot");
6
- class HotStepRunnerFactory extends hot_2.HotRunnerFactory {
7
- createRunner(file, compilerOptions, env) {
4
+ const hot_1 = require("./hot");
5
+ const web_1 = require("./runner/web");
6
+ class HotStepRunnerFactory extends hot_1.HotRunnerFactory {
7
+ createRunner(file, stats, compilerOptions, env) {
8
8
  const compiler = this.context.getCompiler(this.name);
9
9
  const testConfig = this.context.getTestConfig();
10
- const stats = compiler.getStats();
11
10
  const source = this.context.getSource();
12
11
  const dist = this.context.getDist();
13
12
  const hotUpdateContext = this.context.getValue(this.name, "hotUpdateContext");
@@ -20,7 +19,7 @@ class HotStepRunnerFactory extends hot_2.HotRunnerFactory {
20
19
  if (!stats)
21
20
  return callback(new Error("Should generate stats during build"));
22
21
  const jsonStats = stats.toJson({
23
- // errorDetails: true
22
+ errorDetails: true
24
23
  });
25
24
  try {
26
25
  const checker = this.context.getValue(this.name, ((_a = jsonStats.errors) === null || _a === void 0 ? void 0 : _a.length)
@@ -35,15 +34,24 @@ class HotStepRunnerFactory extends hot_2.HotRunnerFactory {
35
34
  })
36
35
  .catch(callback);
37
36
  };
38
- return new hot_1.HotRunner({
37
+ return new web_1.WebRunner({
38
+ dom: "jsdom",
39
39
  env,
40
- stats: stats,
40
+ stats,
41
41
  name: this.name,
42
42
  runInNewContext: false,
43
- testConfig,
43
+ testConfig: {
44
+ ...testConfig,
45
+ moduleScope(ms) {
46
+ if (typeof testConfig.moduleScope === "function") {
47
+ ms = testConfig.moduleScope(ms);
48
+ }
49
+ ms["NEXT"] = next;
50
+ return ms;
51
+ }
52
+ },
44
53
  source,
45
54
  dist,
46
- next,
47
55
  compilerOptions
48
56
  });
49
57
  }
@@ -1,5 +1,5 @@
1
- import { ECompilerType, ITestEnv, ITestRunner, TCompilerOptions } from "../type";
1
+ import { ECompilerType, ITestEnv, ITestRunner, TCompilerOptions, TCompilerStatsCompilation } from "../type";
2
2
  import { BasicRunnerFactory } from "./basic";
3
3
  export declare class HotRunnerFactory<T extends ECompilerType> extends BasicRunnerFactory<T> {
4
- protected createRunner(file: string, compilerOptions: TCompilerOptions<T>, env: ITestEnv): ITestRunner;
4
+ protected createRunner(file: string, stats: TCompilerStatsCompilation<T>, compilerOptions: TCompilerOptions<T>, env: ITestEnv): ITestRunner;
5
5
  }
@@ -6,12 +6,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.HotRunnerFactory = void 0;
7
7
  const checkArrayExpectation_1 = __importDefault(require("../helper/legacy/checkArrayExpectation"));
8
8
  const basic_1 = require("./basic");
9
- const hot_1 = require("./runner/hot");
9
+ const web_1 = require("./runner/web");
10
10
  class HotRunnerFactory extends basic_1.BasicRunnerFactory {
11
- createRunner(file, compilerOptions, env) {
11
+ createRunner(file, stats, compilerOptions, env) {
12
12
  const compiler = this.context.getCompiler(this.name);
13
13
  const testConfig = this.context.getTestConfig();
14
- const stats = compiler.getStats();
15
14
  const source = this.context.getSource();
16
15
  const dist = this.context.getDist();
17
16
  const hotUpdateContext = this.context.getValue(this.name, "hotUpdateContext");
@@ -35,15 +34,24 @@ class HotRunnerFactory extends basic_1.BasicRunnerFactory {
35
34
  })
36
35
  .catch(callback);
37
36
  };
38
- return new hot_1.HotRunner({
37
+ return new web_1.WebRunner({
38
+ dom: "fake",
39
39
  env,
40
- stats: stats,
40
+ stats,
41
41
  name: this.name,
42
42
  runInNewContext: false,
43
- testConfig,
43
+ testConfig: {
44
+ ...testConfig,
45
+ moduleScope(ms) {
46
+ if (typeof testConfig.moduleScope === "function") {
47
+ ms = testConfig.moduleScope(ms);
48
+ }
49
+ ms["NEXT"] = next;
50
+ return ms;
51
+ }
52
+ },
44
53
  source,
45
54
  dist,
46
- next,
47
55
  compilerOptions
48
56
  });
49
57
  }
@@ -1,9 +1,9 @@
1
- import { ECompilerType, ITestEnv, ITestRunner, TCompilerOptions } from "../type";
1
+ import { ECompilerType, ITestEnv, ITestRunner, TCompilerOptions, TCompilerStatsCompilation } from "../type";
2
2
  import { BasicRunnerFactory } from "./basic";
3
3
  export declare class MultipleRunnerFactory<T extends ECompilerType> extends BasicRunnerFactory<T> {
4
4
  protected runned: Set<string>;
5
5
  protected getRunnerKey(file: string): string;
6
- protected createRunner(file: string, compilerOptions: TCompilerOptions<T>, env: ITestEnv): ITestRunner;
6
+ protected createRunner(file: string, stats: TCompilerStatsCompilation<T>, compilerOptions: TCompilerOptions<T>, env: ITestEnv): ITestRunner;
7
7
  protected getFileIndexHandler(file: string): {
8
8
  getIndex: () => number[];
9
9
  flagIndex: () => Set<string>;
@@ -12,11 +12,11 @@ class MultipleRunnerFactory extends basic_1.BasicRunnerFactory {
12
12
  const [index, seq] = getIndex();
13
13
  return `${this.name}-${index}[${seq}]`;
14
14
  }
15
- createRunner(file, compilerOptions, env) {
15
+ createRunner(file, stats, compilerOptions, env) {
16
16
  const multiCompilerOptions = this.context.getValue(this.name, "multiCompilerOptions") || [];
17
17
  const { getIndex, flagIndex } = this.getFileIndexHandler(file);
18
18
  const [index] = getIndex();
19
- const runner = super.createRunner(file, multiCompilerOptions[index], env);
19
+ const runner = super.createRunner(file, stats.children[index], multiCompilerOptions[index], env);
20
20
  flagIndex();
21
21
  return runner;
22
22
  }
@@ -1,5 +1,5 @@
1
- import { ECompilerType, ITestEnv, ITestRunner, TCompilerOptions } from "../type";
1
+ import { ECompilerType, ITestEnv, ITestRunner, TCompilerOptions, TCompilerStatsCompilation } from "../type";
2
2
  import { BasicRunnerFactory } from "./basic";
3
3
  export declare class NormalRunnerFactory<T extends ECompilerType> extends BasicRunnerFactory<T> {
4
- protected createRunner(file: string, compilerOptions: TCompilerOptions<T>, env: ITestEnv): ITestRunner;
4
+ protected createRunner(file: string, stats: TCompilerStatsCompilation<T>, compilerOptions: TCompilerOptions<T>, env: ITestEnv): ITestRunner;
5
5
  }
@@ -4,7 +4,7 @@ exports.NormalRunnerFactory = void 0;
4
4
  const basic_1 = require("./basic");
5
5
  const normal_1 = require("./runner/normal");
6
6
  class NormalRunnerFactory extends basic_1.BasicRunnerFactory {
7
- createRunner(file, compilerOptions, env) {
7
+ createRunner(file, stats, compilerOptions, env) {
8
8
  return new normal_1.NormalRunner({
9
9
  env,
10
10
  name: this.name,
@@ -1,6 +1,16 @@
1
- import { ECompilerType, ITestRunner } from "../../type";
2
- import { IBasicGlobalContext, IBasicModuleScope, IBasicRunnerOptions, TBasicRunnerFile, TRunnerRequirer } from "../type";
3
- export declare class BasicRunner<T extends ECompilerType = ECompilerType.Rspack> implements ITestRunner {
1
+ import { ECompilerType, ITestEnv, ITestRunner, TCompilerOptions, TCompilerStatsCompilation, TTestConfig } from "../../type";
2
+ import { 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
+ }
13
+ export declare abstract class BasicRunner<T extends ECompilerType = ECompilerType.Rspack> implements ITestRunner {
4
14
  protected _options: IBasicRunnerOptions<T>;
5
15
  protected globalContext: IBasicGlobalContext | null;
6
16
  protected baseModuleScope: IBasicModuleScope | null;
@@ -8,14 +18,10 @@ export declare class BasicRunner<T extends ECompilerType = ECompilerType.Rspack>
8
18
  constructor(_options: IBasicRunnerOptions<T>);
9
19
  run(file: string): Promise<unknown>;
10
20
  getRequire(): TRunnerRequirer;
11
- protected createGlobalContext(): IBasicGlobalContext;
12
- protected createBaseModuleScope(): IBasicModuleScope;
13
- protected createModuleScope(requireFn: TRunnerRequirer, m: {
14
- exports: unknown;
15
- }, file: TBasicRunnerFile): IBasicModuleScope;
21
+ protected abstract createGlobalContext(): IBasicGlobalContext;
22
+ protected abstract createBaseModuleScope(): IBasicModuleScope;
23
+ protected abstract createModuleScope(requireFn: TRunnerRequirer, m: TModuleObject, file: TBasicRunnerFile): IBasicModuleScope;
16
24
  protected getFile(modulePath: string[] | string, currentDirectory: string): TBasicRunnerFile | null;
17
- protected createMissRequirer(): TRunnerRequirer;
18
- protected createCjsRequirer(): TRunnerRequirer;
19
25
  protected preExecute(code: string, file: TBasicRunnerFile): void;
20
26
  protected postExecute(m: Object, file: TBasicRunnerFile): void;
21
27
  protected createRunner(): void;
@@ -4,13 +4,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.BasicRunner = void 0;
7
- const vm_1 = __importDefault(require("vm"));
8
7
  const path_1 = __importDefault(require("path"));
9
8
  const fs_1 = __importDefault(require("fs"));
10
- const define = function (...args) {
11
- const factory = args.pop();
12
- factory();
13
- };
14
9
  const isRelativePath = (p) => /^\.\.?\//.test(p);
15
10
  const getSubPath = (p) => {
16
11
  const lastSlash = p.lastIndexOf("/");
@@ -54,51 +49,6 @@ class BasicRunner {
54
49
  getRequire() {
55
50
  return this.requirers.get("entry");
56
51
  }
57
- createGlobalContext() {
58
- return {
59
- console: console,
60
- expect: expect,
61
- setTimeout: ((cb, ms, ...args) => {
62
- let timeout = setTimeout(cb, ms, ...args);
63
- timeout.unref();
64
- return timeout;
65
- }),
66
- clearTimeout: clearTimeout
67
- };
68
- }
69
- createBaseModuleScope() {
70
- const baseModuleScope = {
71
- console: console,
72
- it: this._options.env.it,
73
- beforeEach: this._options.env.beforeEach,
74
- afterEach: this._options.env.afterEach,
75
- expect,
76
- jest,
77
- __STATS__: this._options.stats,
78
- nsObj: (m) => {
79
- Object.defineProperty(m, Symbol.toStringTag, {
80
- value: "Module"
81
- });
82
- return m;
83
- }
84
- };
85
- if (this._options.stats) {
86
- baseModuleScope["__STATS__"] = this._options.stats;
87
- }
88
- return baseModuleScope;
89
- }
90
- createModuleScope(requireFn, m, file) {
91
- return {
92
- ...this.baseModuleScope,
93
- require: requireFn.bind(null, path_1.default.dirname(file.path)),
94
- module: m,
95
- exports: m.exports,
96
- __dirname: path_1.default.dirname(file.path),
97
- __filename: file.path,
98
- _globalAssign: { expect },
99
- define
100
- };
101
- }
102
52
  getFile(modulePath, currentDirectory) {
103
53
  if (Array.isArray(modulePath)) {
104
54
  return {
@@ -123,62 +73,12 @@ class BasicRunner {
123
73
  return null;
124
74
  }
125
75
  }
126
- createMissRequirer() {
127
- return (currentDirectory, modulePath, context = {}) => {
128
- const modulePathStr = modulePath;
129
- const modules = this._options.testConfig.modules;
130
- if (modules && modulePathStr in modules) {
131
- return modules[modulePathStr];
132
- }
133
- else {
134
- return require(modulePathStr.startsWith("node:")
135
- ? modulePathStr.slice(5)
136
- : modulePathStr);
137
- }
138
- };
139
- }
140
- createCjsRequirer() {
141
- const requireCache = Object.create(null);
142
- return (currentDirectory, modulePath, context = {}) => {
143
- let file = context["file"] || this.getFile(modulePath, currentDirectory);
144
- if (!file) {
145
- return this.requirers.get("miss")(currentDirectory, modulePath);
146
- }
147
- if (file.path in requireCache) {
148
- return requireCache[file.path].exports;
149
- }
150
- const m = {
151
- exports: {}
152
- };
153
- requireCache[file.path] = m;
154
- const currentModuleScope = this.createModuleScope(this.getRequire(), m, file);
155
- if (this._options.testConfig.moduleScope) {
156
- this._options.testConfig.moduleScope(currentModuleScope);
157
- }
158
- if (!this._options.runInNewContext) {
159
- file.content = `Object.assign(global, _globalAssign);\n ${file.content}`;
160
- }
161
- const args = Object.keys(currentModuleScope);
162
- const argValues = args.map(arg => currentModuleScope[arg]);
163
- const code = `(function(${args.join(", ")}) {
164
- ${file.content}
165
- })`;
166
- this.preExecute(code, file);
167
- const fn = this._options.runInNewContext
168
- ? vm_1.default.runInNewContext(code, this.globalContext, file.path)
169
- : vm_1.default.runInThisContext(code, file.path);
170
- fn.call(this._options.testConfig.nonEsmThis
171
- ? this._options.testConfig.nonEsmThis(modulePath)
172
- : m.exports, ...argValues);
173
- this.postExecute(m, file);
174
- return m.exports;
175
- };
176
- }
177
76
  preExecute(code, file) { }
178
77
  postExecute(m, file) { }
179
78
  createRunner() {
180
- this.requirers.set("miss", this.createMissRequirer());
181
- this.requirers.set("entry", this.createCjsRequirer());
79
+ this.requirers.set("entry", (currentDirectory, modulePath, context = {}) => {
80
+ throw new Error("Not implement");
81
+ });
182
82
  }
183
83
  }
184
84
  exports.BasicRunner = BasicRunner;
@@ -0,0 +1,11 @@
1
+ import { ECompilerType } from "../../type";
2
+ import { IBasicGlobalContext, IBasicModuleScope, TBasicRunnerFile, TModuleObject, TRunnerRequirer } from "../type";
3
+ import { BasicRunner } from "./basic";
4
+ export declare class CommonJsRunner<T extends ECompilerType = ECompilerType.Rspack> extends BasicRunner<T> {
5
+ protected createGlobalContext(): IBasicGlobalContext;
6
+ protected createBaseModuleScope(): IBasicModuleScope;
7
+ protected createModuleScope(requireFn: TRunnerRequirer, m: TModuleObject, file: TBasicRunnerFile): IBasicModuleScope;
8
+ protected createRunner(): void;
9
+ protected createMissRequirer(): TRunnerRequirer;
10
+ protected createCjsRequirer(): TRunnerRequirer;
11
+ }