-
-
Notifications
You must be signed in to change notification settings - Fork 752
Expand file tree
/
Copy pathcodecept.js
More file actions
311 lines (278 loc) · 8.93 KB
/
codecept.js
File metadata and controls
311 lines (278 loc) · 8.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import { existsSync, readFileSync } from 'fs'
import { globSync } from 'glob'
import shuffle from 'lodash.shuffle'
import fsPath from 'path'
import { resolve } from 'path'
import { fileURLToPath, pathToFileURL } from 'url'
import { dirname } from 'path'
import { createRequire } from 'module'
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
import Helper from '@codeceptjs/helper'
import Runner from './runner.js'
import container from './container.js'
import Config from './config.js'
import runHook from './hooks.js'
import ActorFactory from './actor.js'
import { emptyFolder } from './utils.js'
import { initCodeceptGlobals } from './globals.js'
import store from './store.js'
import storeListener from './listener/store.js'
import stepsListener from './listener/steps.js'
import configListener from './listener/config.js'
import resultListener from './listener/result.js'
import helpersListener from './listener/helpers.js'
import globalTimeoutListener from './listener/globalTimeout.js'
import globalRetryListener from './listener/globalRetry.js'
import exitListener from './listener/exit.js'
import emptyRunListener from './listener/emptyRun.js'
/**
* CodeceptJS runner
*/
class Codecept {
/**
* Create CodeceptJS runner.
* Config and options should be passed
*
* @param {*} config
* @param {*} opts
*/
constructor(config, opts) {
this.config = Config.create(config)
this.opts = opts
this.testFiles = new Array(0)
this.requiringModules = config.require
}
/**
* Require modules before codeceptjs running
*
* @param {string[]} requiringModules
*/
async requireModules(requiringModules) {
if (requiringModules) {
for (const requiredModule of requiringModules) {
let modulePath = requiredModule
const isLocalFile = existsSync(modulePath) || existsSync(`${modulePath}.js`)
if (isLocalFile) {
modulePath = resolve(modulePath)
// For ESM, ensure .js extension for local files
if (!modulePath.endsWith('.js') && !modulePath.endsWith('.mjs') && !modulePath.endsWith('.cjs')) {
if (existsSync(`${modulePath}.js`)) {
modulePath = `${modulePath}.js`
}
}
} else {
// For npm packages, resolve from the user's directory
// This ensures packages like tsx are found in user's node_modules
const userDir = store.codeceptDir || process.cwd()
try {
// Use createRequire to resolve from user's directory
const userRequire = createRequire(pathToFileURL(resolve(userDir, 'package.json')).href)
const resolvedPath = userRequire.resolve(requiredModule)
modulePath = pathToFileURL(resolvedPath).href
} catch (resolveError) {
// If resolution fails, try direct import (will check from CodeceptJS node_modules)
// This is the fallback for globally installed packages
modulePath = requiredModule
}
}
// Use dynamic import for ESM
await import(modulePath)
}
}
}
/**
* Initialize CodeceptJS at specific dir.
* Loads config, requires factory methods
*
* @param {string} dir
*/
async init(dir) {
await this.initGlobals(dir)
// Require modules before initializing
await this.requireModules(this.requiringModules)
// initializing listeners
await container.create(this.config, this.opts)
this.runner = new Runner(this)
await this.runHooks()
}
/**
* Creates global variables
*
* @param {string} dir
*/
async initGlobals(dir) {
await initCodeceptGlobals(dir, this.config, container)
}
/**
* Executes hooks.
*/
async runHooks() {
// For workers parent process we only need plugins/hooks.
// Core listeners are executed inside worker threads.
if (!this.opts?.skipDefaultListeners) {
const listenerModules = [
'./listener/store.js',
'./listener/steps.js',
'./listener/config.js',
'./listener/result.js',
'./listener/helpers.js',
'./listener/globalTimeout.js',
'./listener/globalRetry.js',
'./listener/retryEnhancer.js',
'./listener/exit.js',
'./listener/emptyRun.js',
]
for (const modulePath of listenerModules) {
const module = await import(modulePath)
runHook(module.default || module)
}
}
// custom hooks (previous iteration of plugins)
this.config.hooks.forEach(hook => runHook(hook))
}
/**
* Executes bootstrap.
*
* @returns {Promise<void>}
*/
async bootstrap() {
return runHook(this.config.bootstrap, 'bootstrap')
}
/**
* Executes teardown.
*
* @returns {Promise<void>}
*/
async teardown() {
return runHook(this.config.teardown, 'teardown')
}
/**
* Loads tests by pattern or by config.tests
*
* @param {string} [pattern]
*/
loadTests(pattern) {
const options = {
cwd: store.codeceptDir,
}
let patterns = [pattern]
if (!pattern) {
patterns = []
// If the user wants to test a specific set of test files as an array or string.
if (this.config.tests && !this.opts.features) {
if (Array.isArray(this.config.tests)) {
patterns.push(...this.config.tests)
} else {
patterns.push(this.config.tests)
}
}
if (this.config.gherkin && this.config.gherkin.features && !this.opts.tests) {
if (Array.isArray(this.config.gherkin.features)) {
this.config.gherkin.features.forEach(feature => {
patterns.push(feature)
})
} else {
patterns.push(this.config.gherkin.features)
}
}
}
for (pattern of patterns) {
if (pattern) {
globSync(pattern, options).forEach(file => {
if (file.includes('node_modules')) return
if (!fsPath.isAbsolute(file)) {
file = fsPath.join(store.codeceptDir, file)
}
if (!this.testFiles.includes(fsPath.resolve(file))) {
this.testFiles.push(fsPath.resolve(file))
}
})
}
}
if (this.opts.shuffle) {
this.testFiles = shuffle(this.testFiles)
}
if (this.opts.shard) {
this.testFiles = this._applySharding(this.testFiles, this.opts.shard)
}
}
/**
* Apply sharding to test files based on shard configuration
*
* @param {Array<string>} testFiles - Array of test file paths
* @param {string} shardConfig - Shard configuration in format "index/total" (e.g., "1/4")
* @returns {Array<string>} - Filtered array of test files for this shard
*/
_applySharding(testFiles, shardConfig) {
const shardMatch = shardConfig.match(/^(\d+)\/(\d+)$/)
if (!shardMatch) {
throw new Error('Invalid shard format. Expected format: "index/total" (e.g., "1/4")')
}
const shardIndex = parseInt(shardMatch[1], 10)
const shardTotal = parseInt(shardMatch[2], 10)
if (shardTotal < 1) {
throw new Error('Shard total must be at least 1')
}
if (shardIndex < 1 || shardIndex > shardTotal) {
throw new Error(`Shard index ${shardIndex} must be between 1 and ${shardTotal}`)
}
if (testFiles.length === 0) {
return testFiles
}
// Calculate which tests belong to this shard
const shardSize = Math.ceil(testFiles.length / shardTotal)
const startIndex = (shardIndex - 1) * shardSize
const endIndex = Math.min(startIndex + shardSize, testFiles.length)
return testFiles.slice(startIndex, endIndex)
}
/**
* Returns parsed suites with their tests.
* Creates a temporary Mocha instance to avoid polluting container state.
* Must be called after init(). Calls loadTests() internally if testFiles is empty.
*
* @param {string} [pattern] - glob pattern for test files
* @returns {Array<{title: string, file: string, tags: string[], tests: Array<{title: string, uid: string, tags: string[], fullTitle: string}>}>}
*/
getSuites(pattern) {
return this.runner.getSuites(pattern)
}
/**
* Run all tests in a suite.
* Must be called after init() and bootstrap().
*
* @param {{file: string}} suite - suite object returned by getSuites()
* @returns {Promise<void>}
*/
async runSuite(suite) {
return this.runner.runSuite(suite)
}
/**
* Run a single test by its fullTitle.
* Must be called after init() and bootstrap().
*
* @param {{fullTitle: string}} test - test object returned by getSuites()
* @returns {Promise<void>}
*/
async runTest(test) {
return this.runner.runTest(test)
}
/**
* Run a specific test or all loaded tests.
*
* @param {string} [test]
* @returns {Promise<void>}
*/
async run(test) {
return this.runner.run(test)
}
/**
* Returns the version string of CodeceptJS.
*
* @returns {string} The version string.
*/
static version() {
return JSON.parse(readFileSync(`${__dirname}/../package.json`, 'utf8')).version
}
}
export default Codecept