@rspack/test-tools 1.0.0-beta.2 → 1.0.0-beta.3

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.
@@ -1 +1 @@
1
- export declare function replaceRuntimeModuleName(raw: string): string;
1
+ export declare function replaceRuntimeModuleName(name: string): string;
@@ -53,17 +53,14 @@ const RUNTIME_MODULE_PARAM_REGEX = {
53
53
  "webpack/runtime/get_main_filename": /webpack\/runtime\/get_main_filename\/([\w.\-_\s]+)(\*\/)?/g,
54
54
  "webpack/runtime/chunk_prefetch_function": /webpack\/runtime\/chunk_prefetch_function\/([\w.\-_\s]+)(\*\/)?/g
55
55
  };
56
- function replaceRuntimeModuleName(raw) {
57
- for (const [rspackName, webpackName] of Object.entries(RUNTIME_MODULE_NAME_MAPPING)) {
56
+ function replaceRuntimeModuleName(name) {
57
+ return Object.entries(RUNTIME_MODULE_NAME_MAPPING).reduce((name, [rspackName, webpackName]) => {
58
58
  if (RUNTIME_MODULE_PARAM_REGEX[rspackName]) {
59
- raw = raw.replace(RUNTIME_MODULE_PARAM_REGEX[rspackName], (full, $1, $2) => {
59
+ return name.replace(RUNTIME_MODULE_PARAM_REGEX[rspackName], (_, $1, $2) => {
60
60
  return webpackName.replace("$1", $1.trim()) + ($2 ? " */" : "");
61
61
  });
62
62
  }
63
- else {
64
- raw = raw.split(rspackName).join(webpackName);
65
- }
66
- }
67
- return raw;
63
+ return name.split(rspackName).join(webpackName);
64
+ }, name);
68
65
  }
69
66
  exports.replaceRuntimeModuleName = replaceRuntimeModuleName;
@@ -1,12 +1,24 @@
1
+ /// <reference types="node" />
2
+ import type { FileMatcherOptions } from "../../../jest";
1
3
  /**
2
4
  * Match given content against content of the specified file.
3
5
  *
4
- * @param {string | Buffer} content Output content to match
5
- * @param {string} [filepath] Path to the file to match against
6
- * @param {{ diff?: import('jest-diff').DiffOptions }} options Additional options for matching
7
- * @this {{ testPath: string, currentTestName: string, assertionCalls: number, isNot: boolean, snapshotState: { added: number, updated: number, unmatched: number, _updateSnapshot: 'none' | 'new' | 'all' } }}
6
+ * @param content Output content to match
7
+ * @param filepath Path to the file to match against
8
+ * @param options Additional options for matching
8
9
  */
9
- export declare function toMatchFileSnapshot(content: any, filepath: any, options?: {}): {
10
- pass: any;
10
+ export declare function toMatchFileSnapshot(this: {
11
+ testPath: string;
12
+ currentTestName: string;
13
+ assertionCalls: number;
14
+ isNot: boolean;
15
+ snapshotState: {
16
+ added: number;
17
+ updated: number;
18
+ unmatched: number;
19
+ _updateSnapshot: "none" | "new" | "all";
20
+ };
21
+ }, content: string | Buffer, filepath: string, options?: FileMatcherOptions): {
22
+ pass: boolean;
11
23
  message: () => string;
12
24
  };
@@ -14,8 +14,6 @@ const jest_diff_1 = require("jest-diff");
14
14
  const mkdirp_1 = __importDefault(require("mkdirp"));
15
15
  /**
16
16
  * Check if 2 strings or buffer are equal
17
- * @param {string | Buffer} a
18
- * @param {string | Buffer} b
19
17
  */
20
18
  const isEqual = (a, b) => {
21
19
  // @ts-ignore: TypeScript gives error if we pass string to buffer.equals
@@ -24,10 +22,9 @@ const isEqual = (a, b) => {
24
22
  /**
25
23
  * Match given content against content of the specified file.
26
24
  *
27
- * @param {string | Buffer} content Output content to match
28
- * @param {string} [filepath] Path to the file to match against
29
- * @param {{ diff?: import('jest-diff').DiffOptions }} options Additional options for matching
30
- * @this {{ testPath: string, currentTestName: string, assertionCalls: number, isNot: boolean, snapshotState: { added: number, updated: number, unmatched: number, _updateSnapshot: 'none' | 'new' | 'all' } }}
25
+ * @param content Output content to match
26
+ * @param filepath Path to the file to match against
27
+ * @param options Additional options for matching
31
28
  */
32
29
  function toMatchFileSnapshot(content, filepath, options = {}) {
33
30
  const { isNot, snapshotState } = this;
@@ -37,14 +34,6 @@ function toMatchFileSnapshot(content, filepath, options = {}) {
37
34
  replacement: "-"
38
35
  }).replace(/\s/g, "-")}-${this.assertionCalls}`)
39
36
  : filepath;
40
- options = {
41
- // Options for jest-diff
42
- diff: Object.assign({
43
- expand: false,
44
- contextLines: 5,
45
- aAnnotation: "Snapshot"
46
- }, options.diff || {})
47
- };
48
37
  if (snapshotState._updateSnapshot === "none" && !node_fs_1.default.existsSync(filename)) {
49
38
  // We're probably running in CI environment
50
39
  snapshotState.unmatched++;
@@ -81,7 +70,11 @@ function toMatchFileSnapshot(content, filepath, options = {}) {
81
70
  snapshotState.unmatched++;
82
71
  const difference = Buffer.isBuffer(content) || Buffer.isBuffer(output)
83
72
  ? ""
84
- : `\n\n${(0, jest_diff_1.diff)(output, content, options.diff)}`;
73
+ : `\n\n${(0, jest_diff_1.diff)(output, content, Object.assign({
74
+ expand: false,
75
+ contextLines: 5,
76
+ aAnnotation: "Snapshot"
77
+ }, options.diff || {}))}`;
85
78
  return {
86
79
  pass: false,
87
80
  message: () => `Received content ${chalk_1.default.red("doesn't match")} the file ${chalk_1.default.blue(node_path_1.default.basename(filename))}.${difference}`
@@ -10,16 +10,16 @@ const serializer = require("jest-serializer-path");
10
10
  const normalizePaths = serializer.normalizePaths;
11
11
  const rspackPath = node_path_1.default.resolve(__dirname, "../../../rspack");
12
12
  function replacePaths(input) {
13
+ const paths = input.split("\\\\").join("\\");
13
14
  const rspackRoot = normalizePaths(rspackPath);
14
- input = input.split("\\\\").join("\\");
15
15
  if (node_os_1.default.platform() === "win32") {
16
16
  const winRspackRoot = rspackRoot.split("\\\\").join(node_path_1.default.win32.sep);
17
- return normalizePaths(input)
17
+ return normalizePaths(paths)
18
18
  .split(rspackRoot)
19
19
  .join("<RSPACK_ROOT>")
20
20
  .split(winRspackRoot)
21
21
  .join("<RSPACK_ROOT>");
22
22
  }
23
- return normalizePaths(input).split(rspackRoot).join("<RSPACK_ROOT>");
23
+ return normalizePaths(paths).split(rspackRoot).join("<RSPACK_ROOT>");
24
24
  }
25
25
  exports.replacePaths = replacePaths;
@@ -311,20 +311,20 @@ exports.parseResourceWithoutFragment = makeCacheable(_parseResourceWithoutFragme
311
311
  exports.getUndoPath = (filename, outputPath, enforceRelative) => {
312
312
  let depth = -1;
313
313
  let append = "";
314
- outputPath = outputPath.replace(/[\\/]$/, "");
314
+ let path = outputPath.replace(/[\\/]$/, "");
315
315
  for (const part of filename.split(/[/\\]+/)) {
316
316
  if (part === "..") {
317
317
  if (depth > -1) {
318
318
  depth--;
319
319
  }
320
320
  else {
321
- const i = outputPath.lastIndexOf("/");
322
- const j = outputPath.lastIndexOf("\\");
321
+ const i = path.lastIndexOf("/");
322
+ const j = path.lastIndexOf("\\");
323
323
  const pos = i < 0 ? j : j < 0 ? i : Math.max(i, j);
324
324
  if (pos < 0)
325
- return `${outputPath}/`;
326
- append = `${outputPath.slice(pos + 1)}/${append}`;
327
- outputPath = outputPath.slice(0, pos);
325
+ return `${path}/`;
326
+ append = `${path.slice(pos + 1)}/${append}`;
327
+ path = path.slice(0, pos);
328
328
  }
329
329
  }
330
330
  else if (part !== ".") {
@@ -13,15 +13,16 @@ const cwdRegExp = new RegExp(`${quoteMeta(CURRENT_CWD)}((?:\\\\)?(?:[a-zA-Z.\\-_
13
13
  const escapedCwd = JSON.stringify(CURRENT_CWD).slice(1, -1);
14
14
  const escapedCwdRegExp = new RegExp(`${quoteMeta(escapedCwd)}((?:\\\\\\\\)?(?:[a-zA-Z.\\-_]+\\\\\\\\)*)`, "g");
15
15
  const normalize = (str) => {
16
+ let normalizedStr;
16
17
  if (CURRENT_CWD.startsWith("/")) {
17
- str = str.replace(new RegExp(quoteMeta(CURRENT_CWD), "g"), "<cwd>");
18
+ normalizedStr = str.replace(new RegExp(quoteMeta(CURRENT_CWD), "g"), "<cwd>");
18
19
  }
19
20
  else {
20
- str = str.replace(cwdRegExp, (m, g) => `<cwd>${g.replace(/\\/g, "/")}`);
21
- str = str.replace(escapedCwdRegExp, (m, g) => `<cwd>${g.replace(/\\\\/g, "/")}`);
21
+ normalizedStr = str.replace(cwdRegExp, (_, g) => `<cwd>${g.replace(/\\/g, "/")}`);
22
+ normalizedStr = normalizedStr.replace(escapedCwdRegExp, (_, g) => `<cwd>${g.replace(/\\\\/g, "/")}`);
22
23
  }
23
- str = str.replace(/@@ -\d+,\d+ \+\d+,\d+ @@/g, "@@ ... @@");
24
- return str;
24
+ normalizedStr = normalizedStr.replace(/@@ -\d+,\d+ \+\d+,\d+ @@/g, "@@ ... @@");
25
+ return normalizedStr;
25
26
  };
26
27
  class Diff {
27
28
  constructor(value) {
@@ -71,12 +72,13 @@ class DefaultsConfigProcessor extends simple_1.SimpleTaskProcessor {
71
72
  static getDefaultConfig(cwd, config) {
72
73
  process.chdir(cwd);
73
74
  const { applyWebpackOptionsDefaults, getNormalizedWebpackOptions } = require("@rspack/core").config;
74
- config = getNormalizedWebpackOptions(config);
75
- applyWebpackOptionsDefaults(config);
75
+ const normalizedConfig = getNormalizedWebpackOptions(config);
76
+ applyWebpackOptionsDefaults(normalizedConfig);
76
77
  // make snapshot stable
77
- config.experiments.rspackFuture.bundlerInfo.version = "$version$";
78
+ normalizedConfig.experiments.rspackFuture.bundlerInfo.version =
79
+ "$version$";
78
80
  process.chdir(CURRENT_CWD);
79
- return config;
81
+ return normalizedConfig;
80
82
  }
81
83
  static addSnapshotSerializer(expectImpl) {
82
84
  expectImpl.addSnapshotSerializer({
@@ -1,11 +1,11 @@
1
1
  /// <reference types="jest" />
2
2
  /// <reference types="../jest.d.ts" />
3
- import type { StatsError, StatsWarnings } from "@rspack/core";
3
+ import type { StatsError } from "@rspack/core";
4
4
  import type { ECompilerType, ITestContext, ITestEnv, TCompiler, TCompilerOptions } from "../type";
5
5
  import { SimpleTaskProcessor } from "./simple";
6
6
  type TStatsDiagnostics = {
7
7
  errors: StatsError[];
8
- warnings: StatsWarnings[];
8
+ warnings: StatsError[];
9
9
  };
10
10
  export interface IErrorProcessorOptions<T extends ECompilerType> {
11
11
  name: string;
@@ -106,14 +106,14 @@ class HookCasesContext extends context_1.TestContext {
106
106
  * @internal
107
107
  */
108
108
  _addSnapshot(content, name, group) {
109
- content = Buffer.isBuffer(content)
109
+ const normalizedContent = Buffer.isBuffer(content)
110
110
  ? content
111
111
  : serialize(content, undefined, {
112
112
  escapeString: true,
113
113
  printBasicPrototype: true
114
114
  }).replace(/\r\n/g, "\n");
115
115
  (this.snapshots[group] = this.snapshots[group] || []).push([
116
- content,
116
+ normalizedContent,
117
117
  name
118
118
  ]);
119
119
  if (!this.snapshotsList.includes(group)) {
@@ -133,11 +133,9 @@ class HookCasesContext extends context_1.TestContext {
133
133
  const block = this.snapshots[group || index].reduce((acc, [content, name]) => {
134
134
  name = `## ${name || `test: ${index}`}\n\n`;
135
135
  const block = `\`\`\`javascript\n${content}\n\`\`\`\n`;
136
- return (acc += `${name + block}\n`);
136
+ return `${acc}${name + block}\n`;
137
137
  }, "");
138
- group = Number.isInteger(group) ? `Group: ${index}` : group;
139
- group = `# ${group}\n\n`;
140
- return (acc += group + block);
138
+ return `${acc}# ${Number.isInteger(group) ? `Group: ${index}` : group}\n\n${block}`;
141
139
  }, "");
142
140
  env
143
141
  .expect(snapshots)
@@ -134,21 +134,19 @@ class HotSnapshotProcessor extends hot_1.HotProcessor {
134
134
  }
135
135
  }
136
136
  const replaceContent = (str) => {
137
- for (const [raw, replacement] of Object.entries(hashes)) {
138
- str = str.split(raw).join(replacement);
139
- }
140
- // handle timestamp in css-extract
141
- str = str.replace(/\/\/ (\d+)\s+(?=var cssReload)/, "");
142
- return (0, helper_1.replacePaths)(str);
137
+ return (0, helper_1.replacePaths)(Object.entries(hashes)
138
+ .reduce((str, [raw, replacement]) => {
139
+ return str.split(raw).join(replacement);
140
+ }, str)
141
+ .replace(/\/\/ (\d+)\s+(?=var cssReload)/, ""));
143
142
  };
144
143
  const replaceFileName = (str) => {
145
- for (const [raw, replacement] of Object.entries({
144
+ return Object.entries({
146
145
  ...hashes,
147
146
  ...runtimes
148
- })) {
149
- str = str.split(raw).join(replacement);
150
- }
151
- return str;
147
+ }).reduce((str, [raw, replacement]) => {
148
+ return str.split(raw).join(replacement);
149
+ }, str);
152
150
  };
153
151
  const fileList = stats
154
152
  .assets.map(i => {
@@ -58,11 +58,11 @@ class HotStepRunnerFactory extends hot_1.HotRunnerFactory {
58
58
  testConfig: {
59
59
  ...testConfig,
60
60
  moduleScope(ms, stats) {
61
- if (typeof testConfig.moduleScope === "function") {
62
- ms = testConfig.moduleScope(ms, stats);
63
- }
64
- ms.NEXT = next;
65
- return ms;
61
+ const moduleScope = typeof testConfig.moduleScope === "function"
62
+ ? testConfig.moduleScope(ms, stats)
63
+ : ms;
64
+ moduleScope.NEXT = next;
65
+ return moduleScope;
66
66
  }
67
67
  },
68
68
  source,
@@ -49,11 +49,11 @@ class HotRunnerFactory extends basic_1.BasicRunnerFactory {
49
49
  testConfig: {
50
50
  ...testConfig,
51
51
  moduleScope(ms, stats) {
52
- if (typeof testConfig.moduleScope === "function") {
53
- ms = testConfig.moduleScope(ms, stats);
54
- }
55
- ms.NEXT = next;
56
- return ms;
52
+ const moduleScope = typeof testConfig.moduleScope === "function"
53
+ ? testConfig.moduleScope(ms, stats)
54
+ : ms;
55
+ moduleScope.NEXT = next;
56
+ return moduleScope;
57
57
  }
58
58
  },
59
59
  source,
@@ -38,9 +38,7 @@ class FakeDocumentWebRunner extends cjs_1.CommonJsRunner {
38
38
  globalContext.document = this.document;
39
39
  globalContext.getComputedStyle = this.document.getComputedStyle.bind(this.document);
40
40
  const urlToPath = (url) => {
41
- if (url.startsWith("https://test.cases/path/"))
42
- url = url.slice(24);
43
- return node_path_1.default.resolve(this._options.dist, `./${url}`);
41
+ return node_path_1.default.resolve(this._options.dist, `./${url.startsWith("https://test.cases/path/") ? url.slice(24) : url}`);
44
42
  };
45
43
  globalContext.fetch = async (url) => {
46
44
  try {
@@ -70,9 +70,9 @@ class JSDOMWebRunner extends cjs_1.CommonJsRunner {
70
70
  }
71
71
  createResourceLoader() {
72
72
  const urlToPath = (url) => {
73
- if (url.startsWith("https://test.cases/path/"))
74
- url = url.slice(24);
75
- return node_path_1.default.resolve(this._webOptions.dist, `./${url}`).split("?")[0];
73
+ return node_path_1.default
74
+ .resolve(this._webOptions.dist, `./${url.startsWith("https://test.cases/path/") ? url.slice(24) : url}`)
75
+ .split("?")[0];
76
76
  };
77
77
  class CustomResourceLoader extends jsdom_1.ResourceLoader {
78
78
  fetch(url, _) {
@@ -97,9 +97,7 @@ class JSDOMWebRunner extends cjs_1.CommonJsRunner {
97
97
  outputDirectory: this._options.dist
98
98
  });
99
99
  const urlToPath = (url) => {
100
- if (url.startsWith("https://test.cases/path/"))
101
- url = url.slice(24);
102
- return node_path_1.default.resolve(this._webOptions.dist, `./${url}`);
100
+ return node_path_1.default.resolve(this._webOptions.dist, `./${url.startsWith("https://test.cases/path/") ? url.slice(24) : url}`);
103
101
  };
104
102
  moduleScope.fetch = async (url) => {
105
103
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rspack/test-tools",
3
- "version": "1.0.0-beta.2",
3
+ "version": "1.0.0-beta.3",
4
4
  "license": "MIT",
5
5
  "description": "Test tools for rspack",
6
6
  "main": "dist/index.js",
@@ -93,14 +93,15 @@
93
93
  "react": "^18.2.0",
94
94
  "react-dom": "^18.2.0",
95
95
  "react-refresh": "^0.14.0",
96
- "sass-loader": "14.2.1",
96
+ "sass-embedded": "^1.77.8",
97
+ "sass-loader": "^16.0.0",
97
98
  "style-loader": "^3.3.3",
98
99
  "source-map-loader": "^5.0.0",
99
100
  "terser": "5.27.2",
100
101
  "typescript": "5.0.2",
101
102
  "wast-loader": "^1.12.1",
102
- "@rspack/core": "1.0.0-beta.2",
103
- "@rspack/cli": "1.0.0-beta.2"
103
+ "@rspack/cli": "1.0.0-beta.3",
104
+ "@rspack/core": "1.0.0-beta.3"
104
105
  },
105
106
  "peerDependencies": {
106
107
  "@rspack/core": ">=0.7.0"