-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathvitest.setup.ts
More file actions
75 lines (65 loc) · 2.19 KB
/
vitest.setup.ts
File metadata and controls
75 lines (65 loc) · 2.19 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import * as matchers from 'jest-extended';
import { expect } from 'vitest';
expect.extend(matchers);
expect.extend({
toBeBigDecimalCloseTo(
received: string,
expected: number | string,
precision = 2,
) {
const numValue = Number(received);
const pass =
!Number.isNaN(numValue) &&
Math.abs(numValue - Number(expected)) < 10 ** -precision;
return {
pass,
message: () =>
pass
? `expected "${received}" not to be close to ${expected}`
: `expected "${received}" to be close to ${expected}, but got difference of ${Math.abs(numValue - Number(expected))}`,
};
},
toBeBigDecimalWithin(received: string, start: number, end: number) {
const numValue = Number(received);
const pass =
!Number.isNaN(numValue) && numValue >= start && numValue <= end;
return {
pass,
message: () =>
pass
? `expected "${received}" not to be within range [${start}, ${end}]`
: `expected "${received}" to be within range [${start}, ${end}], but got ${numValue}`,
};
},
toBeBigDecimalGreaterThan(received: string, expected: number | string) {
const numValue = Number(received);
const pass = !Number.isNaN(numValue) && numValue > Number(expected);
return {
pass,
message: () =>
pass
? `expected "${received}" not to be greater than ${expected}`
: `expected "${received}" to be greater than ${expected}, but got ${numValue}`,
};
},
toBeBetweenDates(received: string, start: Date, end: Date) {
const receivedDate = new Date(received);
const pass =
!Number.isNaN(receivedDate.getTime()) &&
receivedDate >= start &&
receivedDate <= end;
return {
pass,
message: () =>
pass
? `expected "${received}" not to be between ${start.toISOString()} and ${end.toISOString()}`
: `expected "${received}" to be between ${start.toISOString()} and ${end.toISOString()}, but got ${receivedDate.toISOString()}`,
};
},
toBeHexString: (received) => {
return {
pass: /^0x[a-fA-F0-9]+$/.test(received),
message: () => `expected ${received} to be an hex string (0x…)`,
};
},
});