@@ -21,25 +21,24 @@ const {UnmatchedFile} = require('./collect-files');
21
21
22
22
/**
23
23
* Exits Mocha when tests + code under test has finished execution (default)
24
- * @param {number } code - Exit code; typically # of failures
24
+ * @param {number } clampedCode - Exit code; typically # of failures
25
25
* @ignore
26
26
* @private
27
27
*/
28
- const exitMochaLater = code => {
28
+ const exitMochaLater = clampedCode => {
29
29
process . on ( 'exit' , ( ) => {
30
- process . exitCode = Math . min ( code , 255 ) ;
30
+ process . exitCode = clampedCode ;
31
31
} ) ;
32
32
} ;
33
33
34
34
/**
35
35
* Exits Mocha when Mocha itself has finished execution, regardless of
36
36
* what the tests or code under test is doing.
37
- * @param {number } code - Exit code; typically # of failures
37
+ * @param {number } clampedCode - Exit code; typically # of failures
38
38
* @ignore
39
39
* @private
40
40
*/
41
- const exitMocha = code => {
42
- const clampedCode = Math . min ( code , 255 ) ;
41
+ const exitMocha = clampedCode => {
43
42
let draining = 0 ;
44
43
45
44
// Eagerly set the process's exit code in case stream.write doesn't
@@ -139,12 +138,17 @@ const handleUnmatchedFiles = (mocha, unmatchedFiles) => {
139
138
* @param {Mocha } mocha - Mocha instance
140
139
* @param {Options } [opts] - Command line options
141
140
* @param {boolean } [opts.exit] - Whether or not to force-exit after tests are complete
141
+ * @param {boolean } [opts.passOnFailingTestSuite] - Whether or not to fail test run if tests were failed
142
142
* @param {Object } fileCollectParams - Parameters that control test
143
143
* file collection. See `lib/cli/collect-files.js`.
144
144
* @returns {Promise<Runner> }
145
145
* @private
146
146
*/
147
- const singleRun = async ( mocha , { exit} , fileCollectParams ) => {
147
+ const singleRun = async (
148
+ mocha ,
149
+ { exit, passOnFailingTestSuite} ,
150
+ fileCollectParams
151
+ ) => {
148
152
const fileCollectionObj = collectFiles ( fileCollectParams ) ;
149
153
150
154
if ( fileCollectionObj . unmatchedFiles . length > 0 ) {
@@ -156,7 +160,9 @@ const singleRun = async (mocha, {exit}, fileCollectParams) => {
156
160
157
161
// handles ESM modules
158
162
await mocha . loadFilesAsync ( ) ;
159
- return mocha . run ( exit ? exitMocha : exitMochaLater ) ;
163
+ return mocha . run (
164
+ createExitHandler ( { exit, passOnFailingTestSuite} )
165
+ ) ;
160
166
} ;
161
167
162
168
/**
@@ -186,7 +192,9 @@ const parallelRun = async (mocha, options, fileCollectParams) => {
186
192
mocha . files = fileCollectionObj . files ;
187
193
188
194
// note that we DO NOT load any files here; this is handled by the worker
189
- return mocha . run ( options . exit ? exitMocha : exitMochaLater ) ;
195
+ return mocha . run (
196
+ createExitHandler ( options )
197
+ ) ;
190
198
} ;
191
199
192
200
/**
@@ -282,3 +290,15 @@ exports.validateLegacyPlugin = (opts, pluginType, map = {}) => {
282
290
}
283
291
}
284
292
} ;
293
+
294
+ const createExitHandler = ( { exit, passOnFailingTestSuite } ) => {
295
+ return code => {
296
+ const clampedCode = passOnFailingTestSuite
297
+ ? 0
298
+ : Math . min ( code , 255 ) ;
299
+
300
+ return exit
301
+ ? exitMocha ( clampedCode )
302
+ : exitMochaLater ( clampedCode ) ;
303
+ } ;
304
+ } ;
0 commit comments