-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
Copy pathreporter.ts
161 lines (143 loc) · 4.42 KB
/
reporter.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {Config} from '@jest/types';
import {AssertionResult, TestResult} from '@jest/test-result';
import {formatResultsErrors} from 'jest-message-util';
import {SpecResult} from './jasmine/Spec';
import {SuiteResult} from './jasmine/Suite';
import {Reporter, RunDetails} from './types';
type Microseconds = number;
export default class Jasmine2Reporter implements Reporter {
private _testResults: Array<AssertionResult>;
private _globalConfig: Config.GlobalConfig;
private _config: Config.ProjectConfig;
private _currentSuites: Array<string>;
private _resolve: any;
private _resultsPromise: Promise<TestResult>;
private _startTimes: Map<string, Microseconds>;
private _testPath: Config.Path;
constructor(
globalConfig: Config.GlobalConfig,
config: Config.ProjectConfig,
testPath: Config.Path,
) {
this._globalConfig = globalConfig;
this._config = config;
this._testPath = testPath;
this._testResults = [];
this._currentSuites = [];
this._resolve = null;
this._resultsPromise = new Promise(resolve => (this._resolve = resolve));
this._startTimes = new Map();
}
jasmineStarted(_runDetails: RunDetails) {}
specStarted(spec: SpecResult) {
this._startTimes.set(spec.id, Date.now());
}
specDone(result: SpecResult): void {
this._testResults.push(
this._extractSpecResults(result, this._currentSuites.slice(0)),
);
}
suiteStarted(suite: SuiteResult): void {
this._currentSuites.push(suite.description);
}
suiteDone(_result: SuiteResult): void {
this._currentSuites.pop();
}
jasmineDone(_runDetails: RunDetails): void {
let numFailingTests = 0;
let numPassingTests = 0;
let numPendingTests = 0;
let numTodoTests = 0;
const testResults = this._testResults;
testResults.forEach(testResult => {
if (testResult.status === 'failed') {
numFailingTests++;
} else if (testResult.status === 'pending') {
numPendingTests++;
} else if (testResult.status === 'todo') {
numTodoTests++;
} else {
numPassingTests++;
}
});
const testResult = {
console: null,
failureMessage: formatResultsErrors(
testResults,
this._config,
this._globalConfig,
this._testPath,
),
numFailingTests,
numPassingTests,
numPendingTests,
numTodoTests,
perfStats: {
end: 0,
start: 0,
},
snapshot: {
added: 0,
fileDeleted: false,
matched: 0,
unchecked: 0,
unmatched: 0,
updated: 0,
},
testFilePath: this._testPath,
testResults,
};
this._resolve(testResult);
}
getResults(): Promise<TestResult> {
return this._resultsPromise;
}
private _addMissingMessageToStack(stack: string, message?: string) {
// Some errors (e.g. Angular injection error) don't prepend error.message
// to stack, instead the first line of the stack is just plain 'Error'
const ERROR_REGEX = /^Error:?\s*\n/;
if (stack && message && !stack.includes(message)) {
return message + stack.replace(ERROR_REGEX, '\n');
}
return stack;
}
private _extractSpecResults(
specResult: SpecResult,
ancestorTitles: Array<string>,
): AssertionResult {
const start = this._startTimes.get(specResult.id);
const duration = start ? Date.now() - start : undefined;
const status =
specResult.status === 'disabled' ? 'pending' : specResult.status;
const location = specResult.__callsite
? {
column: specResult.__callsite.getColumnNumber(),
line: specResult.__callsite.getLineNumber(),
}
: null;
const results: AssertionResult = {
ancestorTitles,
duration,
failureMessages: [],
fullName: specResult.fullName,
location,
numPassingAsserts: 0, // Jasmine2 only returns an array of failed asserts.
status,
title: specResult.description,
};
specResult.failedExpectations.forEach(failed => {
const message =
!failed.matcherName && failed.stack
? this._addMissingMessageToStack(failed.stack, failed.message)
: failed.message || '';
results.failureMessages.push(message);
});
return results;
}
}