@@ -26,15 +26,20 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
2626 } ) ;
2727
2828 it ( 'should succeed with a file' , async ( ) => {
29- const stream = run ( { files : [ join ( testFixtures , 'test/random.cjs' ) ] } ) ;
29+ const stream = run ( { files : [ join ( testFixtures , 'default-behavior/ test/random.cjs' ) ] } ) ;
3030 stream . on ( 'test:fail' , common . mustNotCall ( ) ) ;
3131 stream . on ( 'test:pass' , common . mustCall ( 1 ) ) ;
3232 // eslint-disable-next-line no-unused-vars
3333 for await ( const _ of stream ) ;
3434 } ) ;
3535
3636 it ( 'should run same file twice' , async ( ) => {
37- const stream = run ( { files : [ join ( testFixtures , 'test/random.cjs' ) , join ( testFixtures , 'test/random.cjs' ) ] } ) ;
37+ const stream = run ( {
38+ files : [
39+ join ( testFixtures , 'default-behavior/test/random.cjs' ) ,
40+ join ( testFixtures , 'default-behavior/test/random.cjs' ) ,
41+ ]
42+ } ) ;
3843 stream . on ( 'test:fail' , common . mustNotCall ( ) ) ;
3944 stream . on ( 'test:pass' , common . mustCall ( 2 ) ) ;
4045 // eslint-disable-next-line no-unused-vars
@@ -68,7 +73,9 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
6873 } ) ;
6974
7075 it ( 'should be piped with dot' , async ( ) => {
71- const result = await run ( { files : [ join ( testFixtures , 'test/random.cjs' ) ] } ) . compose ( dot ) . toArray ( ) ;
76+ const result = await run ( {
77+ files : [ join ( testFixtures , 'default-behavior/test/random.cjs' ) ]
78+ } ) . compose ( dot ) . toArray ( ) ;
7279 assert . deepStrictEqual ( result , [
7380 '.' ,
7481 '\n' ,
@@ -77,15 +84,19 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
7784
7885 it ( 'should be piped with spec' , async ( ) => {
7986 const specReporter = new spec ( ) ;
80- const result = await run ( { files : [ join ( testFixtures , 'test/random.cjs' ) ] } ) . compose ( specReporter ) . toArray ( ) ;
87+ const result = await run ( {
88+ files : [ join ( testFixtures , 'default-behavior/test/random.cjs' ) ]
89+ } ) . compose ( specReporter ) . toArray ( ) ;
8190 const stringResults = result . map ( ( bfr ) => bfr . toString ( ) ) ;
8291 assert . match ( stringResults [ 0 ] , / t h i s s h o u l d p a s s / ) ;
8392 assert . match ( stringResults [ 1 ] , / t e s t s 1 / ) ;
8493 assert . match ( stringResults [ 1 ] , / p a s s 1 / ) ;
8594 } ) ;
8695
8796 it ( 'should be piped with tap' , async ( ) => {
88- const result = await run ( { files : [ join ( testFixtures , 'test/random.cjs' ) ] } ) . compose ( tap ) . toArray ( ) ;
97+ const result = await run ( {
98+ files : [ join ( testFixtures , 'default-behavior/test/random.cjs' ) ]
99+ } ) . compose ( tap ) . toArray ( ) ;
89100 assert . strictEqual ( result . length , 13 ) ;
90101 assert . strictEqual ( result [ 0 ] , 'TAP version 13\n' ) ;
91102 assert . strictEqual ( result [ 1 ] , '# Subtest: this should pass\n' ) ;
@@ -103,24 +114,48 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
103114 } ) ;
104115
105116 it ( 'should skip tests not matching testNamePatterns - RegExp' , async ( ) => {
106- const result = await run ( { files : [ join ( testFixtures , 'test/skip_by_name.cjs' ) ] , testNamePatterns : [ / e x e c u t e d / ] } )
117+ const result = await run ( {
118+ files : [ join ( testFixtures , 'default-behavior/test/skip_by_name.cjs' ) ] ,
119+ testNamePatterns : [ / e x e c u t e d / ]
120+ } )
107121 . compose ( tap )
108122 . toArray ( ) ;
109123 assert . strictEqual ( result [ 2 ] , 'ok 1 - this should be skipped # SKIP test name does not match pattern\n' ) ;
110124 assert . strictEqual ( result [ 5 ] , 'ok 2 - this should be executed\n' ) ;
111125 } ) ;
112126
113127 it ( 'should skip tests not matching testNamePatterns - string' , async ( ) => {
114- const result = await run ( { files : [ join ( testFixtures , 'test/skip_by_name.cjs' ) ] , testNamePatterns : [ 'executed' ] } )
128+ const result = await run ( {
129+ files : [ join ( testFixtures , 'default-behavior/test/skip_by_name.cjs' ) ] ,
130+ testNamePatterns : [ 'executed' ]
131+ } )
115132 . compose ( tap )
116133 . toArray ( ) ;
117134 assert . strictEqual ( result [ 2 ] , 'ok 1 - this should be skipped # SKIP test name does not match pattern\n' ) ;
118135 assert . strictEqual ( result [ 5 ] , 'ok 2 - this should be executed\n' ) ;
119136 } ) ;
120137
121- it ( 'should stop watch mode when abortSignal aborts ' , async ( ) => {
138+ it ( 'should emit "test: watch:drained" event on watch mode ' , async ( ) => {
122139 const controller = new AbortController ( ) ;
123- const result = await run ( { files : [ join ( testFixtures , 'test/random.cjs' ) ] , watch : true , signal : controller . signal } )
140+ await run ( {
141+ files : [ join ( testFixtures , 'default-behavior/test/random.cjs' ) ] ,
142+ watch : true ,
143+ signal : controller . signal ,
144+ } ) . on ( 'data' , function ( { type } ) {
145+ if ( type === 'test:watch:drained' ) {
146+ controller . abort ( ) ;
147+ }
148+ } ) ;
149+ } ) ;
150+
151+ describe ( 'AbortSignal' , ( ) => {
152+ it ( 'should stop watch mode when abortSignal aborts' , async ( ) => {
153+ const controller = new AbortController ( ) ;
154+ const result = await run ( {
155+ files : [ join ( testFixtures , 'default-behavior/test/random.cjs' ) ] ,
156+ watch : true ,
157+ signal : controller . signal
158+ } )
124159 . compose ( async function * ( source ) {
125160 for await ( const chunk of source ) {
126161 if ( chunk . type === 'test:pass' ) {
@@ -130,17 +165,74 @@ describe('require(\'node:test\').run', { concurrency: true }, () => {
130165 }
131166 } )
132167 . toArray ( ) ;
133- assert . deepStrictEqual ( result , [ 'this should pass' ] ) ;
134- } ) ;
168+ assert . deepStrictEqual ( result , [ 'this should pass' ] ) ;
169+ } ) ;
135170
136- it ( 'should emit "test:watch:drained" event on watch mode' , async ( ) => {
137- const controller = new AbortController ( ) ;
138- await run ( { files : [ join ( testFixtures , 'test/random.cjs' ) ] , watch : true , signal : controller . signal } )
139- . on ( 'data' , function ( { type } ) {
140- if ( type === 'test:watch:drained' ) {
141- controller . abort ( ) ;
171+ it ( 'should abort when test succeeded' , async ( ) => {
172+ const stream = run ( {
173+ files : [
174+ fixtures . path (
175+ 'test-runner' ,
176+ 'aborts' ,
177+ 'successful-test-still-call-abort.js'
178+ ) ,
179+ ] ,
180+ } ) ;
181+
182+ let passedTestCount = 0 ;
183+ let failedTestCount = 0 ;
184+
185+ let output = '' ;
186+ for await ( const data of stream ) {
187+ if ( data . type === 'test:stdout' ) {
188+ output += data . data . message . toString ( ) ;
142189 }
190+ if ( data . type === 'test:fail' ) {
191+ failedTestCount ++ ;
192+ }
193+ if ( data . type === 'test:pass' ) {
194+ passedTestCount ++ ;
195+ }
196+ }
197+
198+ assert . match ( output , / a b o r t c a l l e d f o r t e s t 1 / ) ;
199+ assert . match ( output , / a b o r t c a l l e d f o r t e s t 2 / ) ;
200+ assert . strictEqual ( failedTestCount , 0 , new Error ( 'no tests should fail' ) ) ;
201+ assert . strictEqual ( passedTestCount , 2 ) ;
202+ } ) ;
203+
204+ it ( 'should abort when test failed' , async ( ) => {
205+ const stream = run ( {
206+ files : [
207+ fixtures . path (
208+ 'test-runner' ,
209+ 'aborts' ,
210+ 'failed-test-still-call-abort.js'
211+ ) ,
212+ ] ,
143213 } ) ;
214+
215+ let passedTestCount = 0 ;
216+ let failedTestCount = 0 ;
217+
218+ let output = '' ;
219+ for await ( const data of stream ) {
220+ if ( data . type === 'test:stdout' ) {
221+ output += data . data . message . toString ( ) ;
222+ }
223+ if ( data . type === 'test:fail' ) {
224+ failedTestCount ++ ;
225+ }
226+ if ( data . type === 'test:pass' ) {
227+ passedTestCount ++ ;
228+ }
229+ }
230+
231+ assert . match ( output , / a b o r t c a l l e d f o r t e s t 1 / ) ;
232+ assert . match ( output , / a b o r t c a l l e d f o r t e s t 2 / ) ;
233+ assert . strictEqual ( passedTestCount , 0 , new Error ( 'no tests should pass' ) ) ;
234+ assert . strictEqual ( failedTestCount , 2 ) ;
235+ } ) ;
144236 } ) ;
145237
146238 describe ( 'sharding' , ( ) => {
0 commit comments