-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
vitest.setup.ts
41 lines (35 loc) · 1.15 KB
/
vitest.setup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { expect, Assertion, AsymmetricMatchersContaining } from 'vitest';
interface CustomMatchers<R = unknown> {
toMatchMarkdown: (str: string) => R;
}
declare module 'vitest' {
interface Assertion<T = any> extends CustomMatchers<T> {}
interface AsymmetricMatchersContaining extends CustomMatchers {}
}
// Function to normalize whitespace
const normalizeWhitespace = (str: string) => str.replace(/\s+/g, ' ').trim();
declare global {
namespace vitest {
interface Assertion<T> {
toMatchMarkdown(expected: string): void;
}
}
}
// Custom matcher to compare Markdown strings ignoring whitespace differences
expect.extend({
toMatchMarkdown(received, expected) {
const normalizedReceived = normalizeWhitespace(received);
const normalizedExpected = normalizeWhitespace(expected);
if (normalizedReceived === normalizedExpected) {
return {
message: () => `expected ${received} not to match Markdown ${expected} ignoring whitespace`,
pass: true,
};
} else {
return {
message: () => `expected ${received} to match Markdown ${expected} ignoring whitespace`,
pass: false,
};
}
},
});