Skip to content

Commit 89ddc85

Browse files
authored
test(snapshot): make snapshots cleaner and update path-serializer 0.3.4 (#8161)
* chore: update * chore: upate * chore: update * chore: update * chore: update * chore: update * chore: update
1 parent 4cedd61 commit 89ddc85

File tree

100 files changed

+498
-624
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+498
-624
lines changed

biome.jsonc

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"useKeyWithClickEvents": "off"
3434
},
3535
"complexity": {
36+
"useArrowFunction": "off",
3637
"noStaticOnlyClass": "off",
3738
"noThisInStatic": "off",
3839
"noBannedTypes": "off"

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"build:cli:release:win": "pnpm --filter @rspack/binding build:release:win && npm run build:js",
2121
"build:viewer": "pnpm --filter \"@rspack/*\" build:viewer",
2222
"test:js": "pnpm -r run test",
23-
"format:rs": "pnpm --filter @rspack/core... build",
23+
"format:rs": "cargo fmt --all",
2424
"format:js": "pnpm run format-ci:js --write",
2525
"format-ci:js": "node ./node_modules/prettier/bin/prettier.cjs \"packages/**/src/**/*.{ts,tsx,js}\" \"crates/rspack_plugin_runtime/**/*.{ts,js}\" \"scripts/**/*.{cjs,mjs,js}\" \"x.mjs\" \"x\" --check",
2626
"format-ci:toml": "npx @taplo/cli format --check '.cargo/*.toml' './crates/**/Cargo.toml' './Cargo.toml'",

packages/rspack-test-tools/etc/test-tools.api.md

+2-6
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,6 @@ export class ErrorProcessor<T extends ECompilerType> extends SimpleTaskProcessor
352352
run(env: ITestEnv, context: ITestContext): Promise<void>;
353353
}
354354

355-
// @public (undocumented)
356-
export function escapeEOL(str: string): string;
357-
358355
// @public (undocumented)
359356
export function escapeSep(str: string): string;
360357

@@ -1130,9 +1127,6 @@ export function readConfigFile<T extends ECompilerType>(files: string[], functio
11301127
// @public (undocumented)
11311128
export function replaceModuleArgument(raw: string): string;
11321129

1133-
// @public (undocumented)
1134-
export function replacePaths(input: string): any;
1135-
11361130
// @public (undocumented)
11371131
export function replaceRuntimeModuleName(name: string): string;
11381132

@@ -1194,6 +1188,8 @@ export class SnapshotProcessor<T extends ECompilerType> extends BasicProcessor<T
11941188
// (undocumented)
11951189
check(env: ITestEnv, context: ITestContext): Promise<void>;
11961190
// (undocumented)
1191+
serializeEachFile(content: string): string;
1192+
// (undocumented)
11971193
protected _snapshotOptions: ISnapshotProcessorOptions<T>;
11981194
}
11991195

packages/rspack-test-tools/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,11 @@
5757
"fs-extra": "^11.2.0",
5858
"glob": "^10.3.10",
5959
"jest-diff": "^29.7.0",
60-
"jest-serializer-path": "^0.1.15",
6160
"jest-snapshot": "29.7.0",
6261
"jsdom": "^25.0.0",
6362
"memfs": "4.14.0",
6463
"mkdirp": "0.5.6",
65-
"path-serializer": "0.1.2",
64+
"path-serializer": "0.3.4",
6665
"pretty-format": "29.7.0",
6766
"rimraf": "3.0.2",
6867
"webpack": "^5.94.0",

packages/rspack-test-tools/src/helper/expect/char.ts

-23
This file was deleted.

packages/rspack-test-tools/src/helper/expect/placeholder.ts

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import path from "node:path";
2-
const { createSnapshotSerializer } = require("path-serializer");
2+
import { createSnapshotSerializer } from "path-serializer";
33

