Skip to content

Commit

Permalink
Update testName.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
keroxp committed Feb 19, 2024
1 parent 1d4a48c commit ed2bfac
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/pure/testName.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

function escapeRegExp(str: string) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\\\^\|\.\$]/g, "\\$&");
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\\\^\|\.\$]/g, '\\$&')
}

const kReplacers = new Map<string, string>([
Expand All @@ -16,17 +15,19 @@ const kReplacers = new Map<string, string>([

export function transformTestPattern(
{ testName, isEach }:
{ testName: string; isEach: boolean }): string {
{ 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;
let result = testName
if (isEach) {
// 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 = result.replace(/%[i#dfsjo%]/g, m => kReplacers.get(m) || m)
}
else {
result = escapeRegExp(result)
}
return result
Expand Down

0 comments on commit ed2bfac

Please sign in to comment.