@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
package/dist/case/hash.js CHANGED
@@ -1,15 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createHashCase = createHashCase;
4
- const processor_1 = require("../processor");
5
4
  const creator_1 = require("../test/creator");
6
- const type_1 = require("../type");
5
+ const common_1 = require("./common");
6
+ const REG_ERROR_CASE = /error$/;
7
7
  class HashCaseCreator extends creator_1.BasicCaseCreator {
8
- describe(name, tester, testConfig) {
8
+ describe(name, tester, testConfig, options) {
9
9
  it(`should print correct hash for ${name}`, async () => {
10
10
  await tester.prepare();
11
11
  await tester.compile();
12
- await tester.check(this.createEnv(testConfig));
12
+ await tester.check(this.createEnv(testConfig, options));
13
13
  await tester.resume();
14
14
  }, 30000);
15
15
  }
@@ -18,13 +18,76 @@ const creator = new HashCaseCreator({
18
18
  clean: true,
19
19
  describe: false,
20
20
  steps: ({ name }) => [
21
- new processor_1.HashProcessor({
22
- name,
23
- compilerType: type_1.ECompilerType.Rspack,
24
- configFiles: ["rspack.config.js", "webpack.config.js"]
25
- })
21
+ {
22
+ config: async (context) => {
23
+ (0, common_1.configMultiCompiler)(context, name, ["rspack.config.js", "webpack.config.js"], defaultOptions, overrideOptions);
24
+ },
25
+ compiler: async (context) => {
26
+ await (0, common_1.compiler)(context, name);
27
+ },
28
+ build: async (context) => {
29
+ await (0, common_1.build)(context, name);
30
+ },
31
+ run: async (env, context) => {
32
+ // no need to run, just check snapshot
33
+ },
34
+ check: async (env, context) => {
35
+ await check(env, context, name);
36
+ }
37
+ }
26
38
  ]
27
39
  });
28
40
  function createHashCase(name, src, dist) {
29
41
  creator.create(name, src, dist);
30
42
  }
43
+ function defaultOptions(index, context) {
44
+ return {
45
+ context: context.getSource(),
46
+ output: {
47
+ path: context.getDist()
48
+ },
49
+ experiments: {
50
+ css: true,
51
+ rspackFuture: {
52
+ bundlerInfo: {
53
+ force: false
54
+ }
55
+ },
56
+ inlineConst: true
57
+ }
58
+ };
59
+ }
60
+ function overrideOptions(index, context, options) {
61
+ if (!options.entry) {
62
+ options.entry = "./index.js";
63
+ }
64
+ if (!global.printLogger) {
65
+ options.infrastructureLogging = {
66
+ level: "error"
67
+ };
68
+ }
69
+ }
70
+ async function check(env, context, name) {
71
+ const compiler = context.getCompiler();
72
+ const stats = compiler.getStats();
73
+ const testConfig = context.getTestConfig();
74
+ if (!stats) {
75
+ throw new Error("No stats found\n" +
76
+ context
77
+ .getError()
78
+ .map(e => e.stack)
79
+ .join("\n"));
80
+ }
81
+ if (REG_ERROR_CASE.test(name)) {
82
+ env.expect(stats.hasErrors());
83
+ }
84
+ else {
85
+ env.expect(!stats.hasErrors());
86
+ }
87
+ if (typeof testConfig.validate === "function") {
88
+ testConfig.validate(stats);
89
+ }
90
+ else {
91
+ throw new Error("HashTestCases should have test.config.js and a validate method");
92
+ }
93
+ }
@@ -1,6 +1,38 @@
1
- import { type IHookProcessorOptions } from "../processor";
2
- import { ECompilerType } from "../type";
3
- export type THookCaseConfig = Omit<IHookProcessorOptions<ECompilerType.Rspack>, "name" | "compilerType" | "runable"> & {
1
+ import { Compiler, type RspackOptions } from "@rspack/core";
2
+ import { TestContext, type TTestContextOptions } from "../test/context";
3
+ import type { ITestContext, ITestEnv } from "../type";
4
+ export declare function createHookCase(name: string, src: string, dist: string, source: string): void;
5
+ export declare class HookCasesContext extends TestContext {
6
+ protected src: string;
7
+ protected testName: string;
8
+ protected options: TTestContextOptions;
9
+ protected promises: Promise<void>[];
10
+ protected count: number;
11
+ protected snapshots: Record<string | number, Array<[string | Buffer, string]>>;
12
+ protected snapshotsList: Array<string | number>;
13
+ constructor(src: string, testName: string, options: TTestContextOptions);
14
+ /**
15
+ * Snapshot function arguments and return value.
16
+ * Generated snapshot is located in the same directory with the test source.
17
+ * @example
18
+ * compiler.hooks.compilation("name", context.snapped((...args) => { ... }))
19
+ */
20
+ snapped(cb: (...args: unknown[]) => Promise<unknown>, prefix?: string): (this: any, ...args: unknown[]) => Promise<unknown>;
21
+ /**
22
+ * @internal
23
+ */
24
+ _addSnapshot(content: unknown, name: string, group: string | number): void;
25
+ /**
26
+ * @internal
27
+ */
28
+ collectSnapshots(env: ITestEnv, options?: {
29
+ diff: {};
30
+ }): Promise<void>;
31
+ }
32
+ export type THookCaseConfig = {
33
+ options?: (context: ITestContext) => RspackOptions;
34
+ compiler?: (context: ITestContext, compiler: Compiler) => Promise<void>;
35
+ check?: (context: ITestContext) => Promise<void>;
36
+ snapshotFileFilter?: (file: string) => boolean;
4
37
  description: string;
5
38
  };
6
- export declare function createHookCase(name: string, src: string, dist: string, source: string): void;
package/dist/case/hook.js CHANGED
@@ -3,33 +3,222 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.HookCasesContext = void 0;
6
7
  exports.createHookCase = createHookCase;
7
8
  const node_path_1 = __importDefault(require("node:path"));
8
- const createLazyTestEnv_1 = __importDefault(require("../helper/legacy/createLazyTestEnv"));
9
- const processor_1 = require("../processor");
10
- const runner_1 = require("../runner");
11
- const simple_1 = require("../test/simple");
12
- const type_1 = require("../type");
9
+ const core_1 = require("@rspack/core");
10
+ const jest_snapshot_1 = require("jest-snapshot");
11
+ const path_serializer_1 = require("path-serializer");
12
+ const pretty_format_1 = require("pretty-format");
13
+ const webpack_merge_1 = __importDefault(require("webpack-merge"));
14
+ const context_1 = require("../test/context");
15
+ const creator_1 = require("../test/creator");
16
+ const common_1 = require("./common");
17
+ const srcDir = __TEST_FIXTURES_PATH__;
18
+ const distDir = node_path_1.default.resolve(__TEST_DIST_PATH__, "hook");
19
+ const creator = new creator_1.BasicCaseCreator({
20
+ clean: true,
21
+ describe: true,
22
+ createContext: (config) => new HookCasesContext(config.src, config.name, config),
23
+ steps: ({ name, caseConfig: _caseConfig, src }) => {
24
+ const caseConfig = _caseConfig;
25
+ return [
26
+ {
27
+ config: async (context) => {
28
+ const compiler = context.getCompiler();
29
+ const options = await (0, common_1.config)(context, name, ["rspack.config.js", "webpack.config.js"], defaultOptions(context, caseConfig.options));
30
+ if (!global.printLogger) {
31
+ options.infrastructureLogging = {
32
+ level: "error"
33
+ };
34
+ }
35
+ compiler.setOptions(options);
36
+ },
37
+ compiler: async (context) => {
38
+ const c = await (0, common_1.compiler)(context, name);
39
+ if (caseConfig.compiler) {
40
+ await caseConfig.compiler(context, c);
41
+ }
42
+ },
43
+ build: async (context) => {
44
+ await (0, common_1.build)(context, name);
45
+ },
46
+ run: async (env, context) => {
47
+ // no need to run, just check snapshot
48
+ },
49
+ check: async (env, context) => {
50
+ await (0, common_1.checkSnapshot)(env, context, name, node_path_1.default.join(src, "output.snap.txt"), caseConfig.snapshotFileFilter);
51
+ }
52
+ }
53
+ ];
54
+ }
55
+ });
13
56
  function createHookCase(name, src, dist, source) {
14
57
  const caseConfig = require(node_path_1.default.join(src, "test.js"));
15
58
  const testName = node_path_1.default.basename(name.slice(0, name.indexOf(node_path_1.default.extname(name))));
16
- const runner = (0, simple_1.getSimpleProcessorRunner)(source, dist, {
17
- env: () => env,
18
- context: () => new processor_1.HookCasesContext(src, testName, {
59
+ creator.create(name, src, dist, undefined, {
60
+ caseConfig,
61
+ description: () => caseConfig.description,
62
+ createContext: (config) => new HookCasesContext(src, testName, {
19
63
  src: source,
20
64
  dist: dist,
21
- runnerFactory: runner_1.BasicRunnerFactory
65
+ name: name
22
66
  })
23
67
  });
24
- it(caseConfig.description, async () => {
25
- await runner(name, new processor_1.HookTaskProcessor({
26
- name,
27
- compilerType: type_1.ECompilerType.Rspack,
28
- findBundle: () => ["main.js"],
29
- snapshot: node_path_1.default.join(src, "output.snap.txt"),
30
- runable: true,
31
- ...caseConfig
32
- }));
33
- });
34
- const env = (0, createLazyTestEnv_1.default)(10000);
68
+ }
69
+ const sourceSerializer = {
70
+ test(val) {
71
+ return val instanceof core_1.sources.Source;
72
+ },
73
+ print(val) {
74
+ return val.source();
75
+ }
76
+ };
77
+ const internalSerializer = {
78
+ test(val) {
79
+ return val instanceof core_1.Compiler || val instanceof core_1.Compilation;
80
+ },
81
+ print(val) {
82
+ return JSON.stringify(`${val.constructor.name}(internal ignored)`);
83
+ }
84
+ };
85
+ const testPathSerializer = (0, path_serializer_1.createSnapshotSerializer)({
86
+ replace: [
87
+ {
88
+ match: srcDir,
89
+ mark: "<HOOK_SRC_DIR>"
90
+ },
91
+ {
92
+ match: distDir,
93
+ mark: "<HOOK_DIST_DIR>"
94
+ }
95
+ ]
96
+ });
97
+ const escapeRegex = true;
98
+ const printFunctionName = false;
99
+ const normalizeNewlines = (str) => str.replace(/\r\n|\r/g, "\n");
100
+ const serialize = (val, indent = 2, formatOverrides = {}) => normalizeNewlines((0, pretty_format_1.format)(val, {
101
+ escapeRegex,
102
+ indent,
103
+ plugins: [
104
+ ...(0, jest_snapshot_1.getSerializers)(),
105
+ sourceSerializer,
106
+ internalSerializer,
107
+ testPathSerializer
108
+ ],
109
+ printFunctionName,
110
+ ...formatOverrides
111
+ }));
112
+ class HookCasesContext extends context_1.TestContext {
113
+ constructor(src, testName, options) {
114
+ super(options);
115
+ this.src = src;
116
+ this.testName = testName;
117
+ this.options = options;
118
+ this.promises = [];
119
+ this.count = 0;
120
+ this.snapshots = {};
121
+ this.snapshotsList = [];
122
+ this.snapped = this.snapped.bind(this);
123
+ }
124
+ /**
125
+ * Snapshot function arguments and return value.
126
+ * Generated snapshot is located in the same directory with the test source.
127
+ * @example
128
+ * compiler.hooks.compilation("name", context.snapped((...args) => { ... }))
129
+ */
130
+ snapped(cb, prefix = "") {
131
+ // eslint-disable-next-line
132
+ const context = this;
133
+ return function SNAPPED_HOOK(...args) {
134
+ const group = prefix ? prefix : context.count++;
135
+ context._addSnapshot(args, "input", group);
136
+ const output = cb.apply(this, args);
137
+ if (output && typeof output.then === "function") {
138
+ let resolve;
139
+ context.promises.push(new Promise(r => (resolve = r)));
140
+ return output
141
+ .then((o) => {
142
+ context._addSnapshot(o, "output (Promise resolved)", group);
143
+ return o;
144
+ })
145
+ .catch((o) => {
146
+ context._addSnapshot(o, "output (Promise rejected)", group);
147
+ return o;
148
+ })
149
+ .finally(resolve);
150
+ }
151
+ context._addSnapshot(output, "output", group);
152
+ return output;
153
+ };
154
+ }
155
+ /**
156
+ * @internal
157
+ */
158
+ _addSnapshot(content, name, group) {
159
+ const normalizedContent = Buffer.isBuffer(content)
160
+ ? content
161
+ : serialize(content, undefined, {
162
+ escapeString: true,
163
+ printBasicPrototype: true
164
+ }).replace(/\r\n/g, "\n");
165
+ (this.snapshots[group] = this.snapshots[group] || []).push([
166
+ normalizedContent,
167
+ name
168
+ ]);
169
+ if (!this.snapshotsList.includes(group)) {
170
+ this.snapshotsList.push(group);
171
+ }
172
+ }
173
+ /**
174
+ * @internal
175
+ */
176
+ async collectSnapshots(env, options = {
177
+ diff: {}
178
+ }) {
179
+ await Promise.allSettled(this.promises);
180
+ if (!this.snapshotsList.length)
181
+ return;
182
+ const snapshots = this.snapshotsList.reduce((acc, group, index) => {
183
+ const block = this.snapshots[group || index].reduce((acc, [content, name]) => {
184
+ name = `## ${name || `test: ${index}`}\n\n`;
185
+ const block = `\`\`\`javascript\n${content}\n\`\`\`\n`;
186
+ return `${acc}${name + block}\n`;
187
+ }, "");
188
+ return `${acc}# ${Number.isInteger(group) ? `Group: ${index}` : group}\n\n${block}`;
189
+ }, "");
190
+ env
191
+ .expect(snapshots)
192
+ .toMatchFileSnapshotSync(node_path_1.default.join(this.src, "hooks.snap.txt"), options);
193
+ }
194
+ }
195
+ exports.HookCasesContext = HookCasesContext;
196
+ function defaultOptions(context, custom) {
197
+ let defaultOptions = {
198
+ context: context.getSource(),
199
+ mode: "production",
200
+ target: "async-node",
201
+ devtool: false,
202
+ cache: false,
203
+ entry: "./hook",
204
+ output: {
205
+ path: context.getDist()
206
+ },
207
+ optimization: {
208
+ minimize: false
209
+ },
210
+ experiments: {
211
+ css: true,
212
+ rspackFuture: {
213
+ bundlerInfo: {
214
+ force: false
215
+ }
216
+ },
217
+ inlineConst: true
218
+ }
219
+ };
220
+ if (custom) {
221
+ defaultOptions = (0, webpack_merge_1.default)(defaultOptions, custom(context));
222
+ }
223
+ return defaultOptions;
35
224
  }
@@ -1,2 +1,2 @@
1
- import { ECompilerType, type TCompilerOptions } from "../type";
2
- export declare function createHotStepCase(name: string, src: string, dist: string, target: TCompilerOptions<ECompilerType.Rspack>["target"]): void;
1
+ import type { RspackOptions } from "@rspack/core";
2
+ export declare function createHotStepCase(name: string, src: string, dist: string, temp: string, target: RspackOptions["target"]): void;