-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
toMatchDocument.ts
63 lines (56 loc) · 1.51 KB
/
toMatchDocument.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
55
56
57
58
59
60
61
62
63
import { checkDocument, print } from '../../utilities';
import type { DocumentNode } from '../../core';
import type { MatcherFunction } from 'expect';
export const toMatchDocument: MatcherFunction<[document: DocumentNode]> =
function (actual, document) {
const hint = this.utils.matcherHint('toMatchDocument');
const actualDocument = print(
validateDocument(
actual,
hint +
`\n\n${this.utils.RECEIVED_COLOR(
'received'
)} document must be a parsed document.`
)
);
const expectedDocument = print(
validateDocument(
document,
hint +
`\n\n${this.utils.EXPECTED_COLOR(
'expected'
)} document must be a parsed document.`
)
);
const pass = actualDocument === expectedDocument;
return {
pass,
message: () => {
const hint = this.utils.matcherHint(
'toMatchDocument',
undefined,
undefined,
{ isNot: this.isNot }
);
if (pass) {
return (
hint +
'\n\n' +
'Received:\n\n' +
this.utils.RECEIVED_COLOR(actualDocument)
);
}
return (
hint + '\n\n' + this.utils.diff(expectedDocument, actualDocument)
);
},
};
};
function validateDocument(document: unknown, message: string) {
try {
checkDocument(document as DocumentNode);
} catch (e) {
throw new Error(message);
}
return document as DocumentNode;
}