1+ import os from 'os'
12import { expect } from 'chai'
23import { exec } from 'child_process'
34import path from 'path'
@@ -6,21 +7,18 @@ import { fileURLToPath } from 'url'
67
78const __filename = fileURLToPath ( import . meta. url )
89const __dirname = path . dirname ( __filename )
9- const codecept_run = `node ${ path . resolve ( __dirname , '../../bin/codecept.js' ) } `
10+ const codeceptRun = `" ${ path . resolve ( __dirname , '../../bin/codecept.js' ) } " `
1011
1112describe ( 'CLI Sharding Integration' , ( ) => {
1213 let tempDir
1314 let configFile
1415
1516 beforeEach ( ( ) => {
16- // Create temporary test setup
17- tempDir = `/tmp/shard_test_${ Date . now ( ) } `
17+ tempDir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , 'shard_test_' ) )
1818 configFile = path . join ( tempDir , 'codecept.conf.js' )
1919
20- // Create temp directory and test files
2120 fs . mkdirSync ( tempDir , { recursive : true } )
2221
23- // Create 4 test files
2422 for ( let i = 1 ; i <= 4 ; i ++ ) {
2523 fs . writeFileSync (
2624 path . join ( tempDir , `shard_test${ i } .js` ) ,
@@ -30,17 +28,16 @@ Feature('Shard Test ${i}')
3028Scenario('test ${ i } ', ({ I }) => {
3129 I.say('This is test ${ i } ')
3230})
33- ` ,
31+ ` ,
3432 )
3533 }
3634
37- // Create config file
3835 fs . writeFileSync (
3936 configFile ,
4037 `
4138exports.config = {
42- tests: ' ${ tempDir } / shard_test*.js',
43- output: ' ${ tempDir } / output',
39+ tests: ${ JSON . stringify ( path . join ( tempDir , ' shard_test*.js') ) } ,
40+ output: ${ JSON . stringify ( path . join ( tempDir , ' output') ) } ,
4441 helpers: {
4542 FileSystem: {}
4643 },
@@ -49,23 +46,18 @@ exports.config = {
4946 mocha: {},
5047 name: 'shard-test'
5148}
52- ` ,
49+ ` ,
5350 )
5451 } )
5552
5653 afterEach ( ( ) => {
57- // Cleanup temp files
58- try {
59- fs . rmSync ( tempDir , { recursive : true , force : true } )
60- } catch ( err ) {
61- // Ignore cleanup errors
62- }
54+ fs . rmSync ( tempDir , { recursive : true , force : true } )
6355 } )
6456
6557 it ( 'should run tests with shard option' , function ( done ) {
6658 this . timeout ( 10000 )
6759
68- exec ( `${ codecept_run } run --config ${ configFile } --shard 1/4` , ( err , stdout , stderr ) => {
60+ exec ( `node ${ codeceptRun } run --config " ${ configFile } " --shard 1/4` , ( err , stdout ) => {
6961 expect ( stdout ) . to . contain ( 'CodeceptJS' )
7062 expect ( stdout ) . to . contain ( 'OK' )
7163 expect ( stdout ) . to . match ( / 1 p a s s e d / )
@@ -77,7 +69,7 @@ exports.config = {
7769 it ( 'should handle invalid shard format' , function ( done ) {
7870 this . timeout ( 10000 )
7971
80- exec ( `${ codecept_run } run --config ${ configFile } --shard invalid` , ( err , stdout , stderr ) => {
72+ exec ( `node ${ codeceptRun } run --config " ${ configFile } " --shard invalid` , ( err , stdout ) => {
8173 expect ( stdout ) . to . contain ( 'Invalid shard format' )
8274 expect ( err . code ) . to . equal ( 1 )
8375 done ( )
@@ -87,7 +79,7 @@ exports.config = {
8779 it ( 'should handle shard index out of range' , function ( done ) {
8880 this . timeout ( 10000 )
8981
90- exec ( `${ codecept_run } run --config ${ configFile } --shard 0/4` , ( err , stdout , stderr ) => {
82+ exec ( `node ${ codeceptRun } run --config " ${ configFile } " --shard 0/4` , ( err , stdout ) => {
9183 expect ( stdout ) . to . contain ( 'Shard index 0 must be between 1 and 4' )
9284 expect ( err . code ) . to . equal ( 1 )
9385 done ( )
@@ -99,19 +91,28 @@ exports.config = {
9991
10092 const shardResults = [ ]
10193 let completedShards = 0
94+ let finished = false
10295
10396 for ( let i = 1 ; i <= 4 ; i ++ ) {
104- exec ( `${ codecept_run } run --config ${ configFile } --shard ${ i } /4` , ( err , stdout , stderr ) => {
105- expect ( err ) . to . be . null
106- expect ( stdout ) . to . contain ( 'OK' )
107- expect ( stdout ) . to . match ( / 1 p a s s e d / )
108-
109- shardResults . push ( i )
110- completedShards ++
111-
112- if ( completedShards === 4 ) {
113- expect ( shardResults ) . to . have . lengthOf ( 4 )
114- done ( )
97+ exec ( `node ${ codeceptRun } run --config "${ configFile } " --shard ${ i } /4` , ( err , stdout ) => {
98+ if ( finished ) return
99+
100+ try {
101+ expect ( err ) . to . be . null
102+ expect ( stdout ) . to . contain ( 'OK' )
103+ expect ( stdout ) . to . match ( / 1 p a s s e d / )
104+
105+ shardResults . push ( i )
106+ completedShards ++
107+
108+ if ( completedShards === 4 ) {
109+ finished = true
110+ expect ( shardResults ) . to . have . lengthOf ( 4 )
111+ done ( )
112+ }
113+ } catch ( e ) {
114+ finished = true
115+ done ( e )
115116 }
116117 } )
117118 }
0 commit comments