Skip to content

Commit f2deb7c

Browse files
committed
Ignore whitespace harder
1 parent 419ad3a commit f2deb7c

File tree

1 file changed

+17
-3
lines changed
  • packages/utils/graphql-codegen-testing/src

1 file changed

+17
-3
lines changed

packages/utils/graphql-codegen-testing/src/index.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,25 @@ function compareStrings(a: string, b: string): boolean {
1919
return a.includes(b);
2020
}
2121

22+
/** Ignore whitespace, trailing commas, and leading pipes */
23+
function similarize(str: string): string {
24+
return (
25+
oneLine`${str}`
26+
// Trim trailing commas
27+
.replace(/\s*,(\s*[)}])/g, '$1')
28+
// Remove leading pipes
29+
.replace(/([<:,=(])\s*(?:\|\s*)?/g, '$1')
30+
// Remove spaces around brackets and semicolons
31+
.replace(/\s*([[\](){}<>;])\s*/g, '$1')
32+
// Replace multiple spaces with a single space
33+
.replace(/\s\s+/g, ' ')
34+
);
35+
}
36+
2237
expect.extend({
2338
toBeSimilarStringTo(received: string, expected: string) {
24-
// Ignore whitespace and trailing commas
25-
const strippedReceived = oneLine`${received}`.replace(/\s\s+/g, ' ').replace(/\s*,(\s*[)}])/, '$1');
26-
const strippedExpected = oneLine`${expected}`.replace(/\s\s+/g, ' ').replace(/\s*,(\s*[)}])/, '$1');
39+
const strippedReceived = similarize(received);
40+
const strippedExpected = similarize(expected);
2741

2842
if (compareStrings(strippedReceived, strippedExpected)) {
2943
return {

0 commit comments

Comments
 (0)