@rspack-debug/test-tools 1.4.11 → 1.6.4

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 (259) hide show
  1. package/README.md +1 -1
  2. package/dist/case/builtin.d.ts +3 -0
  3. package/dist/case/builtin.js +152 -14
  4. package/dist/case/cache.d.ts +2 -2
  5. package/dist/case/cache.js +214 -12
  6. package/dist/case/common.d.ts +11 -0
  7. package/dist/case/common.js +249 -0
  8. package/dist/case/compiler.d.ts +17 -4
  9. package/dist/case/compiler.js +149 -12
  10. package/dist/case/config.d.ts +7 -2
  11. package/dist/case/config.js +106 -18
  12. package/dist/case/defaults.d.ts +13 -4
  13. package/dist/case/defaults.js +73 -18
  14. package/dist/case/diagnostic.d.ts +6 -0
  15. package/dist/case/diagnostic.js +121 -14
  16. package/dist/case/error.d.ts +15 -4
  17. package/dist/case/error.js +137 -12
  18. package/dist/case/esm-output.d.ts +1 -0
  19. package/dist/case/esm-output.js +117 -0
  20. package/dist/case/example.d.ts +1 -0
  21. package/dist/case/example.js +57 -0
  22. package/dist/case/hash.d.ts +2 -2
  23. package/dist/case/hash.js +72 -9
  24. package/dist/case/hook.d.ts +36 -4
  25. package/dist/case/hook.js +209 -20
  26. package/dist/case/hot-step.d.ts +2 -2
  27. package/dist/case/hot-step.js +309 -13
  28. package/dist/case/hot.d.ts +11 -2
  29. package/dist/case/hot.js +217 -12
  30. package/dist/case/incremental.d.ts +2 -2
  31. package/dist/case/incremental.js +24 -45
  32. package/dist/case/index.d.ts +31 -20
  33. package/dist/case/index.js +50 -34
  34. package/dist/case/multi-compiler.d.ts +19 -0
  35. package/dist/case/multi-compiler.js +97 -0
  36. package/dist/case/{nativeWatcher.js → native-watcher.js} +11 -23
  37. package/dist/case/normal.d.ts +3 -0
  38. package/dist/case/normal.js +202 -18
  39. package/dist/case/runner.d.ts +19 -0
  40. package/dist/case/runner.js +113 -0
  41. package/dist/case/serial.d.ts +2 -2
  42. package/dist/case/serial.js +10 -14
  43. package/dist/case/stats-api.d.ts +8 -3
  44. package/dist/case/stats-api.js +69 -10
  45. package/dist/case/stats-output.d.ts +11 -0
  46. package/dist/case/stats-output.js +199 -8
  47. package/dist/case/treeshaking.js +34 -7
  48. package/dist/case/watch.d.ts +27 -0
  49. package/dist/case/watch.js +318 -22
  50. package/dist/compiler.d.ts +16 -16
  51. package/dist/compiler.js +120 -15
  52. package/dist/helper/directory.d.ts +1 -1
  53. package/dist/helper/directory.js +6 -2
  54. package/dist/helper/disable-iconv-lite-warning.d.ts +1 -0
  55. package/dist/helper/disable-iconv-lite-warning.js +12 -0
  56. package/dist/helper/expect/error.js +1 -1
  57. package/dist/helper/expect/placeholder.js +7 -7
  58. package/dist/helper/expect/to-match-file-snapshot.d.ts +1 -2
  59. package/dist/helper/expect/to-match-file-snapshot.js +3 -3
  60. package/dist/helper/hot-update/loader.d.ts +2 -1
  61. package/dist/helper/hot-update/loader.js +5 -30
  62. package/dist/helper/hot-update/plugin.d.ts +1 -1
  63. package/dist/helper/hot-update/plugin.js +14 -13
  64. package/dist/helper/index.d.ts +2 -1
  65. package/dist/helper/index.js +2 -1
  66. package/dist/helper/is.d.ts +2 -0
  67. package/dist/helper/is.js +15 -0
  68. package/dist/helper/legacy/LogTestPlugin.d.ts +6 -0
  69. package/dist/helper/legacy/LogTestPlugin.js +35 -0
  70. package/dist/helper/legacy/asModule.js +0 -2
  71. package/dist/helper/legacy/checkArrayExpectation.d.ts +1 -1
  72. package/dist/helper/legacy/checkArrayExpectation.js +8 -8
  73. package/dist/helper/legacy/createFakeWorker.d.ts +2 -2
  74. package/dist/helper/legacy/createFakeWorker.js +44 -20
  75. package/dist/helper/legacy/createLazyTestEnv.d.ts +3 -2
  76. package/dist/helper/legacy/createLazyTestEnv.js +3 -3
  77. package/dist/helper/legacy/deprecationTracking.d.ts +1 -0
  78. package/dist/helper/legacy/deprecationTracking.js +41 -0
  79. package/dist/helper/legacy/expectSource.d.ts +10 -0
  80. package/dist/helper/legacy/expectSource.js +23 -0
  81. package/dist/helper/legacy/findOutputFiles.d.ts +6 -0
  82. package/dist/helper/legacy/findOutputFiles.js +14 -0
  83. package/dist/helper/legacy/regexEscape.d.ts +2 -0
  84. package/dist/helper/legacy/regexEscape.js +5 -0
  85. package/dist/helper/legacy/supportDefaultAssignment.js +13 -0
  86. package/dist/helper/legacy/supportsTextDecoder.d.ts +2 -0
  87. package/dist/helper/legacy/supportsTextDecoder.js +9 -0
  88. package/dist/helper/legacy/supportsUsing.d.ts +2 -0
  89. package/dist/helper/legacy/supportsUsing.js +26 -0
  90. package/dist/helper/legacy/urlToRelativePath.js +5 -2
  91. package/dist/helper/read-config-file.d.ts +3 -2
  92. package/dist/helper/read-config-file.js +12 -2
  93. package/dist/helper/setup-env.js +26 -4
  94. package/dist/helper/setup-expect.js +2 -2
  95. package/dist/helper/setup-wasm.js +2 -2
  96. package/dist/helper/stringify-config.d.ts +2 -0
  97. package/dist/helper/stringify-config.js +43 -0
  98. package/dist/helper/util/checkStats.d.ts +0 -1
  99. package/dist/helper/util/checkStats.js +0 -18
  100. package/dist/helper/util/expectWarningFactory.js +2 -4
  101. package/dist/index.d.ts +0 -4
  102. package/dist/index.js +1 -4
  103. package/dist/plugin/index.d.ts +0 -3
  104. package/dist/plugin/index.js +0 -3
  105. package/dist/plugin/lazy-compilation-test-plugin.js +4 -4
  106. package/dist/runner/index.d.ts +2 -9
  107. package/dist/runner/index.js +2 -9
  108. package/dist/runner/node/index.d.ts +41 -0
  109. package/dist/runner/node/index.js +450 -0
  110. package/dist/runner/web/index.d.ts +30 -0
  111. package/dist/runner/web/index.js +343 -0
  112. package/dist/test/context.d.ts +15 -18
  113. package/dist/test/context.js +52 -57
  114. package/dist/test/creator.d.ts +20 -19
  115. package/dist/test/creator.js +69 -57
  116. package/dist/test/debug.d.ts +15 -0
  117. package/dist/test/debug.js +205 -0
  118. package/dist/test/tester.d.ts +1 -0
  119. package/dist/test/tester.js +44 -6
  120. package/dist/type.d.ts +94 -64
  121. package/dist/type.js +8 -12
  122. package/package.json +22 -61
  123. package/rstest.d.ts +24 -0
  124. package/dist/case/diff.d.ts +0 -3
  125. package/dist/case/diff.js +0 -126
  126. package/dist/compare/comparator.d.ts +0 -17
  127. package/dist/compare/comparator.js +0 -52
  128. package/dist/compare/compare.d.ts +0 -17
  129. package/dist/compare/compare.js +0 -175
  130. package/dist/compare/format-code.d.ts +0 -16
  131. package/dist/compare/format-code.js +0 -239
  132. package/dist/compare/index.d.ts +0 -5
  133. package/dist/compare/index.js +0 -21
  134. package/dist/compare/replace-module-argument.d.ts +0 -1
  135. package/dist/compare/replace-module-argument.js +0 -8
  136. package/dist/compare/replace-runtime-module-name.d.ts +0 -1
  137. package/dist/compare/replace-runtime-module-name.js +0 -71
  138. package/dist/helper/legacy/FakeDocument.d.ts +0 -54
  139. package/dist/helper/legacy/FakeDocument.js +0 -278
  140. package/dist/helper/legacy/createLogger.d.ts +0 -23
  141. package/dist/helper/legacy/createLogger.js +0 -20
  142. package/dist/helper/legacy/supportsImportFn.d.ts +0 -1
  143. package/dist/helper/legacy/supportsImportFn.js +0 -15
  144. package/dist/helper/legacy/supportsWorker.js +0 -17
  145. package/dist/helper/legacy/walkCssTokens.d.ts +0 -40
  146. package/dist/helper/legacy/walkCssTokens.js +0 -761
  147. package/dist/helper/legacy/warmup-webpack.d.ts +0 -1
  148. package/dist/helper/legacy/warmup-webpack.js +0 -26
  149. package/dist/helper/loaders/hot-update.d.ts +0 -1
  150. package/dist/helper/loaders/hot-update.js +0 -40
  151. package/dist/helper/plugins/hot-update.d.ts +0 -7
  152. package/dist/helper/plugins/hot-update.js +0 -40
  153. package/dist/helper/plugins/index.d.ts +0 -1
  154. package/dist/helper/plugins/index.js +0 -17
  155. package/dist/helper/util/currentWatchStep.d.ts +0 -1
  156. package/dist/helper/util/currentWatchStep.js +0 -2
  157. package/dist/helper/util/identifier.d.ts +0 -76
  158. package/dist/helper/util/identifier.js +0 -339
  159. package/dist/helper/util/replaceMitteDiagnostic.d.ts +0 -2
  160. package/dist/helper/util/replaceMitteDiagnostic.js +0 -17
  161. package/dist/plugin/rspack-diff-config-plugin.d.ts +0 -7
  162. package/dist/plugin/rspack-diff-config-plugin.js +0 -42
  163. package/dist/plugin/webpack-diff-config-plugin.d.ts +0 -7
  164. package/dist/plugin/webpack-diff-config-plugin.js +0 -41
  165. package/dist/plugin/webpack-module-placeholder-plugin.d.ts +0 -3
  166. package/dist/plugin/webpack-module-placeholder-plugin.js +0 -106
  167. package/dist/processor/basic.d.ts +0 -24
  168. package/dist/processor/basic.js +0 -146
  169. package/dist/processor/builtin.d.ts +0 -9
  170. package/dist/processor/builtin.js +0 -171
  171. package/dist/processor/cache.d.ts +0 -20
  172. package/dist/processor/cache.js +0 -128
  173. package/dist/processor/config.d.ts +0 -11
  174. package/dist/processor/config.js +0 -81
  175. package/dist/processor/defaults.d.ts +0 -30
  176. package/dist/processor/defaults.js +0 -72
  177. package/dist/processor/diagnostic.d.ts +0 -15
  178. package/dist/processor/diagnostic.js +0 -102
  179. package/dist/processor/diff.d.ts +0 -30
  180. package/dist/processor/diff.js +0 -140
  181. package/dist/processor/error.d.ts +0 -23
  182. package/dist/processor/error.js +0 -95
  183. package/dist/processor/hash.d.ts +0 -10
  184. package/dist/processor/hash.js +0 -63
  185. package/dist/processor/hook.d.ts +0 -44
  186. package/dist/processor/hook.js +0 -204
  187. package/dist/processor/hot-incremental.d.ts +0 -14
  188. package/dist/processor/hot-incremental.js +0 -43
  189. package/dist/processor/hot-step.d.ts +0 -18
  190. package/dist/processor/hot-step.js +0 -304
  191. package/dist/processor/hot.d.ts +0 -17
  192. package/dist/processor/hot.js +0 -140
  193. package/dist/processor/index.d.ts +0 -20
  194. package/dist/processor/index.js +0 -36
  195. package/dist/processor/multi.d.ts +0 -17
  196. package/dist/processor/multi.js +0 -73
  197. package/dist/processor/normal.d.ts +0 -12
  198. package/dist/processor/normal.js +0 -150
  199. package/dist/processor/simple.d.ts +0 -23
  200. package/dist/processor/simple.js +0 -49
  201. package/dist/processor/snapshot.d.ts +0 -12
  202. package/dist/processor/snapshot.js +0 -66
  203. package/dist/processor/stats-api.d.ts +0 -18
  204. package/dist/processor/stats-api.js +0 -48
  205. package/dist/processor/stats.d.ts +0 -18
  206. package/dist/processor/stats.js +0 -206
  207. package/dist/processor/treeshaking.d.ts +0 -10
  208. package/dist/processor/treeshaking.js +0 -33
  209. package/dist/processor/watch.d.ts +0 -30
  210. package/dist/processor/watch.js +0 -241
  211. package/dist/reporter/diff-html.d.ts +0 -15
  212. package/dist/reporter/diff-html.js +0 -69
  213. package/dist/reporter/diff-stats.d.ts +0 -24
  214. package/dist/reporter/diff-stats.js +0 -131
  215. package/dist/reporter/index.d.ts +0 -2
  216. package/dist/reporter/index.js +0 -18
  217. package/dist/runner/basic.d.ts +0 -10
  218. package/dist/runner/basic.js +0 -63
  219. package/dist/runner/cache.d.ts +0 -5
  220. package/dist/runner/cache.js +0 -90
  221. package/dist/runner/hot-step.d.ts +0 -5
  222. package/dist/runner/hot-step.js +0 -87
  223. package/dist/runner/hot.d.ts +0 -5
  224. package/dist/runner/hot.js +0 -83
  225. package/dist/runner/multiple.d.ts +0 -11
  226. package/dist/runner/multiple.js +0 -38
  227. package/dist/runner/normal.d.ts +0 -5
  228. package/dist/runner/normal.js +0 -20
  229. package/dist/runner/runner/basic.d.ts +0 -30
  230. package/dist/runner/runner/basic.js +0 -107
  231. package/dist/runner/runner/cjs.d.ts +0 -14
  232. package/dist/runner/runner/cjs.js +0 -151
  233. package/dist/runner/runner/esm.d.ts +0 -7
  234. package/dist/runner/runner/esm.js +0 -125
  235. package/dist/runner/runner/index.d.ts +0 -8
  236. package/dist/runner/runner/index.js +0 -24
  237. package/dist/runner/runner/normal.d.ts +0 -9
  238. package/dist/runner/runner/normal.js +0 -23
  239. package/dist/runner/runner/watch.d.ts +0 -16
  240. package/dist/runner/runner/watch.js +0 -27
  241. package/dist/runner/runner/web/fake.d.ts +0 -18
  242. package/dist/runner/runner/web/fake.js +0 -143
  243. package/dist/runner/runner/web/jsdom.d.ts +0 -26
  244. package/dist/runner/runner/web/jsdom.js +0 -219
  245. package/dist/runner/runner/web.d.ts +0 -15
  246. package/dist/runner/runner/web.js +0 -32
  247. package/dist/runner/type.d.ts +0 -42
  248. package/dist/runner/type.js +0 -9
  249. package/dist/runner/watch.d.ts +0 -7
  250. package/dist/runner/watch.js +0 -60
  251. package/dist/test/simple.d.ts +0 -5
  252. package/dist/test/simple.js +0 -43
  253. package/jest.d.ts +0 -25
  254. package/template/diff.bundle.css +0 -1
  255. package/template/diff.bundle.js +0 -144
  256. package/template/diff.html +0 -14
  257. package/template/editor.worker.js +0 -1
  258. /package/dist/case/{nativeWatcher.d.ts → native-watcher.d.ts} +0 -0
  259. /package/dist/helper/legacy/{supportsWorker.d.ts → supportDefaultAssignment.d.ts} +0 -0
