-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
46 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,33 @@ | ||
export function transformTestPattern({ testName, isEach }: { testName: string; isEach: boolean }): string { | ||
// Vitest's test name pattern is a regex, so we need to escape any special regex characters. | ||
// Additionally, when a custom start process is not used on Windows, child_process.spawn is used with shell: true. | ||
// That disables automatic quoting/escaping of arguments, requiring us to manually perform that here as well. | ||
let result = testName.replace(/[$^+?()[\]"]/g, '\\$&') | ||
|
||
function escapeRegExp(str: string) { | ||
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\\\^\|\.\$]/g, "\\$&"); | ||
Check failure on line 3 in src/pure/testName.ts GitHub Actions / build (20.x)
|
||
} | ||
|
||
const kReplacers = new Map<string, string>([ | ||
['%i', '\\d+?'], | ||
['%#', '\\d+?'], | ||
['%d', '[\\d.eE+-]+?'], | ||
['%f', '[\\d.eE+-]+?'], | ||
['%s', '.+?'], | ||
['%j', '.+?'], | ||
['%o', '.+?'], | ||
['%%', '%'], | ||
]) | ||
|
||
export function transformTestPattern( | ||
{ testName, isEach }: | ||
{ testName: string; isEach: boolean }): string { | ||
// https://vitest.dev/api/#test-each | ||
// replace vitest's table test placeholder and treat it as regex | ||
let result = testName; | ||
if (isEach) { | ||
// replace \$value, \$obj.a with .+? | ||
result = result.replace(/\\\$[a-zA-Z_.]+/g, '.+?') | ||
// Integer or index of test case | ||
result = result.replace(/%[i#]/g, () => '\\d+?') | ||
// Float | ||
result = result.replace(/%[df]/g, () => '[\\d.eE+-]+?') | ||
// Arbitrary string | ||
result = result.replace(/%[sjo]/g, () => '.+?') | ||
// Single percent sign | ||
result = result.replace(/%%/g, () => '%') | ||
// Replace object access patterns ($value, $obj.a) with %s first | ||
result = result.replace(/\$[a-zA-Z_.]+/g, '%s') | ||
result = escapeRegExp(result) | ||
// Replace percent placeholders with their respective regex | ||
result = result.replace(/%[i#dfsjo%]/g, (m) => kReplacers.get(m) || m) | ||
} else { | ||
result = escapeRegExp(result) | ||
} | ||
return result | ||
} |
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