@@ -13,13 +13,24 @@ const config = `--config ${codecept_dir}/codecept.conf.js`
1313const outputDir = path . join ( codecept_dir , 'output' )
1414const resultFile = path . join ( outputDir , 'result.json' )
1515
16+ const hooks_dir = path . join ( __dirname , '/../data/sandbox/configs/last-failed-hooks' )
17+ const hooksConfig = `--config ${ hooks_dir } /codecept.conf.js`
18+ const hooksResultFile = path . join ( hooks_dir , 'output' , 'result.json' )
19+
1620const run = ( command , env = { } ) =>
1721 new Promise ( resolve => {
1822 exec ( `${ runner } ${ command } ${ config } ` , { env : { ...process . env , LAST_FAILED_FIXED : '' , ...env } } , ( err , stdout ) => {
1923 resolve ( { code : err ? err . code : 0 , stdout } )
2024 } )
2125 } )
2226
27+ const runHooks = ( command , env = { } ) =>
28+ new Promise ( resolve => {
29+ exec ( `${ runner } ${ command } ${ hooksConfig } ` , { env : { ...process . env , LAST_FAILED_HOOKS_OK : '' , ...env } } , ( err , stdout ) => {
30+ resolve ( { code : err ? err . code : 0 , stdout } )
31+ } )
32+ } )
33+
2334const reportedTests = ( ) => {
2435 const { tests } = JSON . parse ( fs . readFileSync ( resultFile , 'utf8' ) )
2536 return Object . fromEntries ( tests . map ( test => [ test . title , test . state ] ) )
@@ -37,6 +48,7 @@ describe('--last-failed', function () {
3748
3849 beforeEach ( ( ) => {
3950 fs . rmSync ( outputDir , { recursive : true , force : true } )
51+ fs . rmSync ( path . join ( hooks_dir , 'output' ) , { recursive : true , force : true } )
4052 } )
4153
4254 it ( 'should save a report with test states after a run' , async ( ) => {
@@ -137,4 +149,51 @@ describe('--last-failed', function () {
137149 expect ( stdout ) . not . toContain ( 'passed' )
138150 }
139151 } )
152+
153+ it ( 'should not wipe result.json when a run executes no tests' , async ( ) => {
154+ await run ( 'run' )
155+ expect ( reportedTests ( ) [ 'multiplies numbers' ] ) . toEqual ( 'failed' )
156+
157+ const { stdout } = await run ( 'run --grep "no such test"' )
158+ expect ( executedTests ( stdout ) ) . toEqual ( [ ] )
159+
160+ expect ( reportedTests ( ) [ 'multiplies numbers' ] ) . toEqual ( 'failed' )
161+ const { stdout : rerun } = await run ( 'run --last-failed' )
162+ expect ( executedTests ( rerun ) ) . toEqual ( [ 'divides numbers' , 'multiplies numbers' , 'uppercases strings' ] )
163+ } )
164+
165+ it ( 'should not wipe result.json in workers when a run executes no tests' , async ( ) => {
166+ await run ( 'run-workers 2' )
167+ expect ( reportedTests ( ) [ 'multiplies numbers' ] ) . toEqual ( 'failed' )
168+
169+ const { stdout } = await run ( 'run-workers 2 --grep "no such test"' )
170+ expect ( executedTests ( stdout ) ) . toEqual ( [ ] )
171+
172+ expect ( reportedTests ( ) [ 'multiplies numbers' ] ) . toEqual ( 'failed' )
173+ } )
174+
175+ it ( 'should rerun tests of a suite with a failed BeforeSuite' , async ( ) => {
176+ const { code } = await runHooks ( 'run' )
177+ expect ( code ) . toEqual ( 1 )
178+ const { tests } = JSON . parse ( fs . readFileSync ( hooksResultFile , 'utf8' ) )
179+ expect ( Object . fromEntries ( tests . map ( test => [ test . title , test . state ] ) ) ) . toEqual ( {
180+ 'prepares data' : 'failed' ,
181+ 'serves requests @smoke' : 'failed' ,
182+ } )
183+
184+ const { stdout } = await runHooks ( 'run --last-failed' )
185+ expect ( executedTests ( stdout ) ) . toEqual ( [ 'prepares data' , 'serves requests' ] )
186+ } )
187+
188+ it ( 'should rerun tests of a suite with a failed BeforeSuite in workers' , async ( ) => {
189+ await runHooks ( 'run-workers 2' )
190+ const { tests } = JSON . parse ( fs . readFileSync ( hooksResultFile , 'utf8' ) )
191+ expect ( Object . fromEntries ( tests . map ( test => [ test . title , test . state ] ) ) ) . toEqual ( {
192+ 'prepares data' : 'failed' ,
193+ 'serves requests @smoke' : 'failed' ,
194+ } )
195+
196+ const { stdout } = await runHooks ( 'run-workers 2 --last-failed' )
197+ expect ( executedTests ( stdout ) ) . toEqual ( [ 'prepares data' , 'serves requests' ] )
198+ } )
140199} )
0 commit comments