@@ -11,17 +11,13 @@ const __filename = fileURLToPath(import.meta.url)
1111const __dirname = dirname ( __filename )
1212
1313import Helper from '@codeceptjs/helper'
14- import MochaFactory from './mocha/factory .js'
14+ import Runner from './runner .js'
1515import container from './container.js'
1616import Config from './config.js'
17- import event from './event.js'
1817import runHook from './hooks.js'
1918import ActorFactory from './actor.js'
20- import output from './output.js'
2119import { emptyFolder } from './utils.js'
2220import { initCodeceptGlobals } from './globals.js'
23- import { validateTypeScriptSetup , getTSNodeESMWarning } from './utils/loaderCheck.js'
24- import recorder from './recorder.js'
2521import store from './store.js'
2622
2723import storeListener from './listener/store.js'
@@ -104,6 +100,7 @@ class Codecept {
104100 await this . requireModules ( this . requiringModules )
105101 // initializing listeners
106102 await container . create ( this . config , this . opts )
103+ this . runner = new Runner ( this )
107104 await this . runHooks ( )
108105 }
109106
@@ -266,87 +263,29 @@ class Codecept {
266263 * @returns {Array<{title: string, file: string, tags: string[], tests: Array<{title: string, uid: string, tags: string[], fullTitle: string}>}> }
267264 */
268265 getSuites ( pattern ) {
269- if ( this . testFiles . length === 0 ) {
270- this . loadTests ( pattern )
271- }
272-
273- const tempMocha = MochaFactory . create ( this . config . mocha || { } , this . opts || { } )
274- tempMocha . files = this . testFiles
275- tempMocha . loadFiles ( )
276-
277- const suites = [ ]
278- for ( const suite of tempMocha . suite . suites ) {
279- suites . push ( {
280- ...suite . simplify ( ) ,
281- file : suite . file || '' ,
282- tests : suite . tests . map ( test => ( {
283- ...test . simplify ( ) ,
284- fullTitle : test . fullTitle ( ) ,
285- } ) ) ,
286- } )
287- }
288-
289- tempMocha . unloadFiles ( )
290- return suites
266+ return this . runner . getSuites ( pattern )
291267 }
292268
293269 /**
294- * Execute all tests in a suite.
270+ * Run all tests in a suite.
295271 * Must be called after init() and bootstrap().
296272 *
297273 * @param {{file: string} } suite - suite object returned by getSuites()
298274 * @returns {Promise<void> }
299275 */
300- async executeSuite ( suite ) {
301- return this . run ( suite . file )
276+ async runSuite ( suite ) {
277+ return this . runner . runSuite ( suite )
302278 }
303279
304280 /**
305- * Execute a single test by its fullTitle.
281+ * Run a single test by its fullTitle.
306282 * Must be called after init() and bootstrap().
307283 *
308284 * @param {{fullTitle: string} } test - test object returned by getSuites()
309285 * @returns {Promise<void> }
310286 */
311- async executeTest ( test ) {
312- await container . started ( )
313-
314- const tsValidation = validateTypeScriptSetup ( this . testFiles , this . requiringModules || [ ] )
315- if ( tsValidation . hasError ) {
316- output . error ( tsValidation . message )
317- process . exit ( 1 )
318- }
319-
320- try {
321- const { loadTranslations } = await import ( './mocha/gherkin.js' )
322- await loadTranslations ( )
323- } catch ( e ) {
324- // Ignore if gherkin module not available
325- }
326-
327- return new Promise ( ( resolve , reject ) => {
328- const mocha = container . mocha ( )
329- mocha . files = this . testFiles
330- mocha . grep ( test . fullTitle )
331-
332- const done = async ( failures ) => {
333- event . emit ( event . all . result , container . result ( ) )
334- event . emit ( event . all . after , this )
335- await recorder . promise ( )
336- if ( failures ) {
337- process . exitCode = 1
338- }
339- resolve ( )
340- }
341-
342- try {
343- event . emit ( event . all . before , this )
344- mocha . run ( async ( failures ) => await done ( failures ) )
345- } catch ( e ) {
346- output . error ( e . stack )
347- reject ( e )
348- }
349- } )
287+ async runTest ( test ) {
288+ return this . runner . runTest ( test )
350289 }
351290
352291 /**
@@ -356,64 +295,7 @@ class Codecept {
356295 * @returns {Promise<void> }
357296 */
358297 async run ( test ) {
359- await container . started ( )
360-
361- // Check TypeScript loader configuration before running tests
362- const tsValidation = validateTypeScriptSetup ( this . testFiles , this . requiringModules || [ ] )
363- if ( tsValidation . hasError ) {
364- output . error ( tsValidation . message )
365- process . exit ( 1 )
366- }
367-
368- // Show warning if ts-node/esm is being used
369- const tsWarning = getTSNodeESMWarning ( this . requiringModules || [ ] )
370- if ( tsWarning ) {
371- output . print ( output . colors . yellow ( tsWarning ) )
372- }
373-
374- // Ensure translations are loaded for Gherkin features
375- try {
376- const { loadTranslations } = await import ( './mocha/gherkin.js' )
377- await loadTranslations ( )
378- } catch ( e ) {
379- // Ignore if gherkin module not available
380- }
381-
382- return new Promise ( ( resolve , reject ) => {
383- const mocha = container . mocha ( )
384- mocha . files = this . testFiles
385-
386- if ( test ) {
387- if ( ! fsPath . isAbsolute ( test ) ) {
388- test = fsPath . join ( store . codeceptDir , test )
389- }
390- const testBasename = fsPath . basename ( test , '.js' )
391- const testFeatureBasename = fsPath . basename ( test , '.feature' )
392- mocha . files = mocha . files . filter ( t => {
393- return fsPath . basename ( t , '.js' ) === testBasename || fsPath . basename ( t , '.feature' ) === testFeatureBasename || t === test
394- } )
395- }
396-
397- const done = async ( failures ) => {
398- event . emit ( event . all . result , container . result ( ) )
399- event . emit ( event . all . after , this )
400- // Wait for any recorder tasks added by event.all.after handlers
401- await recorder . promise ( )
402- // Set exit code based on test failures
403- if ( failures ) {
404- process . exitCode = 1
405- }
406- resolve ( )
407- }
408-
409- try {
410- event . emit ( event . all . before , this )
411- mocha . run ( async ( failures ) => await done ( failures ) )
412- } catch ( e ) {
413- output . error ( e . stack )
414- reject ( e )
415- }
416- } )
298+ return this . runner . run ( test )
417299 }
418300
419301 /**
0 commit comments