forked from tsdjs/tsd
/
utils.ts
76 lines (68 loc) · 1.85 KB
/
utils.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
64
65
66
67
68
69
70
71
72
73
74
75
76
import * as path from 'path';
import {ExecutionContext} from 'ava';
import {Diagnostic} from '../../lib/interfaces';
type Expectation = [
line: number,
column: number,
severity: 'error' | 'warning',
message: string,
];
type ExpectationWithFileName = [
line: number,
column: number,
severity: 'error' | 'warning',
message: string,
fileName: string,
];
/**
* Verify a list of diagnostics.
*
* @param t - The AVA execution context.
* @param diagnostics - List of diagnostics to verify.
* @param expectations - Expected diagnostics.
*/
export const verify = (t: ExecutionContext, diagnostics: Diagnostic[], expectations: Expectation[]) => {
const diagnosticObjs = diagnostics.map(({line, column, severity, message}) => ({
line,
column,
severity,
message,
}));
const expectationObjs = expectations.map(([line, column, severity, message]) => ({
line,
column,
severity,
message,
}));
t.deepEqual(diagnosticObjs, expectationObjs, 'Received diagnostics that are different from expectations!');
};
/**
* Verify a list of diagnostics including file paths.
*
* @param t - The AVA execution context.
* @param cwd - The working directory as passed to `tsd`.
* @param diagnostics - List of diagnostics to verify.
* @param expectations - Expected diagnostics.
*/
export const verifyWithFileName = (
t: ExecutionContext,
cwd: string,
diagnostics: Diagnostic[],
expectations: ExpectationWithFileName[]
) => {
const diagnosticObjs = diagnostics.map(({line, column, severity, message, fileName}) => ({
line,
column,
severity,
message,
fileName: path.relative(cwd, fileName),
}));
const expectationObjs = expectations.map(([line, column, severity, message, fileName]) => ({
line,
column,
severity,
message,
fileName,
}));
t.deepEqual(diagnosticObjs, expectationObjs, 'Received diagnostics that are different from expectations!');
};