Skip to content

Commit 14d5d58

Browse files
author
DavertMik
committed
imitial commit for esm migration
1 parent aa79b88 commit 14d5d58

File tree

129 files changed

+1785
-4807
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

129 files changed

+1785
-4807
lines changed

.cursor/README.md

Lines changed: 0 additions & 42 deletions
This file was deleted.

.cursor/mcp.json

Lines changed: 0 additions & 9 deletions
This file was deleted.

bin/codecept.js

Lines changed: 47 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#!/usr/bin/env node
2-
const program = require('commander')
3-
const Codecept = require('../lib/codecept')
4-
const { print, error } = require('../lib/output')
5-
const { printError } = require('../lib/command/utils')
2+
import { Command } from 'commander'
3+
const program = new Command()
4+
import Codecept from '../lib/codecept.js'
5+
import output from '../lib/output.js'
6+
const { print, error } = output
7+
import commandUtils from '../lib/command/utils.js'
8+
const { printError } = commandUtils
69

710
const commandFlags = {
811
ai: {
@@ -42,6 +45,23 @@ const errorHandler =
4245
}
4346
}
4447

48+
const dynamicImport = async modulePath => {
49+
const module = await import(modulePath)
50+
return module.default || module
51+
}
52+
53+
const commandHandler = modulePath =>
54+
errorHandler(async (...args) => {
55+
const handler = await dynamicImport(modulePath)
56+
return handler(...args)
57+
})
58+
59+
const commandHandlerWithProperty = (modulePath, property) =>
60+
errorHandler(async (...args) => {
61+
const module = await dynamicImport(modulePath)
62+
return module[property](...args)
63+
})
64+
4565
if (process.versions.node && process.versions.node.split('.') && process.versions.node.split('.')[0] < 12) {
4666
error('NodeJS >= 12 is required to run.')
4767
print()
@@ -53,22 +73,16 @@ if (process.versions.node && process.versions.node.split('.') && process.version
5373
program.usage('<command> [options]')
5474
program.version(Codecept.version())
5575

56-
program
57-
.command('init [path]')
58-
.description('Creates dummy config in current dir or [path]')
59-
.action(errorHandler(require('../lib/command/init')))
76+
program.command('init [path]').description('Creates dummy config in current dir or [path]').action(commandHandler('../lib/command/init.js'))
6077

6178
program
6279
.command('check')
6380
.option(commandFlags.config.flag, commandFlags.config.description)
6481
.description('Checks configuration and environment before running tests')
6582
.option('-t, --timeout [ms]', 'timeout for checks in ms, 50000 by default')
66-
.action(errorHandler(require('../lib/command/check')))
83+
.action(commandHandler('../lib/command/check.js'))
6784

68-
program
69-
.command('migrate [path]')
70-
.description('Migrate json config to js config in current dir or [path]')
71-
.action(errorHandler(require('../lib/command/configMigrate')))
85+
program.command('migrate [path]').description('Migrate json config to js config in current dir or [path]').action(commandHandler('../lib/command/configMigrate.js'))
7286

7387
program
7488
.command('shell [path]')
@@ -78,34 +92,30 @@ program
7892
.option(commandFlags.profile.flag, commandFlags.profile.description)
7993
.option(commandFlags.ai.flag, commandFlags.ai.description)
8094
.option(commandFlags.config.flag, commandFlags.config.description)
81-
.action(errorHandler(require('../lib/command/interactive')))
95+
.action(commandHandler('../lib/command/interactive.js'))
8296

83-
program
84-
.command('list [path]')
85-
.alias('l')
86-
.description('List all actions for I.')
87-
.action(errorHandler(require('../lib/command/list')))
97+
program.command('list [path]').alias('l').description('List all actions for I.').action(commandHandler('../lib/command/list.js'))
8898

8999
program
90100
.command('def [path]')
91101
.description('Generates TypeScript definitions for all I actions.')
92102
.option(commandFlags.config.flag, commandFlags.config.description)
93103
.option('-o, --output [folder]', 'target folder to paste definitions')
94-
.action(errorHandler(require('../lib/command/definitions')))
104+
.action(commandHandler('../lib/command/definitions.js'))
95105

96106
program
97107
.command('gherkin:init [path]')
98108
.alias('bdd:init')
99109
.description('Prepare CodeceptJS to run feature files.')
100110
.option(commandFlags.config.flag, commandFlags.config.description)
101-
.action(errorHandler(require('../lib/command/gherkin/init')))
111+
.action(commandHandler('../lib/command/gherkin/init.js'))
102112

103113
program
104114
.command('gherkin:steps [path]')
105115
.alias('bdd:steps')
106116
.description('Prints all defined gherkin steps.')
107117
.option(commandFlags.config.flag, commandFlags.config.description)
108-
.action(errorHandler(require('../lib/command/gherkin/steps')))
118+
.action(commandHandler('../lib/command/gherkin/steps.js'))
109119

110120
program
111121
.command('gherkin:snippets [path]')
@@ -115,38 +125,22 @@ program
115125
.option(commandFlags.config.flag, commandFlags.config.description)
116126
.option('--feature [file]', 'feature files(s) to scan')
117127
.option('--path [file]', 'file in which to place the new snippets')
118-
.action(errorHandler(require('../lib/command/gherkin/snippets')))
128+
.action(commandHandler('../lib/command/gherkin/snippets.js'))
119129

120-
program
121-
.command('generate:test [path]')
122-
.alias('gt')
123-
.description('Generates an empty test')
124-
.action(errorHandler(require('../lib/command/generate').test))
130+
program.command('generate:test [path]').alias('gt').description('Generates an empty test').action(commandHandlerWithProperty('../lib/command/generate.js', 'test'))
125131

126-
program
127-
.command('generate:pageobject [path]')
128-
.alias('gpo')
129-
.description('Generates an empty page object')
130-
.action(errorHandler(require('../lib/command/generate').pageObject))
132+
program.command('generate:pageobject [path]').alias('gpo').description('Generates an empty page object').action(commandHandlerWithProperty('../lib/command/generate.js', 'pageObject'))
131133

132134
program
133135
.command('generate:object [path]')
134136
.alias('go')
135137
.option('--type, -t [kind]', 'type of object to be created')
136138
.description('Generates an empty support object (page/step/fragment)')
137-
.action(errorHandler(require('../lib/command/generate').pageObject))
139+
.action(commandHandlerWithProperty('../lib/command/generate.js', 'pageObject'))
138140

139-
program
140-
.command('generate:helper [path]')
141-
.alias('gh')
142-
.description('Generates a new helper')
143-
.action(errorHandler(require('../lib/command/generate').helper))
141+
program.command('generate:helper [path]').alias('gh').description('Generates a new helper').action(commandHandlerWithProperty('../lib/command/generate.js', 'helper'))
144142

145-
program
146-
.command('generate:heal [path]')
147-
.alias('gr')
148-
.description('Generates basic heal recipes')
149-
.action(errorHandler(require('../lib/command/generate').heal))
143+
program.command('generate:heal [path]').alias('gr').description('Generates basic heal recipes').action(commandHandlerWithProperty('../lib/command/generate.js', 'heal'))
150144

151145
program
152146
.command('run [test]')
@@ -185,7 +179,7 @@ program
185179
.option('--recursive', 'include sub directories')
186180
.option('--trace', 'trace function calls')
187181
.option('--child <string>', 'option for child processes')
188-
.action(errorHandler(require('../lib/command/run')))
182+
.action(commandHandler('../lib/command/run.js'))
189183

190184
program
191185
.command('run-workers <workers> [selectedRuns...]')
@@ -204,7 +198,7 @@ program
204198
.option('-p, --plugins <k=v,k2=v2,...>', 'enable plugins, comma-separated')
205199
.option('-O, --reporter-options <k=v,k2=v2,...>', 'reporter-specific options')
206200
.option('-R, --reporter <name>', 'specify the reporter to use')
207-
.action(errorHandler(require('../lib/command/run-workers')))
201+
.action(commandHandler('../lib/command/run-workers.js'))
208202

209203
program
210204
.command('run-multiple [suites...]')
@@ -230,13 +224,9 @@ program
230224
// mocha options
231225
.option('--colors', 'force enabling of colors')
232226

233-
.action(errorHandler(require('../lib/command/run-multiple')))
227+
.action(commandHandler('../lib/command/run-multiple.js'))
234228

235-
program
236-
.command('info [path]')
237-
.description('Print debugging information concerning the local environment')
238-
.option('-c, --config', 'your config file path')
239-
.action(errorHandler(require('../lib/command/info')))
229+
program.command('info [path]').description('Print debugging information concerning the local environment').option('-c, --config', 'your config file path').action(commandHandler('../lib/command/info.js'))
240230

241231
program
242232
.command('dry-run [test]')
@@ -253,7 +243,7 @@ program
253243
.option(commandFlags.steps.flag, commandFlags.steps.description)
254244
.option(commandFlags.verbose.flag, commandFlags.verbose.description)
255245
.option(commandFlags.debug.flag, commandFlags.debug.description)
256-
.action(errorHandler(require('../lib/command/dryRun')))
246+
.action(commandHandler('../lib/command/dryRun.js'))
257247

258248
program
259249
.command('run-rerun [test]')
@@ -291,7 +281,10 @@ program
291281
.option('--trace', 'trace function calls')
292282
.option('--child <string>', 'option for child processes')
293283

294-
.action(require('../lib/command/run-rerun'))
284+
.action(async (...args) => {
285+
const runRerun = await dynamicImport('../lib/command/run-rerun.js')
286+
return runRerun(...args)
287+
})
295288

296289
program.on('command:*', cmd => {
297290
console.log(`\nUnknown command ${cmd}\n`)

example-esm/codecept.conf.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ export const config = {
55
CustomHelper: {
66
require: './helpers/CustomHelper.js',
77
},
8+
FileSystem: {},
9+
REST: {
10+
endpoint: 'https://jsonplaceholder.typicode.com',
11+
prettyPrintJson: true,
12+
},
813
},
914
name: 'codeceptjs-esm-example',
1015
}

example-esm/helpers/CustomHelper.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import { createRequire } from 'module'
2-
const require = createRequire(import.meta.url)
3-
const { Helper } = require('codeceptjs')
1+
import Helper from '@codeceptjs/helper'
42
import assert from 'assert'
53

64
class CustomHelper extends Helper {

example-esm/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
"test:verbose": "codeceptjs run --verbose"
1010
},
1111
"dependencies": {
12-
"codeceptjs": "file:.."
12+
"codeceptjs": "file:..",
13+
"axios": "^1.6.0",
14+
"joi": "^17.11.0"
1315
},
1416
"devDependencies": {},
1517
"engines": {

lib/actor.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
const Step = require('./step')
2-
const MetaStep = require('./step/meta')
3-
const recordStep = require('./step/record')
4-
const container = require('./container')
5-
const { methodsOfObject } = require('./utils')
6-
const { TIMEOUT_ORDER } = require('./timeout')
7-
const eventModule = require('./event')
8-
const event = eventModule.default || eventModule
9-
const store = require('./store')
10-
const outputModule = require('./output')
11-
const output = outputModule.default || outputModule
1+
import Step, { MetaStep } from './step.js'
2+
import recordStep from './step/record.js'
3+
import retryStep from './step/retry.js'
4+
import { methodsOfObject } from './utils.js'
5+
import { TIMEOUT_ORDER } from './timeout.js'
6+
import event from './event.js'
7+
import storeModule from './store.js'
8+
const store = storeModule.default || storeModule
9+
import output from './output.js'
1210

1311
/**
1412
* @interface
@@ -61,7 +59,6 @@ class Actor {
6159
*/
6260
retry(opts) {
6361
console.log('I.retry() is deprecated, use step.retry() instead')
64-
const retryStep = require('./step/retry')
6562
retryStep(opts)
6663
return this
6764
}
@@ -73,7 +70,7 @@ class Actor {
7370
* Wraps helper methods into promises.
7471
* @ignore
7572
*/
76-
module.exports = function (obj = {}) {
73+
export default function (obj = {}, container) {
7774
const actor = container.actor() || new Actor()
7875

7976
// load all helpers once container initialized

lib/ai.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
const debug = require('debug')('codeceptjs:ai')
2-
const outputModule = require('./output')
1+
import debugModule from 'debug'
2+
const debug = debugModule('codeceptjs:ai')
3+
import outputModule from './output.js'
34
const output = outputModule.default || outputModule
4-
const eventModule = require('./event')
5+
import eventModule from './event.js'
56
const event = eventModule.default || eventModule
6-
const { removeNonInteractiveElements, minifyHtml, splitByChunks } = require('./html')
7+
import { removeNonInteractiveElements, minifyHtml, splitByChunks } from './html.js'
78

89
const defaultHtmlConfig = {
910
maxLength: 50000,
@@ -306,4 +307,4 @@ function parseCodeBlocks(response) {
306307
return modifiedSnippets.filter(snippet => !!snippet)
307308
}
308309

309-
module.exports = new AiAssistant()
310+
export default new AiAssistant()

0 commit comments

Comments
 (0)