1
1
import glob from 'glob' ;
2
2
import path from 'path' ;
3
3
import { MetaStep } from './step.js' ;
4
- import { fileExists , isFunction , isAsyncFunction } from './utils.js' ;
4
+ import { fileExists , isFunction , isAsyncFunction , deepMerge } from './utils.js' ;
5
5
import Translation from './translation.js' ;
6
6
import MochaFactory from './mochaFactory.js' ;
7
7
import recorder from './recorder.js' ;
8
8
import * as event from './event.js' ;
9
9
import WorkerStorage from './workerStorage.js' ;
10
10
import { store } from './store.js' ;
11
11
12
+ import actor from "./actor.js" ;
13
+ import { createRequire } from 'module' ;
14
+ const require = createRequire ( import . meta. url ) ;
15
+
12
16
let container = {
13
17
helpers : { } ,
14
18
support : { } ,
@@ -32,7 +36,7 @@ class Container {
32
36
* @param {* } config
33
37
* @param {* } opts
34
38
*/
35
- static create ( config , opts ) {
39
+ static async create ( config , opts ) {
36
40
const mochaConfig = config . mocha || { } ;
37
41
if ( config . grep && ! opts . grep ) {
38
42
mochaConfig . grep = config . grep ;
@@ -41,9 +45,9 @@ class Container {
41
45
container . mocha = MochaFactory . create ( mochaConfig , opts || { } ) ;
42
46
} ;
43
47
this . createMocha ( ) ;
44
- container . helpers = createHelpers ( config . helpers || { } ) ;
48
+ container . helpers = await createHelpers ( config . helpers || { } ) ;
45
49
container . translation = loadTranslation ( config . translation || null , config . vocabularies || [ ] ) ;
46
- container . support = createSupportObjects ( config . include || { } ) ;
50
+ container . support = await createSupportObjects ( config . include || { } ) ;
47
51
container . plugins = createPlugins ( config . plugins || { } , opts ) ;
48
52
if ( config . gherkin ) loadGherkinSteps ( config . gherkin . steps || [ ] ) ;
49
53
if ( opts && typeof opts . timeouts === 'boolean' ) store . timeouts = opts . timeouts ;
@@ -117,7 +121,6 @@ class Container {
117
121
* @param {Object<string, *> } newContainer
118
122
*/
119
123
static append ( newContainer ) {
120
- const deepMerge = require ( './utils.js' ) . deepMerge ;
121
124
container = deepMerge ( container , newContainer ) ;
122
125
}
123
126
@@ -151,7 +154,7 @@ class Container {
151
154
152
155
export default Container ;
153
156
154
- function createHelpers ( config ) {
157
+ async function createHelpers ( config ) {
155
158
const helpers = { } ;
156
159
let moduleName ;
157
160
for ( const helperName in config ) {
@@ -163,20 +166,25 @@ function createHelpers(config) {
163
166
moduleName = config [ helperName ] . require ; // plugin helper
164
167
}
165
168
} else {
166
- moduleName = `./helper/${ helperName } ` ; // built-in helper
169
+ moduleName = `./helper/${ helperName } .js ` ; // built-in helper
167
170
}
168
171
169
172
// @ts -ignore
170
173
let HelperClass ;
171
174
// check if the helper is the built-in, use the require() syntax.
172
175
if ( moduleName . startsWith ( './helper/' ) ) {
173
- HelperClass = require ( moduleName ) ;
176
+ HelperClass = await import ( path . resolve ( 'lib' , moduleName ) ) . then ( name => {
177
+ return name . default ;
178
+ } ) ;
174
179
} else {
175
180
// check if the new syntax export default HelperName is used and loads the Helper, otherwise loads the module that used old syntax export = HelperName.
176
- HelperClass = require ( moduleName ) . default || require ( moduleName ) ;
181
+ // HelperClass = import(moduleName) //require(moduleName).default || require(moduleName);
182
+ HelperClass = await import ( path . resolve ( moduleName ) ) . then ( name => {
183
+ return name . default ;
184
+ } ) ;
177
185
}
178
186
179
- if ( HelperClass . _checkRequirements ) {
187
+ if ( HelperClass && HelperClass . _checkRequirements ) {
180
188
const requirements = HelperClass . _checkRequirements ( ) ;
181
189
if ( requirements ) {
182
190
let install ;
@@ -201,15 +209,15 @@ function createHelpers(config) {
201
209
return helpers ;
202
210
}
203
211
204
- function createSupportObjects ( config ) {
212
+ async function createSupportObjects ( config ) {
205
213
const objects = { } ;
206
214
207
215
for ( const name in config ) {
208
216
objects [ name ] = { } ; // placeholders
209
217
}
210
218
211
219
if ( ! config . I ) {
212
- objects . I = require ( './ actor' ) ( ) ;
220
+ objects . I = actor ( ) ;
213
221
214
222
if ( container . translation . I !== 'I' ) {
215
223
objects [ container . translation . I ] = objects . I ;
@@ -218,8 +226,8 @@ function createSupportObjects(config) {
218
226
219
227
container . support = objects ;
220
228
221
- function lazyLoad ( name ) {
222
- let newObj = getSupportObject ( config , name ) ;
229
+ async function lazyLoad ( name ) {
230
+ let newObj = await getSupportObject ( config , name ) ;
223
231
try {
224
232
if ( typeof newObj === 'function' ) {
225
233
newObj = newObj ( ) ;
@@ -229,6 +237,7 @@ function createSupportObjects(config) {
229
237
} catch ( err ) {
230
238
throw new Error ( `Initialization failed for ${ name } : ${ newObj } \n${ err . message } \n${ err . stack } ` ) ;
231
239
}
240
+ console . log ( '----' , newObj )
232
241
return newObj ;
233
242
}
234
243
@@ -258,7 +267,7 @@ function createSupportObjects(config) {
258
267
ownKeys ( ) {
259
268
return Reflect . ownKeys ( config ) ;
260
269
} ,
261
- get ( target , key ) {
270
+ async get ( target , key ) {
262
271
// configured but not in support object, yet: load the module
263
272
if ( key in objects && ! ( key in target ) ) {
264
273
// load default I
@@ -267,7 +276,7 @@ function createSupportObjects(config) {
267
276
}
268
277
269
278
// load new object
270
- const object = lazyLoad ( key ) ;
279
+ const object = await lazyLoad ( key ) ;
271
280
// check that object is a real object and not an array
272
281
if ( Object . prototype . toString . call ( object ) === '[object Object]' ) {
273
282
return target [ key ] = Object . assign ( objects [ key ] , object ) ;
@@ -306,7 +315,7 @@ function createPlugins(config, options = {}) {
306
315
return plugins ;
307
316
}
308
317
309
- function getSupportObject ( config , name ) {
318
+ async function getSupportObject ( config , name ) {
310
319
const module = config [ name ] ;
311
320
if ( typeof module === 'string' ) {
312
321
return loadSupportObject ( module , name ) ;
@@ -340,12 +349,14 @@ function loadGherkinSteps(paths) {
340
349
delete global . Fail ;
341
350
}
342
351
343
- function loadSupportObject ( modulePath , supportObjectName ) {
352
+ async function loadSupportObject ( modulePath , supportObjectName ) {
344
353
if ( modulePath . charAt ( 0 ) === '.' ) {
345
354
modulePath = path . join ( global . codecept_dir , modulePath ) ;
346
355
}
347
356
try {
348
- const obj = require ( modulePath ) ;
357
+ const obj = await import ( modulePath ) . then ( name => {
358
+ return name . default ;
359
+ } ) ;
349
360
350
361
if ( typeof obj === 'function' ) {
351
362
const fobj = obj ( ) ;
@@ -422,6 +433,7 @@ function loadTranslation(locale, vocabularies) {
422
433
}
423
434
424
435
let translation ;
436
+ locale = locale . replace ( '-' , '_' ) ;
425
437
426
438
// check if it is a known translation
427
439
if ( Translation . langs [ locale ] ) {
0 commit comments