Skip to content

Commit 9d21160

Browse files
Copilotkobenguyent
andcommitted
Changes before error encountered
Co-authored-by: kobenguyent <[email protected]>
1 parent b50aff8 commit 9d21160

File tree

15 files changed

+53
-283
lines changed

15 files changed

+53
-283
lines changed

.mocharc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export default {
2-
"require": "./test/support/setup.js"
2+
"timeout": 10000,
3+
"recursive": true
34
}

lib/command/init.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export default function (initPath) {
146146
choices: translations,
147147
},
148148
])
149-
.then(result => {
149+
.then(async result => {
150150
if (result.typescript === true) {
151151
isTypeScript = true
152152
extension = isTypeScript === true ? 'ts' : 'js'
@@ -182,7 +182,7 @@ export default function (initPath) {
182182
let helperConfigs = []
183183

184184
try {
185-
const Helper = require(`../helper/${helperName}`)
185+
const Helper = await import(`../helper/${helperName}.js`).then(m => m.default)
186186
if (Helper._checkRequirements) {
187187
packages.concat(Helper._checkRequirements())
188188
}

lib/command/workers/runTests.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,32 @@ let stdout = ''
1717

1818
const stderr = ''
1919

20-
// Requiring of Codecept need to be after tty.getWindowSize is available.
21-
const Codecept = require(process.env.CODECEPT_CLASS_PATH || '../../codecept')
20+
;(async function () {
21+
// Requiring of Codecept need to be after tty.getWindowSize is available.
22+
const Codecept = await import(process.env.CODECEPT_CLASS_PATH || '../../codecept.js').then(m => m.default)
2223

23-
const { options, tests, testRoot, workerIndex } = workerData
24+
const { options, tests, testRoot, workerIndex } = workerData
2425

25-
// hide worker output
26-
if (!options.debug && !options.verbose)
27-
process.stdout.write = string => {
28-
stdout += string
29-
return true
30-
}
26+
// hide worker output
27+
if (!options.debug && !options.verbose)
28+
process.stdout.write = string => {
29+
stdout += string
30+
return true
31+
}
3132

32-
const overrideConfigs = tryOrDefault(() => JSON.parse(options.override), {})
33+
const overrideConfigs = tryOrDefault(() => JSON.parse(options.override), {})
3334

34-
// important deep merge so dynamic things e.g. functions on config are not overridden
35-
const config = deepMerge(getConfigSync(options.config || testRoot), overrideConfigs)
35+
// important deep merge so dynamic things e.g. functions on config are not overridden
36+
const config = deepMerge(getConfigSync(options.config || testRoot), overrideConfigs)
3637

37-
// Load test and run
38-
const codecept = new Codecept(config, options)
39-
codecept.init(testRoot)
40-
codecept.loadTests()
41-
const mocha = container.mocha()
42-
filterTests()
38+
// Load test and run
39+
const codecept = new Codecept(config, options)
40+
codecept.init(testRoot)
41+
codecept.loadTests()
42+
const mocha = container.mocha()
43+
filterTests()
4344

44-
// run tests
45-
;(async function () {
45+
// run tests
4646
if (mocha.suite.total()) {
4747
await runTests()
4848
}

lib/helper/AI.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ class AI extends Helper {
187187
fs.writeFileSync(fileName, code)
188188

189189
try {
190-
registerVariable('page', require(fileName))
190+
const module = await import(fileName)
191+
registerVariable('page', module.default || module)
191192
output.success('Page object registered for this session as `page` variable')
192193
output.print('Use `=>page.methodName()` in shell to run methods of page object')
193194
output.print('Use `click(page.locatorName)` to check locators of page object')

lib/helper/ApiDataFactory.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,8 @@ class ApiDataFactory extends Helper {
265265
* @param {*} [options] options for programmatically generate the attributes
266266
* @returns {Promise<*>}
267267
*/
268-
have(factory, params, options) {
269-
const item = this._createItem(factory, params, options)
268+
async have(factory, params, options) {
269+
const item = await this._createItem(factory, params, options)
270270
this.debug(`Creating ${factory} ${JSON.stringify(item)}`)
271271
return this._requestCreate(factory, item)
272272
}
@@ -298,7 +298,7 @@ class ApiDataFactory extends Helper {
298298
return Promise.all(promises)
299299
}
300300

301-
_createItem(model, data, options) {
301+
async _createItem(model, data, options) {
302302
if (!this.factories[model]) {
303303
throw new Error(`Factory ${model} is not defined in config`)
304304
}
@@ -310,7 +310,8 @@ class ApiDataFactory extends Helper {
310310
modulePath = path.join(global.codecept_dir, modulePath)
311311
}
312312
// check if the new syntax `export default new Factory()` is used and loads the builder, otherwise loads the module that used old syntax `export default new Factory()`.
313-
const builder = require(modulePath).default || require(modulePath)
313+
const module = await import(modulePath)
314+
const builder = module.default || module
314315
return builder.build(data, options)
315316
} catch (err) {
316317
throw new Error(`Couldn't load factory file from ${modulePath}, check that

lib/helper/GraphQLDataFactory.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,8 @@ class GraphQLDataFactory extends Helper {
213213
* @param {string} operation to be performed
214214
* @param {*} params predefined parameters
215215
*/
216-
mutateData(operation, params) {
217-
const variables = this._createItem(operation, params)
216+
async mutateData(operation, params) {
217+
const variables = await this._createItem(operation, params)
218218
this.debug(`Creating ${operation} ${JSON.stringify(variables)}`)
219219
return this._requestCreate(operation, variables)
220220
}
@@ -242,7 +242,7 @@ class GraphQLDataFactory extends Helper {
242242
return Promise.all(promises)
243243
}
244244

245-
_createItem(operation, data) {
245+
async _createItem(operation, data) {
246246
if (!this.factories[operation]) {
247247
throw new Error(`Mutation ${operation} is not defined in config.factories`)
248248
}
@@ -253,7 +253,8 @@ class GraphQLDataFactory extends Helper {
253253
} catch (e) {
254254
modulePath = path.join(global.codecept_dir, modulePath)
255255
}
256-
const builder = require(modulePath)
256+
const module = await import(modulePath)
257+
const builder = module.default || module
257258
return builder.build(data)
258259
} catch (err) {
259260
throw new Error(`Couldn't load factory file from ${modulePath}, check that

lib/mocha/ui.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import addDataContext from '../data/context.js'
66
import { createTest } from './test.js'
77
import { createSuite } from './suite.js'
88
import { HookConfig, AfterSuiteHook, AfterHook, BeforeSuiteHook, BeforeHook } from './hooks.js'
9+
import container from '../container.js'
910

1011
const setContextTranslation = context => {
11-
import container from '../container.js'
1212
const contexts = container.translation().value('contexts')
1313

1414
if (contexts) {

lib/plugin/wdio.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ let restartsSession
8181
* * ... - additional configuration passed into services.
8282
*
8383
*/
84-
export default config => {
84+
export default async config => {
8585
// Keep initial configs to pass as options to wdio services
8686
const wdioOptions = { ...config }
8787
const webDriver = container.helpers('WebDriver')
@@ -97,7 +97,7 @@ export default config => {
9797
const launchers = []
9898

9999
for (const name of config.services) {
100-
const Service = safeRequire(`@wdio/${name.toLowerCase()}-service`)
100+
const Service = await safeRequire(`@wdio/${name.toLowerCase()}-service`)
101101
if (Service) {
102102
if (Service.launcher && typeof Service.launcher === 'function') {
103103
const Launcher = Service.launcher
@@ -235,9 +235,10 @@ export default config => {
235235
}
236236
}
237237

238-
function safeRequire(name) {
238+
async function safeRequire(name) {
239239
try {
240-
return require(name)
240+
const module = await import(name)
241+
return module.default || module
241242
} catch (e) {
242243
if (!e.message.match(`Cannot find module '${name}'`)) {
243244
throw new Error(`Couldn't initialise "${name}".\n${e.stack}`)

lib/translation.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ class Translation {
1313
this.loaded = loaded !== false;
1414
}
1515

16-
loadVocabulary(vocabularyFile) {
16+
async loadVocabulary(vocabularyFile) {
1717
if (!vocabularyFile) return;
1818
const filePath = path.join(global.codecept_dir, vocabularyFile);
1919

2020
try {
21-
const vocabulary = require(filePath);
21+
const vocabulary = await import(filePath).then(m => m.default || m);
2222
this.vocabulary = merge(this.vocabulary, vocabulary);
2323
} catch (err) {
2424
throw new Error(`Can't load vocabulary from ${filePath}; ${err}`);

lib/workers.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import path from 'path'
2+
import { fileURLToPath } from 'url'
23
import mkdirp from 'mkdirp'
34
import { Worker } from 'worker_threads'
45
import { EventEmitter } from 'events'
@@ -19,6 +20,8 @@ import runHook from './hooks.js'
1920
import WorkerStorage from './workerStorage.js'
2021
import collection from './command/run-multiple/collection.js'
2122

23+
const __filename = fileURLToPath(import.meta.url)
24+
const __dirname = path.dirname(__filename)
2225
const pathToWorker = path.join(__dirname, 'command', 'workers', 'runTests.js')
2326

2427
const initializeCodecept = (configPath, options = {}) => {

0 commit comments

Comments
 (0)