/
utils.ts
141 lines (126 loc) · 3.62 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import 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,
];
type ExpectationWithDiff = [
line: number,
column: number,
severity: 'error' | 'warning',
message: string,
diff: {
expected: string;
received: 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!');
};
/**
* Verify a list of diagnostics including diff.
*
* @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 verifyWithDiff = (
t: ExecutionContext,
diagnostics: Diagnostic[],
expectations: ExpectationWithDiff[]
) => {
const diagnosticObjs = diagnostics.map(({line, column, severity, message, diff}) => ({
line,
column,
severity,
message,
diff
}));
const expectationObjs = expectations.map(([line, column, severity, message, diff]) => ({
line,
column,
severity,
message,
diff,
}));
t.deepEqual(diagnosticObjs, expectationObjs, 'Received diagnostics that are different from expectations!');
};
/**
* Verify a list of diagnostics reported from the CLI.
*
* @param t - The AVA execution context.
* @param diagnostics - List of diagnostics to verify.
* @param expectations - Expected diagnostics.
* @param startLine - Optionally specify how many lines to skip from start.
*/
export const verifyCli = (
t: ExecutionContext,
diagnostics: string,
expectedLines: string[],
{startLine = 1}: {startLine: number} // Skip file location.
) => {
// NOTE: If lines are added to the output in the future `startLine` and `endLine` should be adjusted.
const endLine = startLine + expectedLines.length; // Grab diff output only and skip stack trace.
const receivedLines = diagnostics.trim().split('\n').slice(startLine, endLine).map(line => line.trim());
t.deepEqual(receivedLines, expectedLines, 'Received diagnostics that are different from expectations!');
};