@@ -0,0 +1,343 @@
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.createLocatedError = exports.WebRunner = void 0;
7
+ const node_fs_1 = __importDefault(require("node:fs"));
8
+ const node_path_1 = __importDefault(require("node:path"));
9
+ const node_url_1 = require("node:url");
10
+ const node_vm_1 = require("node:vm");
11
+ const jsdom_1 = require("jsdom");
12
+ const helper_1 = require("../../helper");
13
+ const EventSourceForNode_1 = __importDefault(require("../../helper/legacy/EventSourceForNode"));
14
+ const urlToRelativePath_1 = __importDefault(require("../../helper/legacy/urlToRelativePath"));
15
+ const node_1 = require("../node");
16
+ // Compatibility code to suppress iconv-lite warnings
17
+ require("iconv-lite").skipDecodeWarning = true;
18
+ const FAKE_HOSTS = [
19
+ "https://example.com/public/path",
20
+ "https://example.com",
21
+ "https://test.cases/path/",
22
+ "https://test.cases/server/",
23
+ "https://test.cases"
24
+ ];
25
+ const FAKE_TEST_ROOT_HOST = "https://test.cases/root/";
26
+ class WebRunner extends node_1.NodeRunner {
27
+ constructor(_webOptions) {
28
+ super(_webOptions);
29
+ this._webOptions = _webOptions;
30
+ const virtualConsole = new jsdom_1.VirtualConsole({});
31
+ virtualConsole.sendTo(console, {
32
+ omitJSDOMErrors: true
33
+ });
34
+ this.dom = new jsdom_1.JSDOM(`
35
+ <!doctype html>
36
+ <html>
37
+ <head></head>
38
+ <body></body>
39
+ </html>
40
+ `, {
41
+ url: this._webOptions.location,
42
+ resources: this.createResourceLoader(),
43
+ runScripts: "dangerously",
44
+ virtualConsole
45
+ });
46
+ this.dom.window.console = console;
47
+ // compat with FakeDocument
48
+ this.dom.window.eval(`
49
+ Object.defineProperty(document.head, "_children", {
50
+ get: function() {
51
+ return Array.from(document.head.children).map(function(ele) {
52
+ var type = ele.tagName.toLowerCase();
53
+ return new Proxy(ele, {
54
+ get(target, prop, receiver) {
55
+ if (prop === "_type") {
56
+ return target.tagName.toLowerCase();
57
+ }
58
+ if (prop === "_href") {
59
+ return Reflect.get(target, "href", receiver);
60
+ }
61
+ return Reflect.get(target, prop, receiver);
62
+ },
63
+ });
64
+ });
65
+ }
66
+ });
67
+ `);
68
+ if (this._options.compilerOptions.node !== false) {
69
+ const vmContext = this.dom.getInternalVMContext();
70
+ vmContext.global = {};
71
+ }
72
+ }
73
+ run(file) {
74
+ if (!file.endsWith(".js") && !file.endsWith(".mjs")) {
75
+ this.log(`css: ${file}`);
76
+ const cssElement = this.dom.window.document.createElement("link");
77
+ cssElement.href = file;
78
+ cssElement.rel = "stylesheet";
79
+ this.dom.window.document.head.appendChild(cssElement);
80
+ return Promise.resolve();
81
+ }
82
+ return super.run(file);
83
+ }
84
+ getGlobal(name) {
85
+ return this.globalContext[name];
86
+ }
87
+ log(message) {
88
+ this._options.logs?.push(`[WebRunner] ${message}`);
89
+ }
90
+ createResourceLoader() {
91
+ const that = this;
92
+ class CustomResourceLoader extends jsdom_1.ResourceLoader {
93
+ fetch(url, options) {
94
+ if (that._options.testConfig.resourceLoader) {
95
+ that.log(`resource custom loader: start ${url}`);
96
+ const content = that._options.testConfig.resourceLoader(url, options.element);
97
+ if (content !== undefined) {
98
+ that.log(`resource custom loader: accepted`);
99
+ return Promise.resolve(content);
100
+ }
101
+ else {
102
+ that.log(`resource custom loader: not found`);
103
+ }
104
+ }
105
+ const filePath = that.urlToPath(url);
106
+ that.log(`resource loader: ${url} -> ${filePath}`);
107
+ let finalCode;
108
+ if (node_path_1.default.extname(filePath) === ".js") {
109
+ const currentDirectory = node_path_1.default.dirname(filePath);
110
+ const file = that.getFile(filePath, currentDirectory);
111
+ if (!file) {
112
+ throw new Error(`File not found: ${filePath}`);
113
+ }
114
+ const [_m, code] = that.getModuleContent(file);
115
+ finalCode = code;
116
+ }
117
+ else {
118
+ finalCode = node_fs_1.default.readFileSync(filePath);
119
+ }
120
+ try {
121
+ that.dom.window["__LINK_SHEET__"] ??= {};
122
+ that.dom.window["__LINK_SHEET__"][url] = finalCode.toString();
123
+ return Promise.resolve(finalCode);
124
+ }
125
+ catch (err) {
126
+ console.error(err);
127
+ if (err.code === "ENOENT") {
128
+ return null;
129
+ }
130
+ throw err;
131
+ }
132
+ }
133
+ }
134
+ return new CustomResourceLoader();
135
+ }
136
+ urlToPath(url) {
137
+ if (url.startsWith("file://")) {
138
+ return (0, node_url_1.fileURLToPath)(url);
139
+ }
140
+ if (url.startsWith(FAKE_TEST_ROOT_HOST)) {
141
+ return node_path_1.default.resolve(__TEST_PATH__, `./${url.slice(FAKE_TEST_ROOT_HOST.length)}`);
142
+ }
143
+ let dist = url;
144
+ for (const host of FAKE_HOSTS) {
145
+ if (url.startsWith(host)) {
146
+ dist = url.slice(host.length);
147
+ break;
148
+ }
149
+ }
150
+ return node_path_1.default.resolve(this._webOptions.dist, `./${dist}`).split("?")[0];
151
+ }
152
+ createBaseModuleScope() {
153
+ const moduleScope = super.createBaseModuleScope();
154
+ moduleScope.EventSource = EventSourceForNode_1.default;
155
+ moduleScope.fetch = async (url) => {
156
+ try {
157
+ const filePath = this.urlToPath(url);
158
+ this.log(`fetch: ${url} -> ${filePath}`);
159
+ const buffer = await new Promise((resolve, reject) => node_fs_1.default.readFile(filePath, (err, b) => (err ? reject(err) : resolve(b))));
160
+ return {
161
+ status: 200,
162
+ ok: true,
163
+ json: async () => JSON.parse(buffer.toString("utf-8"))
164
+ };
165
+ }
166
+ catch (err) {
167
+ if (err.code === "ENOENT") {
168
+ return {
169
+ status: 404,
170
+ ok: false
171
+ };
172
+ }
173
+ throw err;
174
+ }
175
+ };
176
+ moduleScope.URL = URL;
177
+ moduleScope.importScripts = (url) => {
178
+ const path = (0, urlToRelativePath_1.default)(url);
179
+ this.log(`importScripts: ${url} -> ${path}`);
180
+ this.requirers.get("entry")(this._options.dist, path);
181
+ };
182
+ moduleScope.getComputedStyle = (element) => {
183
+ const computedStyle = this.dom.window.getComputedStyle(element);
184
+ const getPropertyValue = computedStyle.getPropertyValue.bind(computedStyle);
185
+ return {
186
+ ...computedStyle,
187
+ getPropertyValue(v) {
188
+ return getPropertyValue(v);
189
+ }
190
+ };
191
+ };
192
+ moduleScope.window = this.dom.window;
193
+ moduleScope.document = this.dom.window.document;
194
+ moduleScope.getLinkSheet = (link) => {
195
+ return this.dom.window["__LINK_SHEET__"][link.href];
196
+ };
197
+ return moduleScope;
198
+ }
199
+ getModuleContent(file) {
200
+ const m = {
201
+ exports: {}
202
+ };
203
+ const currentModuleScope = this.createModuleScope(this.getRequire(), m, file);
204
+ if (this._options.testConfig.moduleScope) {
205
+ this._options.testConfig.moduleScope(currentModuleScope, this._options.stats, this._options.compilerOptions);
206
+ }
207
+ if (file.content.includes("__STATS__")) {
208
+ currentModuleScope.__STATS__ = this._options.stats?.();
209
+ }
210
+ if (file.content.includes("__STATS_I__")) {
211
+ const statsIndex = this._options.stats?.()?.__index__;
212
+ if (typeof statsIndex === "number") {
213
+ currentModuleScope.__STATS_I__ = statsIndex;
214
+ }
215
+ }
216
+ const proxyCode = `// hijack document.currentScript for auto public path
217
+ var $$g$$ = new Proxy(window, {
218
+ get(target, prop, receiver) {
219
+ if (prop === "document") {
220
+ return new Proxy(window.document, {
221
+ get(target, prop, receiver) {
222
+ if (prop === "currentScript") {
223
+ var script = target.createElement("script");
224
+ script.src = "https://test.cases/path/${(0, helper_1.escapeSep)(file.subPath)}index.js";
225
+ return script;
226
+ }
227
+ return Reflect.get(target, prop, receiver);
228
+ }
229
+ });
230
+ }
231
+ return Reflect.get(target, prop, receiver);
232
+ },
233
+ });
234
+ var $$self$$ = new Proxy(window, {
235
+ get(target, prop, receiver) {
236
+ if (prop === "__HMR_UPDATED_RUNTIME__") {
237
+ return window["__GLOBAL_SHARED__"]["__HMR_UPDATED_RUNTIME__"];
238
+ }
239
+ return Reflect.get(target, prop, receiver);
240
+ },
241
+ set(target, prop, value, receiver) {
242
+ if (prop === "__HMR_UPDATED_RUNTIME__") {
243
+ window["__GLOBAL_SHARED__"]["__HMR_UPDATED_RUNTIME__"] = value;
244
+ }
245
+ return Reflect.set(target, prop, value, receiver);
246
+ }
247
+ });`;
248
+ const proxyLines = proxyCode.split("\n");
249
+ const locatedError = (0, exports.createLocatedError)(this._options.errors || [], proxyLines.length + 1);
250
+ const originIt = currentModuleScope.it;
251
+ currentModuleScope.it = (description, fn) => {
252
+ return originIt(description, async (...args) => {
253
+ try {
254
+ return await fn(...args);
255
+ }
256
+ catch (e) {
257
+ throw locatedError(e, file);
258
+ }
259
+ });
260
+ };
261
+ const scopeKey = (0, helper_1.escapeSep)(file.path);
262
+ const args = Object.keys(currentModuleScope).filter(arg => !["window", "self", "globalThis", "console"].includes(arg));
263
+ const argValues = args
264
+ .map(arg => `window["${scopeKey}"]["${arg}"]`)
265
+ .join(", ");
266
+ this.dom.window[scopeKey] = currentModuleScope;
267
+ this.dom.window["__GLOBAL_SHARED__"] = this.globalContext;
268
+ this.dom.window["__LOCATED_ERROR__"] = locatedError;
269
+ this.dom.window["__FILE__"] = file;
270
+ return [
271
+ m,
272
+ `${proxyCode}
273
+ (function(window, self, globalThis, console, ${args.join(", ")}) { try {
274
+ ${file.content}
275
+ } catch (e) {
276
+ throw __LOCATED_ERROR__(e, window["__FILE__"]);
277
+ }})($$g$$, $$self$$, $$g$$, window["console"], ${argValues});`,
278
+ proxyLines.length + 1
279
+ ];
280
+ }
281
+ createJSDOMRequirer() {
282
+ return (currentDirectory, modulePath, context = {}) => {
283
+ const file = context.file || this.getFile(modulePath, currentDirectory);
284
+ this.log(`jsdom: ${modulePath} -> ${file?.path}`);
285
+ if (!file) {
286
+ return this.requirers.get("miss")(currentDirectory, modulePath);
287
+ }
288
+ if (file.path in this.requireCache) {
289
+ return this.requireCache[file.path].exports;
290
+ }
291
+ const [m, code, lineOffset] = this.getModuleContent(file);
292
+ this.preExecute(code, file);
293
+ try {
294
+ const script = new node_vm_1.Script(code);
295
+ const vmContext = this.dom.getInternalVMContext();
296
+ script.runInContext(vmContext, {
297
+ filename: file.path,
298
+ lineOffset: -lineOffset
299
+ });
300
+ }
301
+ catch (e) {
302
+ const error = new Error(`Parse script '${file.path}' failed:\n${e.message}`);
303
+ error.stack = `${error.message}\n${e.stack}`;
304
+ this._options.errors?.push(error);
305
+ throw error;
306
+ }
307
+ this.postExecute(m, file);
308
+ this.requireCache[file.path] = m;
309
+ return m.exports;
310
+ };
311
+ }
312
+ createRunner() {
313
+ super.createRunner();
314
+ this.requirers.set("cjs", this.createJSDOMRequirer());
315
+ }
316
+ }
317
+ exports.WebRunner = WebRunner;
318
+ const createLocatedError = (collectedErrors, offset) => {
319
+ return (e, file) => {
320
+ const match = (e.stack || e.message).match(/<anonymous>:(\d+)/);
321
+ if (match) {
322
+ const [, line] = match;
323
+ const realLine = Number(line) - offset;
324
+ const codeLines = file.content.split("\n");
325
+ const lineContents = [
326
+ ...codeLines
327
+ .slice(Math.max(0, realLine - 3), Math.max(0, realLine - 1))
328
+ .map(line => `│ ${line}`),
329
+ `│> ${codeLines[realLine - 1]}`,
330
+ ...codeLines.slice(realLine, realLine + 2).map(line => `│ ${line}`)
331
+ ];
332
+ const message = `Error in JSDOM when running file '${file.path}' at line ${realLine}: ${e.message}\n${lineContents.join("\n")}`;
333
+ const finalError = new Error(message);
334
+ finalError.stack = `${message}\n${e.stack}`;
335
+ collectedErrors.push(finalError);
336
+ return finalError;
337
+ }
338
+ else {
339
+ return e;
340
+ }
341
+ };
342
+ };
343
+ exports.createLocatedError = createLocatedError;
@@ -1,26 +1,23 @@
1
- import type { ECompilerType, ITestCompilerManager, ITestContext, ITesterConfig, ITestRunner, TRunnerFactory, TTestConfig } from "../type";
2
- export type TTestContextOptions = Omit<ITesterConfig, "name" | "steps">;
1
+ import type { ITestCompilerManager, ITestContext, ITestEnv, ITesterConfig, ITestRunner, TTestConfig } from "../type";
2
+ export type TTestContextOptions = Omit<ITesterConfig, "steps">;
3
3
  export declare class TestContext implements ITestContext {
4
4
  private config;
5
- protected errors: Map<string, Error[]>;
6
- protected compilers: Map<string, ITestCompilerManager<ECompilerType>>;
7
- protected store: Map<string, Record<string, unknown>>;
5
+ protected errors: Error[];
6
+ protected compiler: ITestCompilerManager | null;
7
+ protected store: Map<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
- getCompiler<T extends ECompilerType>(name: string, type: T | void): ITestCompilerManager<T>;
15
- getRunnerFactory<T extends ECompilerType>(name: string): TRunnerFactory<T> | null;
16
- getRunner(key: string): ITestRunner | null;
17
- setRunner(key: string, runner: ITestRunner): void;
18
- getTestConfig<T extends ECompilerType>(): TTestConfig<T>;
19
- setValue<T>(name: string, key: string, value: T): void;
20
- getValue<T>(name: string, key: string): T | void;
21
- hasError(name?: string): boolean;
22
- emitError(name: string, err: Error | string): void;
23
- getNames(): string[];
24
- getError(name?: string): Error[];
25
- clearError(name?: string): void;
13
+ getCompiler(): ITestCompilerManager;
14
+ getRunner(file: string, env: ITestEnv): ITestRunner;
15
+ getTestConfig(): TTestConfig;
16
+ setValue<T>(key: string, value: T): void;
17
+ getValue<T>(key: string): T | void;
18
+ hasError(): boolean;
19
+ emitError(err: Error | string): void;
20
+ getError(): Error[];
21
+ clearError(): void;
22
+ closeCompiler(): Promise<void>;
26
23
  }
