Skip to content

Commit 0bca261

Browse files
committed
fixed: typings for class Result
1 parent 743d0a1 commit 0bca261

File tree

1 file changed

+63
-22
lines changed

1 file changed

+63
-22
lines changed

lib/result.js

Lines changed: 63 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,21 @@ const path = require('path')
33
const { serializeTest } = require('./mocha/test')
44

55
/**
6-
* Result of the test run
7-
*
8-
* @typedef {Object} Stats
9-
* @property {number} passes
10-
* @property {number} failures
11-
* @property {number} tests
12-
* @property {number} pending
13-
* @property {number} failedHooks
14-
* @property {Date} start
15-
* @property {Date} end
16-
* @property {number} duration
6+
* @typedef {Object} Stats Statistics for a test result.
7+
* @property {number} passes Number of passed tests.
8+
* @property {number} failures Number of failed tests.
9+
* @property {number} tests Total number of tests.
10+
* @property {number} pending Number of pending tests.
11+
* @property {number} failedHooks Number of failed hooks.
12+
* @property {Date} start Start time of the test run.
13+
* @property {Date} end End time of the test run.
14+
* @property {number} duration Duration of the test run, in milliseconds.
15+
*/
16+
17+
/**
18+
* Result of a test run. Will be emitted for example in "event.all.result" events.
1719
*/
1820
class Result {
19-
/**
20-
* Create Result of the test run
21-
*/
2221
constructor() {
2322
this._startTime = new Date()
2423
this._endTime = null
@@ -27,6 +26,9 @@ class Result {
2726
this.start()
2827
}
2928

29+
/**
30+
* Resets all collected stats, tests, and failure reports.
31+
*/
3032
reset() {
3133
this._stats = {
3234
passes: 0,
@@ -42,40 +44,61 @@ class Result {
4244
/** @type {CodeceptJS.Test[]} */
4345
this._tests = []
4446

45-
/** @type {String[]} */
47+
/** @type {string[][]} */
4648
this._failures = []
4749
}
4850

51+
/**
52+
* Sets the start time to the current time.
53+
*/
4954
start() {
5055
this._startTime = new Date()
5156
}
5257

58+
/**
59+
* Sets the end time to the current time.
60+
*/
5361
finish() {
5462
this._endTime = new Date()
5563
}
5664

65+
/**
66+
* @returns {boolean} Whether this result contains any failed tests.
67+
*/
5768
get hasFailed() {
5869
return this._stats.failures > 0
5970
}
6071

72+
/**
73+
* @returns {CodeceptJS.Test[]} All collected tests.
74+
*/
6175
get tests() {
6276
return this._tests
6377
}
6478

79+
/**
80+
* @returns {string[][]} The failure reports (array aof strings per failed test).
81+
*/
6582
get failures() {
6683
return this._failures.filter(f => f && (!Array.isArray(f) || f.length > 0))
6784
}
6885

86+
/**
87+
* @returns {Stats} The test statistics.
88+
*/
6989
get stats() {
7090
return this._stats
7191
}
7292

93+
/**
94+
* @returns {Date} The start time of the test run.
95+
*/
7396
get startTime() {
7497
return this._startTime
7598
}
7699

77100
/**
78-
* Add test to result
101+
* Adds a test to this result.
79102
*
80103
* @param {CodeceptJS.Test} test
81104
*/
@@ -90,34 +113,52 @@ class Result {
90113
}
91114

92115
/**
93-
* Add failures to result
116+
* Adds failure reports to this result.
94117
*
95-
* @param {String[]} newFailures
118+
* @param {string[][]} newFailures
96119
*/
97120
addFailures(newFailures) {
98121
this._failures.push(...newFailures)
99122
}
100123

124+
/**
125+
* @returns {boolean} Whether this result contains any failed tests.
126+
*/
101127
get hasFailures() {
102128
return this.stats.failures > 0
103129
}
104130

131+
/**
132+
* @returns {number} The duration of the test run, in milliseconds.
133+
*/
105134
get duration() {
106135
return this._endTime ? +this._endTime - +this._startTime : 0
107136
}
108137

138+
/**
139+
* @returns {CodeceptJS.Test[]} All failed tests.
140+
*/
109141
get failedTests() {
110142
return this._tests.filter(test => test.state === 'failed')
111143
}
112144

145+
/**
146+
* @returns {CodeceptJS.Test[]} All passed tests.
147+
*/
113148
get passedTests() {
114149
return this._tests.filter(test => test.state === 'passed')
115150
}
116151

152+
/**
153+
* @returns {CodeceptJS.Test[]} All skipped tests.
154+
*/
117155
get skippedTests() {
118156
return this._tests.filter(test => test.state === 'skipped' || test.state === 'pending')
119157
}
120158

159+
/**
160+
* @returns {object} The JSON representation of this result.
161+
*/
121162
simplify() {
122163
return {
123164
hasFailed: this.hasFailed,
@@ -129,19 +170,19 @@ class Result {
129170
}
130171

131172
/**
132-
* Save result to json file
173+
* Saves this result to a JSON file.
133174
*
134-
* @param {string} fileName
175+
* @param {string} [fileName] Path to the JSON file, relative to `output_dir`. Defaults to "result.json".
135176
*/
136177
save(fileName) {
137178
if (!fileName) fileName = 'result.json'
138179
fs.writeFileSync(path.join(global.output_dir, fileName), JSON.stringify(this.simplify(), null, 2))
139180
}
140181

141182
/**
142-
* Add stats to result
183+
* Adds stats to this result.
143184
*
144-
* @param {object} newStats
185+
* @param {Partial<Stats>} [newStats]
145186
*/
146187
addStats(newStats = {}) {
147188
this._stats.passes += newStats.passes || 0

0 commit comments

Comments
 (0)