11const glob = require ( 'glob' )
22const path = require ( 'path' )
3- const { isPromise } = require ( 'util/types' )
43const { MetaStep } = require ( './step' )
5- const { fileExists, isFunction, isAsyncFunction, installedLocally } = require ( './utils' )
4+ const { methodsOfObject , fileExists, isFunction, isAsyncFunction, installedLocally } = require ( './utils' )
65const Translation = require ( './translation' )
76const MochaFactory = require ( './mochaFactory' )
87const recorder = require ( './recorder' )
@@ -11,12 +10,13 @@ const WorkerStorage = require('./workerStorage')
1110const store = require ( './store' )
1211const ai = require ( './ai' )
1312
14- let asyncHelperPromise = Promise . resolve ( )
13+ let asyncHelperPromise
1514
1615let container = {
1716 helpers : { } ,
1817 support : { } ,
1918 plugins : { } ,
19+ actor : null ,
2020 /**
2121 * @type {Mocha | {} }
2222 * @ignore
@@ -37,24 +37,25 @@ class Container {
3737 * @param {* } opts
3838 */
3939 static create ( config , opts ) {
40- const mochaConfig = config . mocha || { }
41- if ( config . grep && ! opts . grep ) {
42- mochaConfig . grep = config . grep
43- }
44- this . createMocha = ( ) => {
45- container . mocha = MochaFactory . create ( mochaConfig , opts || { } )
46- }
47- this . createMocha ( )
40+ asyncHelperPromise = Promise . resolve ( )
41+
4842 container . helpers = createHelpers ( config . helpers || { } )
4943 container . translation = loadTranslation ( config . translation || null , config . vocabularies || [ ] )
5044 container . support = createSupportObjects ( config . include || { } )
5145 container . plugins = createPlugins ( config . plugins || { } , opts )
5246
47+ createMocha ( config , opts )
48+ createActor ( )
49+
5350 if ( opts && opts . ai ) ai . enable ( config . ai ) // enable AI Assistant
5451 if ( config . gherkin ) loadGherkinSteps ( config . gherkin . steps || [ ] )
5552 if ( opts && typeof opts . timeouts === 'boolean' ) store . timeouts = opts . timeouts
5653 }
5754
55+ static actor ( ) {
56+ return container . actor
57+ }
58+
5859 /**
5960 * Get all plugins
6061 *
@@ -125,6 +126,7 @@ class Container {
125126 static append ( newContainer ) {
126127 const deepMerge = require ( './utils' ) . deepMerge
127128 container = deepMerge ( container , newContainer )
129+ container . actor = container . support . I
128130 }
129131
130132 /**
@@ -139,9 +141,14 @@ class Container {
139141 container . support = newSupport || { }
140142 container . plugins = newPlugins || { }
141143 container . translation = loadTranslation ( )
144+ asyncHelperPromise = Promise . resolve ( )
145+ store . actor = null
142146 }
143147
144- static async started ( ) {
148+ static async started ( fn = null ) {
149+ if ( fn ) {
150+ asyncHelperPromise = asyncHelperPromise . then ( fn )
151+ }
145152 return asyncHelperPromise
146153 }
147154
@@ -259,14 +266,6 @@ function createSupportObjects(config) {
259266 objects [ name ] = { } // placeholders
260267 }
261268
262- if ( ! config . I ) {
263- objects . I = require ( './actor' ) ( )
264-
265- if ( container . translation . I !== 'I' ) {
266- objects [ container . translation . I ] = objects . I
267- }
268- }
269-
270269 container . support = objects
271270
272271 function lazyLoad ( name ) {
@@ -361,6 +360,14 @@ function createPlugins(config, options = {}) {
361360 return plugins
362361}
363362
363+ function createActor ( ) {
364+ const actor = require ( './actor' )
365+ container . support . I = container . support . I || actor ( )
366+
367+ container . actor = container . support . I
368+ if ( container . translation . I !== 'I' ) container . support [ container . translation . I ] = container . actor
369+ }
370+
364371function getSupportObject ( config , name ) {
365372 const module = config [ name ]
366373 if ( typeof module === 'string' ) {
@@ -399,23 +406,12 @@ function loadSupportObject(modulePath, supportObjectName) {
399406 if ( modulePath . charAt ( 0 ) === '.' ) {
400407 modulePath = path . join ( global . codecept_dir , modulePath )
401408 }
409+
402410 try {
403411 const obj = require ( modulePath )
404412
405- if ( typeof obj === 'function' ) {
406- const fobj = obj ( )
407-
408- if ( fobj . constructor . name === 'Actor' ) {
409- const methods = getObjectMethods ( fobj )
410- Object . keys ( methods ) . forEach ( ( key ) => {
411- fobj [ key ] = methods [ key ]
412- } )
413-
414- return methods
415- }
416- }
417413 if ( typeof obj !== 'function' && Object . getPrototypeOf ( obj ) !== Object . prototype && ! Array . isArray ( obj ) ) {
418- const methods = getObjectMethods ( obj )
414+ const methods = methodsOfObject ( obj )
419415 Object . keys ( methods )
420416 . filter ( ( key ) => ! key . startsWith ( '_' ) )
421417 . forEach ( ( key ) => {
@@ -452,22 +448,6 @@ function loadSupportObject(modulePath, supportObjectName) {
452448/**
453449 * Method collect own property and prototype
454450 */
455- function getObjectMethods ( obj ) {
456- const methodsSet = new Set ( )
457- let protoObj = Reflect . getPrototypeOf ( obj )
458- do {
459- if ( protoObj . constructor . prototype !== Object . prototype ) {
460- const keys = Reflect . ownKeys ( protoObj )
461- keys . forEach ( ( k ) => methodsSet . add ( k ) )
462- }
463- } while ( ( protoObj = Reflect . getPrototypeOf ( protoObj ) ) )
464- Reflect . ownKeys ( obj ) . forEach ( ( k ) => methodsSet . add ( k ) )
465- const methods = { }
466- for ( const key of methodsSet . keys ( ) ) {
467- if ( key !== 'constructor' ) methods [ key ] = obj [ key ]
468- }
469- return methods
470- }
471451
472452function loadTranslation ( locale , vocabularies ) {
473453 if ( ! locale ) {
@@ -509,3 +489,11 @@ function getHelperModuleName(helperName, config) {
509489 // built-in helpers
510490 return `./helper/${ helperName } `
511491}
492+
493+ function createMocha ( config , opts ) {
494+ const mochaConfig = config . mocha || { }
495+ if ( config . grep && ! opts . grep ) {
496+ mochaConfig . grep = config . grep
497+ }
498+ container . mocha = MochaFactory . create ( mochaConfig , opts || { } )
499+ }
0 commit comments