@@ -6,14 +6,14 @@ 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 debug_1 = require("./debug");
9
10
  class TestContext {
10
11
  constructor(config) {
11
12
  this.config = config;
12
- this.errors = new Map();
13
- this.compilers = new Map();
13
+ this.errors = [];
14
+ this.compiler = null;
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) {
@@ -35,73 +35,68 @@ class TestContext {
35
35
  }
36
36
  return this.config.temp;
37
37
  }
38
- getCompiler(name, type) {
39
- let compiler = this.compilers.get(name);
40
- if (!compiler) {
41
- if (!type) {
42
- throw new Error("Compiler does not exists");
43
- }
44
- compiler = new compiler_1.TestCompilerManager(type, this.config.compilerFactories);
45
- this.compilers.set(name, compiler);
38
+ getCompiler() {
39
+ if (!this.compiler) {
40
+ this.compiler = new compiler_1.TestCompilerManager(this);
46
41
  }
47
- return compiler;
42
+ return this.compiler;
48
43
  }
49
- getRunnerFactory(name) {
50
- if (!this.runnerFactory &&
51
- typeof this.config.runnerFactory === "function") {
52
- this.runnerFactory = new this.config.runnerFactory(name, this);
44
+ getRunner(file, env) {
45
+ if (!this.config.runnerCreator) {
46
+ throw new Error("TestContext: Runner creator not found");
53
47
  }
54
- return this.runnerFactory;
55
- }
56
- getRunner(key) {
57
- return this.runners.get(key) || null;
58
- }
59
- setRunner(key, runner) {
60
- this.runners.set(key, runner);
48
+ const runnerKey = this.config.runnerCreator.key(this, this.config.name, file);
49
+ let runner = this.runners.get(runnerKey);
50
+ if (runner) {
51
+ if (__DEBUG__) {
52
+ const getRunnerInfo = this.getValue(debug_1.DEBUG_SCOPES.RunGetRunner) || {};
53
+ getRunnerInfo[file] = {
54
+ runnerKey,
55
+ reused: true,
56
+ runnerType: runner.constructor.name
57
+ };
58
+ this.setValue(debug_1.DEBUG_SCOPES.RunGetRunner, getRunnerInfo);
59
+ }
60
+ return runner;
61
+ }
62
+ runner = this.config.runnerCreator.runner(this, this.config.name, file, env);
63
+ runner.__key__ = runnerKey;
64
+ if (__DEBUG__) {
65
+ const getRunnerInfo = this.getValue(debug_1.DEBUG_SCOPES.RunGetRunner) || {};
66
+ getRunnerInfo[file] = {
67
+ runnerKey,
68
+ reused: false,
69
+ runnerType: runner.constructor.name
70
+ };
71
+ this.setValue(debug_1.DEBUG_SCOPES.RunGetRunner, getRunnerInfo);
72
+ }
73
+ this.runners.set(runnerKey, runner);
74
+ return runner;
61
75
  }
62
76
  getTestConfig() {
63
77
  return this.config.testConfig || {};
64
78
  }
65
- setValue(name, key, value) {
66
- if (!this.store.has(name)) {
67
- this.store.set(name, {});
68
- }
69
- const scope = this.store.get(name);
70
- scope[key] = value;
79
+ setValue(key, value) {
80
+ this.store.set(key, value);
71
81
  }
72
- getValue(name, key) {
73
- if (!this.store.has(name)) {
74
- this.store.set(name, {});
75
- }
76
- const scope = this.store.get(name);
77
- return scope[key];
82
+ getValue(key) {
83
+ return this.store.get(key);
78
84
  }
79
- hasError(name) {
80
- if (name) {
81
- return this.getError(name).length > 0;
82
- }
83
- return !!Array.from(this.errors.values()).reduce((res, arr) => res + arr.length, 0);
85
+ hasError() {
86
+ return this.errors.length > 0;
84
87
  }
85
- emitError(name, err) {
86
- const errors = this.errors.get(name) || [];
87
- errors.push(typeof err === "string" ? new Error(err) : err);
88
- this.errors.set(name, errors);
88
+ emitError(err) {
89
+ this.errors.push(typeof err === "string" ? new Error(err) : err);
89
90
  }
90
- getNames() {
91
- return Array.from(this.compilers.keys());
91
+ getError() {
92
+ return this.errors;
92
93
  }
93
- getError(name) {
94
- if (name) {
95
- return this.errors.get(name) || [];
96
- }
97
- return Array.prototype.concat(...this.errors.values());
94
+ clearError() {
95
+ this.errors.length = 0;
98
96
  }
99
- clearError(name) {
100
- if (name) {
101
- this.errors.delete(name);
102
- }
103
- else {
104
- this.errors.clear();
97
+ async closeCompiler() {
98
+ if (this.compiler) {
99
+ await this.compiler.close();
105
100
  }
106
101
  }
107
102
  }
@@ -1,4 +1,4 @@
1
- import type { ECompilerType, ITestContext, ITestEnv, ITester, ITestProcessor, TRunnerFactory, TTestConfig } from "../type";
1
+ import type { ITestContext, ITestEnv, ITester, ITesterConfig, ITestProcessor, TTestConfig, TTestRunnerCreator } from "../type";
2
2
  declare global {
3
3
  var testFilter: string | undefined;
4
4
  }
@@ -6,41 +6,42 @@ interface IConcurrentTestEnv {
6
6
  clear: () => void;
7
7
  run: () => Promise<void>;
8
8
  }
9
- export interface IBasicCaseCreatorOptions<T extends ECompilerType> {
9
+ export interface IBasicCaseCreatorOptions {
10
10
  clean?: boolean;
11
11
  describe?: boolean;
12
12
  timeout?: number;
13
13
  contextValue?: Record<string, unknown>;
14
- steps: (creatorConfig: IBasicCaseCreatorOptions<T> & {
14
+ steps: (creatorConfig: IBasicCaseCreatorOptions & {
15
15
  name: string;
16
16
  src: string;
17
17
  dist: string;
18
18
  temp: string | void;
19
19
  }) => ITestProcessor[];
20
- testConfig?: (testConfig: TTestConfig<T>) => void;
20
+ testConfig?: (testConfig: TTestConfig) => void;
21
21
  description?: (name: string, step: number) => string;
22
- runner?: new (name: string, context: ITestContext) => TRunnerFactory<ECompilerType>;
23
- [key: string]: unknown;
22
+ runner?: TTestRunnerCreator;
23
+ createContext?: (config: ITesterConfig) => ITestContext;
24
24
  concurrent?: boolean | number;
25
+ [key: string]: unknown;
25
26
  }
26
- export declare class BasicCaseCreator<T extends ECompilerType> {
27
- protected _options: IBasicCaseCreatorOptions<T>;
27
+ export declare class BasicCaseCreator {
28
+ protected _options: IBasicCaseCreatorOptions;
28
29
  protected currentConcurrent: number;
29
30
  protected tasks: [string, () => void][];
30
- constructor(_options: IBasicCaseCreatorOptions<T>);
31
- create(name: string, src: string, dist: string, temp?: string): ITester | undefined;
31
+ constructor(_options: IBasicCaseCreatorOptions);
32
+ create(name: string, src: string, dist: string, temp?: string, caseOptions?: Partial<IBasicCaseCreatorOptions>): ITester | undefined;
32
33
  protected shouldRun(name: string): boolean;
33
- protected describeConcurrent(name: string, tester: ITester, testConfig: TTestConfig<T>): void;
34
- protected describe(name: string, tester: ITester, testConfig: TTestConfig<T>): void;
34
+ protected describeConcurrent(name: string, tester: ITester, testConfig: TTestConfig, options: IBasicCaseCreatorOptions): void;
35
+ protected describe(name: string, tester: ITester, testConfig: TTestConfig, options: IBasicCaseCreatorOptions): void;
35
36
  protected createConcurrentEnv(): ITestEnv & IConcurrentTestEnv;
36
- protected createEnv(testConfig: TTestConfig<T>): ITestEnv;
37
+ protected createEnv(testConfig: TTestConfig, options: IBasicCaseCreatorOptions): ITestEnv;
37
38
  protected clean(folders: string[]): void;
38
39
  protected skip(name: string, reason: string | boolean): void;
39
- protected readTestConfig(src: string): TTestConfig<T>;
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;
42
- protected tryRunTask(): void;
43
- protected getMaxConcurrent(): number;
44
- protected registerConcurrentTask(name: string, starter: () => void): () => void;
40
+ protected readTestConfig(src: string): TTestConfig;
41
+ protected checkSkipped(src: string, testConfig: TTestConfig, options: IBasicCaseCreatorOptions): boolean | string;
42
+ protected createTester(name: string, src: string, dist: string, temp: string | undefined, testConfig: TTestConfig, options: IBasicCaseCreatorOptions): ITester;
43
+ protected tryRunTask(concurrent?: number): void;
44
+ protected getMaxConcurrent(concurrent?: number): number;
45
+ protected registerConcurrentTask(name: string, starter: () => void, concurrent?: number): () => void;
45
46
  }
46
47
  export {};