-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestSetupFile.ts
53 lines (46 loc) · 1.19 KB
/
testSetupFile.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
import * as fs from 'fs';
import {
matcherHint,
MatcherHintOptions,
printExpected,
printReceived,
} from 'jest-matcher-utils';
function hintMessage(
matcherName: string,
expected: any,
received: any,
options?: MatcherHintOptions,
) {
return (
matcherHint(matcherName, undefined, undefined, options) +
'\n\n' +
`Expected: ${printExpected(expected)}\n` +
`Received: ${printReceived(received)}`
);
}
function registerFsMatchers() {
expect.extend({
toHaveBeenCreated(path: string, expectedContent?: string) {
const exists = fs.existsSync(path);
let receivedContent = '';
let sameContent = true;
if (expectedContent) {
receivedContent = fs.readFileSync(path).toString();
sameContent = Object.is(receivedContent, expectedContent);
}
const message = !exists
? `Failed asserting exists in path "${path}"`
: hintMessage('toHaveBeenCreated', expectedContent, receivedContent, {
isNot: this.isNot,
promise: this.promise,
});
return {
message: () => message,
pass: exists && sameContent,
};
},
});
}
beforeAll(() => {
registerFsMatchers();
});