4+
// 1. escapeEOL \r\n -> \n
5+
// 2. replace <RSPACK_ROOT> etc
6+
// 3. transform win32 sep
47
const placeholderSerializer = createSnapshotSerializer({
5-
workspace: path.resolve(__dirname, "../../../../../"),
8+
root: path.resolve(__dirname, "../../../../../"),
69
replace: [
710
{
8-
match: path.resolve(__dirname, "../../../rspack"),
9-
mark: "rspack"
11+
match: path.resolve(__dirname, "../../../"),
12+
mark: "test_tools_root"
1013
},
1114
{
12-
match: path.resolve(__dirname, "../../"),
13-
mark: "test_tools"
15+
match: path.resolve(__dirname, "../../../../rspack"),
16+
mark: "rspack_root"
1417
},
1518
{
1619
match: /:\d+:\d+-\d+:\d+/g,
@@ -22,8 +25,9 @@ const placeholderSerializer = createSnapshotSerializer({
2225
}
2326
],
2427
features: {
28+
replaceWorkspace: false,
2529
addDoubleQuotes: false,
26-
ansiDoubleQuotes: false
30+
escapeDoubleQuotes: false
2731
}
2832
});
2933

packages/rspack-test-tools/src/helper/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ export * from "./parse-modules";
33
export * from "./read-config-file";
44
export * from "./update-snapshot";
55
export * from "./win";
6-
export * from "./replace-paths";
76
export * from "./util/checkStats";

packages/rspack-test-tools/src/helper/replace-paths.ts

-21
This file was deleted.

packages/rspack-test-tools/src/helper/setup-expect.ts

+1-17
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import { normalizeCLR, normalizeCRLF, normalizeSlash } from "./expect/char";
21
import { normalizeDiff } from "./expect/diff";
32
import { normalizeDignostics, normalizeError } from "./expect/error";
43
import { normalizePlaceholder } from "./expect/placeholder";
54
import { normalizeStats } from "./expect/rspack";
65
import { toBeTypeOf } from "./expect/to-be-typeof";
76
import { toEndWith } from "./expect/to-end-with";
87
import { toMatchFileSnapshot } from "./expect/to-match-file-snapshot";
9-
const { normalizePaths } = require("jest-serializer-path");
108

119
expect.extend({
1210
// CHANGE: new test matcher for `rspack-test-tools`
@@ -16,26 +14,12 @@ expect.extend({
1614
toEndWith
1715
});
1816

19-
const pipes = [
20-
normalizeSlash,
21-
normalizeCLR,
22-
normalizeCRLF,
23-
normalizePlaceholder,
24-
normalizePaths
25-
];
26-
27-
const serialize = (
28-
str: string,
29-
extra: Array<(str: string) => string> = []
30-
): string =>
31-
[...pipes, ...extra].reduce((res, transform) => transform(res), str);
32-
3317
expect.addSnapshotSerializer({
3418
test(received) {
3519
return typeof received === "string";
3620
},
3721
print(received) {
38-
return serialize((received as string).trim());
22+
return normalizePlaceholder((received as string).trim());
3923
}
4024
});
4125

packages/rspack-test-tools/src/helper/win.ts

-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,3 @@ import path from "node:path";
33
export function escapeSep(str: string) {
44
return str.split(path.win32.sep).join(path.posix.sep);
55
}
6-
7-
export function escapeEOL(str: string) {
8-
return str.split("\r\n").join("\n").trim();
9-
}

packages/rspack-test-tools/src/processor/diagnostic.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import assert from "node:assert";
22
import path from "node:path";
33

4-
import { escapeEOL } from "../helper";
5-
import { replacePaths } from "../helper/replace-paths";
4+
import { normalizePlaceholder } from "../helper/expect/placeholder";
65
import type {
76
ECompilerType,
87
ITestContext,
@@ -34,13 +33,13 @@ export class DiagnosticProcessor<
3433
throw new Error("Stats should exists");
3534
}
3635
assert(stats.hasErrors() || stats.hasWarnings());
37-
let output = replacePaths(
36+
let output = normalizePlaceholder(
3837
stats.toString({
3938
all: false,
4039
errors: true,
4140
warnings: true
4241
})
43-
);
42+
).replaceAll("\\", "/"); // stats has some win32 paths that path-serializer can not handle
4443

4544
if (typeof this._diagnosticOptions.format === "function") {
4645
output = this._diagnosticOptions.format(output);
@@ -49,7 +48,7 @@ export class DiagnosticProcessor<
4948
const errorOutputPath = path.resolve(
5049
context.getSource(this._diagnosticOptions.snapshot)
5150
);
52-
env.expect(escapeEOL(output)).toMatchFileSnapshot(errorOutputPath);
51+
env.expect(output).toMatchFileSnapshot(errorOutputPath);
5352
}
5453

5554
static defaultOptions<T extends ECompilerType>(

packages/rspack-test-tools/src/processor/hook.ts

+13-19
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
format as prettyFormat
77
} from "pretty-format";
88

9+
import { createSnapshotSerializer as createPathSerializer } from "path-serializer";
910
import { type TTestContextOptions, TestContext } from "../test/context";
1011
import type {
1112
ECompilerType,
@@ -16,8 +17,6 @@ import type {
1617
} from "../type";
1718
import { type ISnapshotProcessorOptions, SnapshotProcessor } from "./snapshot";
1819

19-
const pathSerializer = require("jest-serializer-path");
20-
const normalizePaths = pathSerializer.normalizePaths;
2120
const srcDir = path.resolve(__dirname, "../../tests/fixtures");
2221
const distDir = path.resolve(__dirname, "../../tests/js/hook");
2322

@@ -39,23 +38,18 @@ const internalSerializer = {
3938
}
4039
};
4140

42-
const testPathSerializer = {
43-
test(val: unknown) {
44-
return typeof val === "string";
45-
},
46-
print(val: string) {
47-
return JSON.stringify(
48-
normalizePaths(
49-
// @ts-ignore
50-
val
51-
.split(srcDir)
52-
.join("<HOOK_SRC_DIR>")
53-
.split(distDir)
54-
.join("<HOOK_DIST_DIR>")
55-
)
56-
);
57-
}
58-
};
41+
const testPathSerializer = createPathSerializer({
42+
replace: [
43+
{
44+
match: srcDir,
45+
mark: "<HOOK_SRC_DIR>"
46+
},
47+
{
48+
match: distDir,
49+
mark: "<HOOK_DIST_DIR>"
50+
}
51+
]
52+
});
5953

6054
const escapeRegex = true;
6155
const printFunctionName = false;

packages/rspack-test-tools/src/processor/hot-step.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import path from "node:path";
22
import fs from "fs-extra";
33

44
import type { Chunk } from "webpack";
5-
import { escapeEOL, escapeSep, replacePaths } from "../helper";
5+
import { normalizePlaceholder } from "../helper/expect/placeholder";
6+
import { escapeSep } from "../helper/win";
67
import type { THotStepRuntimeData } from "../runner";
78
import type {
89
ECompilerType,
@@ -223,7 +224,7 @@ export class HotSnapshotProcessor<
223224
}
224225

225226
const replaceContent = (str: string) => {
226-
return replacePaths(
227+
return normalizePlaceholder(
227228
Object.entries(hashes)
228229
.reduce((str, [raw, replacement]) => {
229230
return str.split(raw).join(replacement);
@@ -400,6 +401,6 @@ ${runtime.javascript.disposedModules.map(i => `- ${i}`).join("\n")}
400401
401402
`.trim();
402403

403-
env.expect(escapeEOL(content)).toMatchFileSnapshot(snapshotPath);
404+
env.expect(content).toMatchFileSnapshot(snapshotPath);
404405
}
405406
}

packages/rspack-test-tools/src/processor/snapshot.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import type {
55
Compiler as WebpackCompiler
66
} from "webpack";
77

8-
import { escapeEOL } from "../helper";
98
import type { ECompilerType, ITestContext, ITestEnv } from "../type";
109
import { BasicProcessor, type IBasicProcessorOptions } from "./basic";
1110

@@ -56,12 +55,12 @@ export class SnapshotProcessor<
5655
.filter(([file]) => snapshotFileFilter(file))
5756
.map(([file, source]) => {
5857
const tag = path.extname(file).slice(1) || "txt";
59-
return `\`\`\`${tag} title=${file}\n${source
60-
.source()
61-
.toString()}\n\`\`\``;
58+
const content = this.serializeEachFile(source.source().toString());
59+
60+
return `\`\`\`${tag} title=${file}\n${content}\n\`\`\``;
6261
});
6362
fileContents.sort();
64-
const content = escapeEOL(fileContents.join("\n\n"));
63+
const content = fileContents.join("\n\n");
6564
const snapshotPath = path.isAbsolute(this._snapshotOptions.snapshot)
6665
? this._snapshotOptions.snapshot
6766
: path.resolve(
@@ -71,4 +70,8 @@ export class SnapshotProcessor<
7170

7271
env.expect(content).toMatchFileSnapshot(snapshotPath);
7372
}
73+
74+
serializeEachFile(content: string): string {
75+
return content;
76+
}
7477
}

packages/rspack-test-tools/src/processor/stats.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from "node:fs";
22
import path from "node:path";
33
import type { Compiler, Stats } from "@rspack/core";
44

5-
import { escapeEOL } from "../helper";
5+
import { normalizePlaceholder } from "../helper/expect/placeholder";
66
import captureStdio from "../helper/legacy/captureStdio";
77
import type {
88
ECompilerType,
@@ -63,7 +63,7 @@ export class StatsProcessor<
6363
if (err) return callback(err);
6464
if (!/\.(js|json|txt)$/.test(args[0]))
6565
return callback(null, result);
66-
callback(null, escapeEOL(result.toString("utf-8")));
66+
callback(null, normalizePlaceholder(result.toString("utf-8")));
6767
}
6868
]) as Parameters<typeof ifs.readFile>
6969
);

packages/rspack-test-tools/src/processor/treeshaking.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { normalizePlaceholder } from "../helper/expect/placeholder";
12
import type { ECompilerType, ITestContext, TCompilerOptions } from "../type";
23
import { BuiltinProcessor } from "./builtin";
34
import { type ISnapshotProcessorOptions, SnapshotProcessor } from "./snapshot";
@@ -27,4 +28,8 @@ export class TreeShakingProcessor<
2728
options.optimization.innerGraph = true;
2829
options.optimization.usedExports = true;
2930
}
31+
32+
serializeEachFile(content: string): string {
33+
return normalizePlaceholder(content);
34+
}
3035
}

0 commit comments

Comments
 (0)