@@ -3,22 +3,21 @@ const path = require('path')
33const { 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 */
1820class 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 ,
@@ -39,43 +41,85 @@ class Result {
3941 duration : 0 ,
4042 }
4143
42- /** @type {CodeceptJS.Test[] } */
44+ /**
45+ * @type {CodeceptJS.Test[] }
46+ * @private
47+ */
4348 this . _tests = [ ]
4449
45- /** @type {String[] } */
50+ /**
51+ * @type {string[][] }
52+ * @private
53+ */
4654 this . _failures = [ ]
4755 }
4856
57+ /**
58+ * Sets the start time to the current time.
59+ */
4960 start ( ) {
5061 this . _startTime = new Date ( )
5162 }
5263
64+ /**
65+ * Sets the end time to the current time.
66+ */
5367 finish ( ) {
5468 this . _endTime = new Date ( )
5569 }
5670
71+ /**
72+ * Whether this result contains any failed tests.
73+ *
74+ * @type {boolean }
75+ * @readonly
76+ */
5777 get hasFailed ( ) {
5878 return this . _stats . failures > 0
5979 }
6080
81+ /**
82+ * All collected tests.
83+ *
84+ * @type {CodeceptJS.Test[] }
85+ * @readonly
86+ */
6187 get tests ( ) {
6288 return this . _tests
6389 }
6490
91+ /**
92+ * The failure reports (array of strings per failed test).
93+ *
94+ * @type {string[][] }
95+ * @readonly
96+ */
6597 get failures ( ) {
6698 return this . _failures . filter ( f => f && ( ! Array . isArray ( f ) || f . length > 0 ) )
6799 }
68100
101+ /**
102+ * The test statistics.
103+ *
104+ * @type {Stats }
105+ * @readonly
106+ */
69107 get stats ( ) {
70108 return this . _stats
71109 }
72110
111+ /**
112+ * The start time of the test run.
113+ *
114+ * @type {Date }
115+ * @readonly
116+ */
73117 get startTime ( ) {
74118 return this . _startTime
75119 }
76120
77121 /**
78- * Add test to result
122+ * Adds a test to this result.
79123 *
80124 * @param {CodeceptJS.Test } test
81125 */
@@ -90,34 +134,67 @@ class Result {
90134 }
91135
92136 /**
93- * Add failures to result
137+ * Adds failure reports to this result.
94138 *
95- * @param {String [] } newFailures
139+ * @param {string[] [] } newFailures
96140 */
97141 addFailures ( newFailures ) {
98142 this . _failures . push ( ...newFailures )
99143 }
100144
145+ /**
146+ * Whether this result contains any failed tests.
147+ *
148+ * @type {boolean }
149+ * @readonly
150+ */
101151 get hasFailures ( ) {
102152 return this . stats . failures > 0
103153 }
104154
155+ /**
156+ * The duration of the test run, in milliseconds.
157+ *
158+ * @type {number }
159+ * @readonly
160+ */
105161 get duration ( ) {
106162 return this . _endTime ? + this . _endTime - + this . _startTime : 0
107163 }
108164
165+ /**
166+ * All failed tests.
167+ *
168+ * @type {CodeceptJS.Test[] }
169+ * readonly
170+ */
109171 get failedTests ( ) {
110172 return this . _tests . filter ( test => test . state === 'failed' )
111173 }
112174
175+ /**
176+ * All passed tests.
177+ *
178+ * @type {CodeceptJS.Test[] }
179+ * @readonly
180+ */
113181 get passedTests ( ) {
114182 return this . _tests . filter ( test => test . state === 'passed' )
115183 }
116184
185+ /**
186+ * All skipped tests.
187+ *
188+ * @type {CodeceptJS.Test[] }
189+ * @readonly
190+ */
117191 get skippedTests ( ) {
118192 return this . _tests . filter ( test => test . state === 'skipped' || test . state === 'pending' )
119193 }
120194
195+ /**
196+ * @returns {object } The JSON representation of this result.
197+ */
121198 simplify ( ) {
122199 return {
123200 hasFailed : this . hasFailed ,
@@ -129,19 +206,19 @@ class Result {
129206 }
130207
131208 /**
132- * Save result to json file
209+ * Saves this result to a JSON file.
133210 *
134- * @param {string } fileName
211+ * @param {string } [ fileName] Path to the JSON file, relative to `output_dir`. Defaults to "result.json".
135212 */
136213 save ( fileName ) {
137214 if ( ! fileName ) fileName = 'result.json'
138215 fs . writeFileSync ( path . join ( global . output_dir , fileName ) , JSON . stringify ( this . simplify ( ) , null , 2 ) )
139216 }
140217
141218 /**
142- * Add stats to result
219+ * Adds stats to this result.
143220 *
144- * @param {object } newStats
221+ * @param {Partial<Stats> } [ newStats]
145222 */
146223 addStats ( newStats = { } ) {
147224 this . _stats . passes += newStats . passes || 0
0 commit comments