-
-
Notifications
You must be signed in to change notification settings - Fork 552
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add global snapshot serializer (#8137)
* test: add global snapshot serializer * test: add global snapshot serializer * test: add global snapshot serializer * test: add global snapshot serializer * test: add global snapshot serializer * test: add global snapshot serializer * test: add global snapshot serializer
- Loading branch information
1 parent
ae075e5
commit 5a6162c
Showing
60 changed files
with
4,063 additions
and
4,013 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
export const normalizeCRLF = (str: string): string => { | ||
return str.replace(/\r\n?/g, "\n"); | ||
}; | ||
|
||
export const normalizeCLR = (str: string): string => { | ||
return ( | ||
str | ||
.replace(/\u001b\[1m\u001b\[([0-9;]*)m/g, "<CLR=$1,BOLD>") | ||
.replace(/\u001b\[1m/g, "<CLR=BOLD>") | ||
.replace(/\u001b\[39m\u001b\[22m/g, "</CLR>") | ||
.replace(/\u001b\[([0-9;]*)m/g, "<CLR=$1>") | ||
// CHANGE: The time unit display in Rspack is second | ||
.replace(/[.0-9]+(<\/CLR>)?(\s?s)/g, "X$1$2") | ||
); | ||
}; | ||
|
||
export const normalizeColor = (str: string): string => { | ||
return str.replace(/\u001b\[[0-9;]*m/g, ""); | ||
}; | ||
|
||
export const normalizeSlash = (str: string): string => { | ||
return str.replace(/(\\)+/g, "/"); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const CURRENT_CWD = process.cwd(); | ||
|
||
const quoteMeta = (str: string) => str.replace(/[-[\]\\/{}()*+?.^$|]/g, "\\$&"); | ||
const cwdRegExp = new RegExp( | ||
`${quoteMeta(CURRENT_CWD)}((?:\\\\)?(?:[a-zA-Z.\\-_]+\\\\)*)`, | ||
"g" | ||
); | ||
const escapedCwd = JSON.stringify(CURRENT_CWD).slice(1, -1); | ||
const escapedCwdRegExp = new RegExp( | ||
`${quoteMeta(escapedCwd)}((?:\\\\\\\\)?(?:[a-zA-Z.\\-_]+\\\\\\\\)*)`, | ||
"g" | ||
); | ||
|
||
export const normalizeDiff = (diff: { value: string }) => { | ||
let normalizedStr: string = diff.value; | ||
if (CURRENT_CWD.startsWith("/")) { | ||
normalizedStr = normalizedStr.replace( | ||
new RegExp(quoteMeta(CURRENT_CWD), "g"), | ||
"<cwd>" | ||
); | ||
} else { | ||
normalizedStr = normalizedStr.replace( | ||
cwdRegExp, | ||
(_, g) => `<cwd>${g.replace(/\\/g, "/")}` | ||
); | ||
normalizedStr = normalizedStr.replace( | ||
escapedCwdRegExp, | ||
(_, g) => `<cwd>${g.replace(/\\\\/g, "/")}` | ||
); | ||
} | ||
normalizedStr = normalizedStr.replace( | ||
/@@ -\d+,\d+ \+\d+,\d+ @@/g, | ||
"@@ ... @@" | ||
); | ||
return normalizedStr; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import prettyFormat from "pretty-format"; | ||
|
||
const ERROR_STACK_PATTERN = /(│.* at ).*/g; | ||
|
||
const prettyFormatOptions = { | ||
escapeRegex: false, | ||
printFunctionName: false, | ||
plugins: [ | ||
{ | ||
test(val: any) { | ||
return typeof val === "string"; | ||
}, | ||
print(val: any) { | ||
return `"${val | ||
.replace(/\\/gm, "/") | ||
.replace(/"/gm, '\\"') | ||
.replace(/\r?\n/gm, "\\n")}"`; | ||
} | ||
} | ||
] | ||
}; | ||
|
||
function cleanErrorStack(message: string) { | ||
return message.replace(ERROR_STACK_PATTERN, "$1xxx"); | ||
} | ||
|
||
function cleanError(err: Error) { | ||
const result: Partial<Record<keyof Error, any>> = {}; | ||
for (const key of Object.getOwnPropertyNames(err)) { | ||
result[key as keyof Error] = err[key as keyof Error]; | ||
} | ||
|
||
if (result.message) { | ||
result.message = cleanErrorStack(err.message); | ||
} | ||
|
||
if (result.stack) { | ||
result.stack = cleanErrorStack(result.stack); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
export function normalizeDignostics(received: { | ||
errors: Error[]; | ||
warnings: Error[]; | ||
}): string { | ||
return prettyFormat( | ||
{ | ||
errors: received.errors.map(e => cleanError(e)), | ||
warnings: received.warnings.map(e => cleanError(e)) | ||
}, | ||
prettyFormatOptions | ||
).trim(); | ||
} | ||
|
||
export function normalizeError(received: Error): string { | ||
return prettyFormat(cleanError(received), prettyFormatOptions).trim(); | ||
} |
30 changes: 30 additions & 0 deletions
30
packages/rspack-test-tools/src/helper/expect/placeholder.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import path from "node:path"; | ||
const { createSnapshotSerializer } = require("path-serializer"); | ||
|
||
const placeholderSerializer = createSnapshotSerializer({ | ||
workspace: path.resolve(__dirname, "../../../../../"), | ||
replace: [ | ||
{ | ||
match: path.resolve(__dirname, "../../../rspack"), | ||
mark: "rspack" | ||
}, | ||
{ | ||
match: path.resolve(__dirname, "../../"), | ||
mark: "test_tools" | ||
}, | ||
{ | ||
match: /:\d+:\d+-\d+:\d+/g, | ||
mark: "line_col_range" | ||
}, | ||
{ | ||
match: /:\d+:\d+/g, | ||
mark: "line_col" | ||
} | ||
], | ||
features: { | ||
addDoubleQuotes: false, | ||
ansiDoubleQuotes: false | ||
} | ||
}); | ||
|
||
export const normalizePlaceholder = placeholderSerializer.serialize; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export const normalizeStats = (stats: { value: string }): string => { | ||
return ( | ||
stats.value | ||
// CHANGE: Remove potential line break and "|" caused by long text | ||
.replace(/((ERROR|WARNING)([\s\S](?!╭|├))*?)(\n {2}│ )/g, "$1") | ||
// CHANGE: Update the regular expression to replace the 'Rspack' version string | ||
.replace(/Rspack [^ )]+(\)?) compiled/g, "Rspack x.x.x$1 compiled") | ||
.replace(/(\w)\\(\w)/g, "$1/$2") | ||
.replace(/, additional resolving: X ms/g, "") | ||
.replace(/Unexpected identifier '.+?'/g, "Unexpected identifier") | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,81 @@ | ||
// @ts-nocheck | ||
|
||
import { normalizeCLR, normalizeCRLF, normalizeSlash } from "./expect/char"; | ||
import { normalizeDiff } from "./expect/diff"; | ||
import { normalizeDignostics, normalizeError } from "./expect/error"; | ||
import { normalizePlaceholder } from "./expect/placeholder"; | ||
import { normalizeStats } from "./expect/rspack"; | ||
import { toBeTypeOf } from "./expect/to-be-typeof"; | ||
import { toEndWith } from "./expect/to-end-with"; | ||
import { toMatchFileSnapshot } from "./expect/to-match-file-snapshot"; | ||
const { normalizePaths } = require("jest-serializer-path"); | ||
|
||
expect.extend({ | ||
// CHANGE: new test matcher for `rspack-test-tools` | ||
// @ts-ignore | ||
toMatchFileSnapshot, | ||
toBeTypeOf, | ||
toEndWith | ||
}); | ||
|
||
const pipes = [ | ||
normalizeSlash, | ||
normalizeCLR, | ||
normalizeCRLF, | ||
normalizePlaceholder, | ||
normalizePaths | ||
]; | ||
|
||
const serialize = ( | ||
str: string, | ||
extra: Array<(str: string) => string> = [] | ||
): string => | ||
[...pipes, ...extra].reduce((res, transform) => transform(res), str); | ||
|
||
expect.addSnapshotSerializer({ | ||
test(received) { | ||
return typeof received === "string"; | ||
}, | ||
print(received) { | ||
return serialize((received as string).trim()); | ||
} | ||
}); | ||
|
||
// for diff | ||
expect.addSnapshotSerializer({ | ||
test(received) { | ||
return received?.constructor?.name === "RspackTestDiff"; | ||
}, | ||
print(received, next) { | ||
return next(normalizeDiff(received as { value: string })); | ||
} | ||
}); | ||
|
||
// for errors | ||
expect.addSnapshotSerializer({ | ||
test(received) { | ||
return received?.constructor?.name === "RspackStatsDiagnostics"; | ||
}, | ||
print(received, next) { | ||
return next( | ||
normalizeDignostics(received as { errors: Error[]; warnings: Error[] }) | ||
); | ||
} | ||
}); | ||
|
||
expect.addSnapshotSerializer({ | ||
test(received) { | ||
return typeof received?.message === "string"; | ||
}, | ||
print(received, next) { | ||
return next(normalizeError(received as Error)); | ||
} | ||
}); | ||
|
||
// for stats | ||
expect.addSnapshotSerializer({ | ||
test(received) { | ||
return received?.constructor?.name === "RspackStats"; | ||
}, | ||
print(received, next) { | ||
return next(normalizeStats(received as { value: string })); | ||
} | ||
}); |
Oops, something went wrong.
5a6162c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 Benchmark detail: Open
5a6162c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 Ran ecosystem CI: Open