-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathTestResultProvider.ts
More file actions
111 lines (93 loc) · 3.19 KB
/
TestResultProvider.ts
File metadata and controls
111 lines (93 loc) · 3.19 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
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
import { TestReconciler, JestTotalResults } from 'jest-editor-support'
import { TestFileAssertionStatus } from 'jest-editor-support'
import { TestReconciliationState } from './TestReconciliationState'
import { TestResult } from './TestResult'
import { parseTest } from '../TestParser'
type TestResultsMap = { [filePath: string]: TestResult[] }
export type SortedTestResults = {
fail: TestResult[]
skip: TestResult[]
success: TestResult[]
unknown: TestResult[]
}
type SortedTestResultsMap = { [filePath: string]: SortedTestResults }
export class TestResultProvider {
private reconciler: TestReconciler
private resultsByFilePath: TestResultsMap
private sortedResultsByFilePath: SortedTestResultsMap
constructor() {
this.reconciler = new TestReconciler()
this.resetCache()
}
resetCache() {
this.resultsByFilePath = {}
this.sortedResultsByFilePath = {}
}
getResults(filePath: string): TestResult[] {
if (this.resultsByFilePath[filePath]) {
return this.resultsByFilePath[filePath]
}
const { itBlocks } = parseTest(filePath)
const results = this.reconciler.assertionsForTestFile(filePath) || []
const result: TestResult[] = []
for (const test of itBlocks) {
const assertion =
results.filter(result => result.line >= test.start.line && result.line <= test.end.line)[0] ||
results.filter(
result => result.title === test.name && result.status !== TestReconciliationState.KnownFail
)[0] ||
({} as any)
// Note the shift from one-based to zero-based line number and columns
result.push({
name: test.name,
start: {
column: test.start.column - 1,
line: test.start.line - 1,
},
end: {
column: test.end.column - 1,
line: test.end.line - 1,
},
status: assertion.status || TestReconciliationState.Unknown,
shortMessage: assertion.shortMessage,
terseMessage: assertion.terseMessage,
lineNumberOfError: assertion.line ? assertion.line - 1 : undefined,
})
}
this.resultsByFilePath[filePath] = result
return result
}
getSortedResults(filePath: string) {
if (this.sortedResultsByFilePath[filePath]) {
return this.sortedResultsByFilePath[filePath]
}
const result: SortedTestResults = {
fail: [],
skip: [],
success: [],
unknown: [],
}
const testResults = this.getResults(filePath)
for (const test of testResults) {
if (test.status === TestReconciliationState.KnownFail) {
result.fail.push(test)
} else if (test.status === TestReconciliationState.KnownSkip) {
result.skip.push(test)
} else if (test.status === TestReconciliationState.KnownSuccess) {
result.success.push(test)
} else {
result.unknown.push(test)
}
}
this.sortedResultsByFilePath[filePath] = result
return result
}
updateTestResults(data: JestTotalResults): TestFileAssertionStatus[] {
this.resetCache()
return this.reconciler.updateFileWithJestStatus(data)
}
removeCachedResults(filePath: string) {
this.resultsByFilePath[filePath] = null
this.sortedResultsByFilePath[filePath] = null
}
}