-
-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathsetup.ts
More file actions
49 lines (42 loc) · 1.78 KB
/
setup.ts
File metadata and controls
49 lines (42 loc) · 1.78 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
import * as assert from "assert";
import * as ts from "typescript";
import * as tstl from "../src";
declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace jest {
// eslint-disable-next-line @typescript-eslint/naming-convention
interface Matchers<R, T> {
toHaveDiagnostics(expected?: number[]): R;
}
}
}
expect.extend({
toHaveDiagnostics(diagnostics: ts.Diagnostic[], expected?: number[]): jest.CustomMatcherResult {
assert(Array.isArray(diagnostics));
// @ts-ignore
const matcherHint = this.utils.matcherHint("toHaveDiagnostics", undefined, "", this);
const diagnosticMessages = ts.formatDiagnosticsWithColorAndContext(
diagnostics.map(tstl.prepareDiagnosticForFormatting),
{ getCurrentDirectory: () => "", getCanonicalFileName: fileName => fileName, getNewLine: () => "\n" }
);
if (this.isNot && expected !== undefined) {
throw new Error("expect(actual).not.toHaveDiagnostics(expected) is not supported");
}
return {
pass: expected
? diagnostics.length === expected.length &&
diagnostics.every((diag, index) => diag.code === expected[index])
: diagnostics.length > 0,
message: () => {
const message = this.isNot
? diagnosticMessages
: expected
? `Expected:\n${expected.join("\n")}\nReceived:\n${diagnostics
.map(diag => diag.code)
.join("\n")}\n\n${diagnosticMessages}\n`
: `Received: ${this.utils.printReceived([])}\n`;
return matcherHint + "\n\n" + message;
},
};
},
});