1- const Mocha = require ( 'mocha' ) ,
2- fs = require ( 'fs' ) ,
3- path = require ( 'path' ) ;
4-
1+ // temp base modules
2+ const Mocha = require ( 'mocha' )
3+ const fs = require ( 'fs' )
4+ const path = require ( 'path' )
55const { getSpecFilesArr} = require ( './rerun' )
6+ // temp base modules
7+
8+
9+ // temp hardcoded values
10+ const everyCycleCallback = null
11+ const intervalPoll = 500
12+ const debugProcess = false
13+ const maxSessionCount = 5
14+ const executionTimesCount = 3
15+ // temp hardcoded values
16+
17+
18+ let currentSessionCount = 0
19+
20+
21+
622
7- // Instantiate a Mocha instance.
8- var mocha = new Mocha ( ) ;
923
1024const itSpecNameRegex = / (?< = i t \( ' ) ( \d | \w | \s ) + / ig
1125
1226
1327const filesWithGrepOpts = [ ]
1428
1529
16-
1730var testDir = path . relative ( __dirname , './mocha_specs' )
1831
1932// Add each .js file to the mocha instance
@@ -23,7 +36,6 @@ getSpecFilesArr(testDir).forEach(function(file) {
2336
2437 const stepGreps = fileContent . match ( itSpecNameRegex )
2538
26-
2739 stepGreps . forEach ( function ( grepItem ) {
2840 filesWithGrepOpts . push ( { file, grepItem} )
2941 } )
@@ -34,9 +46,117 @@ getSpecFilesArr(testDir).forEach(function(file) {
3446} ) ;
3547
3648
37- console . log ( filesWithGrepOpts )
49+ async function executeCommandAsync ( cmd , runIndex ) {
50+
51+ // console.log(cmd)
52+ if ( ! cmd ) return
53+
54+ return new Promise ( function ( resolve ) {
55+ const mocha = new Mocha ( )
56+ console . log ( cmd . file )
57+ mocha . addFile ( cmd . file )
58+ mocha . grep ( cmd . grepItem )
59+ console . log ( cmd )
60+
61+ mocha . run ( function ( failures ) {
62+ console . log ( '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 11111' )
63+ if ( cmd . file === 'mocha_specs/2.spec.js' , cmd . grepItem === 'it 2 second describe ' ) {
64+ console . log ( 'should fail' , '!!!!!!!!!!!!!!!!!!!!!' , failures )
65+ process . exit ( 0 )
66+ }
67+ console . log ( 'FAILERS' , failures )
68+ resolve ( true )
69+ } )
70+
71+ } )
72+ }
73+
74+
75+ const item = {
76+ file : 'mocha_specs/2.spec.js' ,
77+ grepItem : 'it 2 second describe '
78+ }
79+
80+
81+
82+
83+
84+
85+
86+ executeCommandAsync ( item )
87+
88+
89+
90+
91+ // runCommandsArray(filesWithGrepOpts, [], 0).then(console.log)
92+
93+
94+
95+
96+
97+
98+ async function runCommand ( commands , failedCommands , runIndex ) {
99+ if ( maxSessionCount > currentSessionCount && commands . length ) {
100+ currentSessionCount += 1
101+ const result = await executeCommandAsync ( commands . splice ( 0 , 1 ) [ 0 ] , runIndex ) . catch ( console . error )
102+ if ( result ) {
103+ failedCommands . push ( result )
104+ }
105+ currentSessionCount -= 1
106+ }
107+ }
108+
109+
110+
111+
112+ async function runCommandsArray ( commands , failedCommands , executionCount ) {
113+
114+ const asserter = setInterval ( ( ) => runCommand ( commands , failedCommands , executionCount ) , intervalPoll ) ;
115+
116+ do {
117+ if ( commands . length ) { await runCommand ( commands , failedCommands , executionCount ) }
118+ if ( currentSessionCount ) { await sleep ( 2000 ) }
119+ } while ( commands . length || currentSessionCount )
120+
121+ if ( everyCycleCallback && typeof everyCycleCallback === 'function' ) {
122+ try {
123+ await everyCycleCallback ( )
124+ } catch ( e ) {
125+ console . log ( e )
126+ }
127+ }
128+
129+ clearInterval ( asserter )
130+ return failedCommands
131+ }
132+
133+
134+
135+ /*
136+ const executionTimes = new Array(executionTimesCount).fill('noop')
137+ .reduce((resolver, current, index) => {
138+
139+ if(debugProcess) {
140+ console.info('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
141+ console.info(`Execution number: ${index}`)
142+ console.info('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
143+ }
144+
145+ return resolver.then((resolvedCommandsArray) => {
146+ if(debugProcess) {
147+ console.info('=========================================================================')
148+ console.info(`Processes count: ${resolvedCommandsArray.length}`)
149+ console.info('=========================================================================')
150+ }
151+ return runCommandsArray(resolvedCommandsArray, [], index)
38152
153+ .then((failedCommandsArray) => {
154+ return failedCommandsArray
155+ })
156+ })
157+ }, Promise.resolve(filesWithGrepOpts))
39158
159+ */
40160
41161// // Run the tests.
42162// mocha.run(function(failures) {
0 commit comments