Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/ai.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { generateText } from 'ai'
import { fileURLToPath } from 'url'
import path from 'path'
import { fileExists } from './utils.js'
import store from './store.js'

const __dirname = path.dirname(fileURLToPath(import.meta.url))

Expand All @@ -24,8 +25,8 @@ async function loadPrompts() {
for (const name of promptNames) {
let promptPath

if (global.codecept_dir) {
promptPath = path.join(global.codecept_dir, `prompts/${name}.js`)
if (store.codeceptDir) {
promptPath = path.join(store.codeceptDir, `prompts/${name}.js`)
}

if (!promptPath || !fileExists(promptPath)) {
Expand Down
11 changes: 5 additions & 6 deletions lib/codecept.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { emptyFolder } from './utils.js'
import { initCodeceptGlobals } from './globals.js'
import { validateTypeScriptSetup, getTSNodeESMWarning } from './utils/loaderCheck.js'
import recorder from './recorder.js'
import store from './store.js'

import storeListener from './listener/store.js'
import stepsListener from './listener/steps.js'
Expand Down Expand Up @@ -71,7 +72,7 @@ class Codecept {
} else {
// For npm packages, resolve from the user's directory
// This ensures packages like tsx are found in user's node_modules
const userDir = global.codecept_dir || process.cwd()
const userDir = store.codeceptDir || process.cwd()

try {
// Use createRequire to resolve from user's directory
Expand Down Expand Up @@ -102,8 +103,6 @@ class Codecept {
await this.requireModules(this.requiringModules)
// initializing listeners
await container.create(this.config, this.opts)
// Store container globally for easy access
global.container = container
await this.runHooks()
}

Expand Down Expand Up @@ -171,7 +170,7 @@ class Codecept {
*/
loadTests(pattern) {
const options = {
cwd: global.codecept_dir,
cwd: store.codeceptDir,
}

let patterns = [pattern]
Expand Down Expand Up @@ -203,7 +202,7 @@ class Codecept {
globSync(pattern, options).forEach(file => {
if (file.includes('node_modules')) return
if (!fsPath.isAbsolute(file)) {
file = fsPath.join(global.codecept_dir, file)
file = fsPath.join(store.codeceptDir, file)
}
if (!this.testFiles.includes(fsPath.resolve(file))) {
this.testFiles.push(fsPath.resolve(file))
Expand Down Expand Up @@ -293,7 +292,7 @@ class Codecept {

if (test) {
if (!fsPath.isAbsolute(test)) {
test = fsPath.join(global.codecept_dir, test)
test = fsPath.join(store.codeceptDir, test)
}
const testBasename = fsPath.basename(test, '.js')
const testFeatureBasename = fsPath.basename(test, '.feature')
Expand Down
2 changes: 1 addition & 1 deletion lib/command/dryRun.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async function printTests(files) {
const { default: figures } = await import('figures')
const { default: colors } = await import('chalk')

output.print(output.styles.debug(`Tests from ${global.codecept_dir}:`))
output.print(output.styles.debug(`Tests from ${store.codeceptDir}:`))
output.print()

const mocha = Container.mocha()
Expand Down
2 changes: 2 additions & 0 deletions lib/command/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { mkdirp } from 'mkdirp'
import path from 'path'
import { fileExists, ucfirst, lcfirst, beautify } from '../utils.js'
import output from '../output.js'
import store from '../store.js'
import generateDefinitions from './definitions.js'
import { getConfig, getTestRoot, safeFileWrite, readConfig } from './utils.js'

Expand All @@ -20,6 +21,7 @@ Scenario('test something', async ({ {{actor}} }) => {
// generates empty test
export async function test(genPath) {
const testsPath = getTestRoot(genPath)
store.codeceptDir = testsPath
global.codecept_dir = testsPath
const config = await getConfig(testsPath)
if (!config) return
Expand Down
9 changes: 5 additions & 4 deletions lib/command/gherkin/snippets.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import fsPath from 'path'
import { getConfig, getTestRoot } from '../utils.js'
import Codecept from '../../codecept.js'
import output from '../../output.js'
import store from '../../store.js'
import { matchStep } from '../../mocha/bdd.js'

const uuidFn = IdGenerator.uuid()
Expand Down Expand Up @@ -43,9 +44,9 @@ export default async function (genPath, options) {
}

const files = []
globSync(options.feature || config.gherkin.features, { cwd: options.feature ? '.' : global.codecept_dir }).forEach(file => {
globSync(options.feature || config.gherkin.features, { cwd: options.feature ? '.' : store.codeceptDir }).forEach(file => {
if (!fsPath.isAbsolute(file)) {
file = fsPath.join(global.codecept_dir, file)
file = fsPath.join(store.codeceptDir, file)
}
files.push(fsPath.resolve(file))
})
Expand Down Expand Up @@ -92,7 +93,7 @@ export default async function (genPath, options) {
if (child.scenario.keyword === 'Scenario Outline') continue // skip scenario outline
parseSteps(child.scenario.steps)
.map(step => {
return Object.assign(step, { file: file.replace(global.codecept_dir, '').slice(1) })
return Object.assign(step, { file: file.replace(store.codeceptDir, '').slice(1) })
})
.map(step => newSteps.set(`${step.type}(${step})`, step))
}
Expand All @@ -107,7 +108,7 @@ export default async function (genPath, options) {
}

if (!fsPath.isAbsolute(stepFile)) {
stepFile = fsPath.join(global.codecept_dir, stepFile)
stepFile = fsPath.join(store.codeceptDir, stepFile)
}

const snippets = [...newSteps.values()]
Expand Down
1 change: 1 addition & 0 deletions lib/command/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const defaultConfig = {
output: '',
helpers: {},
include: {},
noGlobals: true,
plugins: {
},
}
Expand Down
2 changes: 2 additions & 0 deletions lib/command/run-multiple.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import event from '../event.js'
import { createRuns } from './run-multiple/collection.js'
import { clearString, replaceValueDeep } from '../utils.js'
import { getConfig, getTestRoot, fail } from './utils.js'
import store from '../store.js'

const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
Expand Down Expand Up @@ -35,6 +36,7 @@ export default async function (selectedRuns, options) {
const configFile = options.config

const testRoot = getTestRoot(configFile)
store.codeceptDir = testRoot
global.codecept_dir = testRoot

// copy opts to run
Expand Down
1 change: 1 addition & 0 deletions lib/command/run-workers.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export default async function (workerCount, selectedRuns, options) {
output.print(`CodeceptJS v${Codecept.version()} ${output.standWithUkraine()}`)
output.print(`Running tests in ${output.styles.bold(numberOfWorkers)} workers...`)
store.hasWorkers = true
store.workerMode = true
process.env.RUNS_WITH_WORKERS = 'true'

const workers = new Workers(numberOfWorkers, config)
Expand Down
2 changes: 1 addition & 1 deletion lib/command/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default async function (test, options) {
codecept.loadTests(test)

if (options.verbose) {
global.debugMode = true
store.debugMode = true
const { getMachineInfo } = await import('./info.js')
await getMachineInfo()
}
Expand Down
20 changes: 10 additions & 10 deletions lib/command/workers/runTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ const { options, tests, testRoot, workerIndex, poolMode } = workerData

// Global error handlers to catch critical errors but not test failures
process.on('uncaughtException', (err) => {
if (global.container?.tsFileMapping && fixErrorStack) {
const fileMapping = global.container.tsFileMapping()
if (container?.tsFileMapping && fixErrorStack) {
const fileMapping = container.tsFileMapping()
if (fileMapping) {
fixErrorStack(err, fileMapping)
}
Expand All @@ -40,8 +40,8 @@ process.on('uncaughtException', (err) => {
})

process.on('unhandledRejection', (reason, promise) => {
if (reason && typeof reason === 'object' && reason.stack && global.container?.tsFileMapping && fixErrorStack) {
const fileMapping = global.container.tsFileMapping()
if (reason && typeof reason === 'object' && reason.stack && container?.tsFileMapping && fixErrorStack) {
const fileMapping = container.tsFileMapping()
if (fileMapping) {
fixErrorStack(reason, fileMapping)
}
Expand Down Expand Up @@ -163,8 +163,8 @@ initPromise = (async function () {
// IMPORTANT: await is required here since getConfig is async
baseConfig = await getConfig(options.config || testRoot)
} catch (configErr) {
if (global.container?.tsFileMapping && fixErrorStack) {
const fileMapping = global.container.tsFileMapping()
if (container?.tsFileMapping && fixErrorStack) {
const fileMapping = container.tsFileMapping()
if (fileMapping) {
fixErrorStack(configErr, fileMapping)
}
Expand All @@ -185,8 +185,8 @@ initPromise = (async function () {
try {
await codecept.init(testRoot)
} catch (initErr) {
if (global.container?.tsFileMapping && fixErrorStack) {
const fileMapping = global.container.tsFileMapping()
if (container?.tsFileMapping && fixErrorStack) {
const fileMapping = container.tsFileMapping()
if (fileMapping) {
fixErrorStack(initErr, fileMapping)
}
Expand Down Expand Up @@ -218,8 +218,8 @@ initPromise = (async function () {
parentPort?.close()
}
} catch (err) {
if (global.container?.tsFileMapping && fixErrorStack) {
const fileMapping = global.container.tsFileMapping()
if (container?.tsFileMapping && fixErrorStack) {
const fileMapping = container.tsFileMapping()
if (fileMapping) {
fixErrorStack(err, fileMapping)
}
Expand Down
20 changes: 10 additions & 10 deletions lib/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class Container {
* @api
*/
static tsFileMapping() {
return container.tsFileMapping
return store.tsFileMapping
}

/**
Expand Down Expand Up @@ -426,11 +426,11 @@ async function requireHelperFromModule(helperName, config, HelperClass) {
tempJsFile = allTempFiles
fileMapping = mapping
// Store file mapping in container for runtime error fixing (merge with existing)
if (!container.tsFileMapping) {
container.tsFileMapping = new Map()
if (!store.tsFileMapping) {
store.tsFileMapping = new Map()
}
for (const [key, value] of mapping.entries()) {
container.tsFileMapping.set(key, value)
store.tsFileMapping.set(key, value)
}
} catch (tsError) {
throw new Error(`Failed to load TypeScript helper ${importPath}: ${tsError.message}. Make sure 'typescript' package is installed.`)
Expand Down Expand Up @@ -672,7 +672,7 @@ async function createPlugins(config, options = {}) {
continue
}

if (!options.child && process.env.RUNS_WITH_WORKERS === 'true' && !runInParent) {
if (!options.child && store.workerMode && !runInParent) {
continue
}
let module
Expand All @@ -681,7 +681,7 @@ async function createPlugins(config, options = {}) {
module = pluginConfig.require
if (module.startsWith('.')) {
// local
module = path.resolve(global.codecept_dir, module) // custom plugin
module = path.resolve(store.codeceptDir, module) // custom plugin
}
} else {
module = `./plugin/${pluginName}.js`
Expand Down Expand Up @@ -716,7 +716,7 @@ async function loadGherkinStepsAsync(paths) {
bddModule.clearCurrentStepFile()
}
} else {
const folderPath = paths.startsWith('.') ? normalizeAndJoin(global.codecept_dir, paths) : ''
const folderPath = paths.startsWith('.') ? normalizeAndJoin(store.codeceptDir, paths) : ''
if (folderPath !== '') {
const files = globSync(folderPath)
for (const file of files) {
Expand Down Expand Up @@ -764,7 +764,7 @@ async function loadSupportObject(modulePath, supportObjectName) {
}
}
if (typeof modulePath === 'string' && modulePath.charAt(0) === '.') {
modulePath = path.join(global.codecept_dir, modulePath)
modulePath = path.join(store.codeceptDir, modulePath)
}
try {
// Use dynamic import for both ESM and CJS modules
Expand Down Expand Up @@ -888,7 +888,7 @@ async function loadTranslation(locale, vocabularies) {
const langs = await Translation.getLangs()
if (langs[locale]) {
translation = new Translation(langs[locale])
} else if (fileExists(path.join(global.codecept_dir, locale))) {
} else if (fileExists(path.join(store.codeceptDir, locale))) {
// get from a provided file instead
translation = Translation.createDefault()
translation.loadVocabulary(locale)
Expand All @@ -905,7 +905,7 @@ function getHelperModuleName(helperName, config) {
// classical require
if (config[helperName].require) {
if (config[helperName].require.startsWith('.')) {
let helperPath = path.resolve(global.codecept_dir, config[helperName].require)
let helperPath = path.resolve(store.codeceptDir, config[helperName].require)
// Add .js extension if not present for ESM compatibility
if (!path.extname(helperPath)) {
helperPath += '.js'
Expand Down
Loading
Loading