-
Notifications
You must be signed in to change notification settings - Fork 443
/
setupFiles.ts
54 lines (47 loc) · 1.7 KB
/
setupFiles.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
42
43
44
45
46
47
48
49
50
51
52
53
54
import { expect } from 'vitest';
const dateRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(.\d{1,6})?Z$/;
const dateRegexWithTZ = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(.\d{1,6})?\+\d{2}:\d{2}$/;
const uuidRegex = /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i;
const sha256Regex = /^[a-f0-9]{64}$/;
expect.extend({
toBeIsoDate: (received: any) => {
if (received instanceof Date) {
return { pass: true, message: () => '' };
} else if (typeof received === 'string' && dateRegex.test(received)) {
return { pass: true, message: () => '' };
}
return {
message: () => `expected ${received} to be an ISO Date`,
pass: false
};
},
toBeIsoDateTimezone: (received: any) => {
if (received instanceof Date) {
return { pass: true, message: () => '' };
} else if (typeof received === 'string' && dateRegexWithTZ.test(received)) {
return { pass: true, message: () => '' };
}
return {
message: () => `expected ${received} to be an ISO Date`,
pass: false
};
},
toBeUUID: (received: any) => {
if (!uuidRegex.test(received)) {
return {
message: () => `expected ${received} to be a UUID v4`,
pass: false
};
}
return { pass: true, message: () => '' };
},
toBeSha256: (received: any) => {
if (!sha256Regex.test(received)) {
return {
message: () => `expected ${received} to be a valid SHA-256 hash`,
pass: false
};
}
return { pass: true, message: () => '' };
}
});