Skip to content

Commit 1b20c3c

Browse files
DavertMikclaude
andcommitted
refactor: install globalThis.codeceptjs from each suite's own bootstrap
Drop the side-effect block from lib/codecept.js. The framework now lets each test suite own the registry install via its own bootstrap script: - mocha-driven tests (unit, runner, rest): .mocharc → test/support/setup.mjs already runs before every file. setup.mjs calls installCodeceptjs(). - codeceptjs-runner-driven acceptance suites: each codecept.*.js config references installCodeceptjs as its `bootstrap` field. The runner invokes bootstrap after container.create and before tests start, so globalThis.codeceptjs is in place by the time @codeceptjs/expect-helper's Proxy is read at I.expect*() call time. The shared installer lives in test/support/install-codeceptjs.js — five lines, one job, idempotent. WebDriver's existing 5s selenium-wait bootstrap calls installCodeceptjs() then waits, in that order. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 6c94972 commit 1b20c3c

7 files changed

Lines changed: 30 additions & 32 deletions

File tree

lib/codecept.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,6 @@ import globalRetryListener from './listener/globalRetry.js'
3333
import exitListener from './listener/exit.js'
3434
import emptyRunListener from './listener/emptyRun.js'
3535

36-
// Companion packages (@codeceptjs/configure, @codeceptjs/expect-helper,
37-
// @codeceptjs/helper) read framework singletons through globalThis.codeceptjs.
38-
// `@codeceptjs/configure` in particular runs at the top of a user's
39-
// codecept.conf.js, before `Config.load` returns — so the registry has to
40-
// exist by the time this module is imported, not later during init.
41-
// In tests we install the same handle from .mocharc → test/support/setup.mjs.
42-
if (!globalThis.codeceptjs) {
43-
globalThis.codeceptjs = { config: Config, container, event, output, recorder, Helper }
44-
}
45-
4636
/**
4737
* CodeceptJS runner
4838
*/

test/acceptance/codecept.Playwright.coverage.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import TestHelper from '../support/TestHelper.js'
2+
import installCodeceptjs from '../support/install-codeceptjs.js'
23

34
export const config = {
45
tests: './*_test.js',
@@ -28,7 +29,7 @@ export const config = {
2829
},
2930
},
3031
include: {},
31-
bootstrap: false,
32+
bootstrap: installCodeceptjs,
3233
mocha: {},
3334
plugins: {
3435
screenshotOnFail: {

test/acceptance/codecept.Playwright.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import TestHelper from '../support/TestHelper.js'
2+
import installCodeceptjs from '../support/install-codeceptjs.js'
23

34
export const config = {
45
tests: './*_test.js',
@@ -31,7 +32,7 @@ export const config = {
3132
'@codeceptjs/expect-helper': {},
3233
},
3334
include: {},
34-
bootstrap: false,
35+
bootstrap: installCodeceptjs,
3536
mocha: {},
3637
plugins: {
3738
screenshotOnFail: {

test/acceptance/codecept.Puppeteer.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import TestHelper from '../support/TestHelper.js'
2+
import installCodeceptjs from '../support/install-codeceptjs.js'
23

34
export const config = {
45
tests: './*_test.js',
@@ -21,7 +22,7 @@ export const config = {
2122
},
2223
},
2324
include: {},
24-
bootstrap: false,
25+
bootstrap: installCodeceptjs,
2526
mocha: {},
2627
plugins: {
2728
screenshotOnFail: {

test/acceptance/codecept.WebDriver.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import TestHelper from '../support/TestHelper.js'
2+
import installCodeceptjs from '../support/install-codeceptjs.js'
23

34
export const config = {
45
tests: './*_test.js',
@@ -26,10 +27,10 @@ export const config = {
2627
},
2728
},
2829
include: {},
29-
bootstrap: async () =>
30-
new Promise(done => {
31-
setTimeout(done, 5000)
32-
}), // let's wait for selenium
30+
bootstrap: async () => {
31+
installCodeceptjs()
32+
await new Promise(done => setTimeout(done, 5000)) // let's wait for selenium
33+
},
3334
mocha: {},
3435
name: 'acceptance',
3536
plugins: {

test/support/install-codeceptjs.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import Helper from '@codeceptjs/helper'
2+
import Config from '../../lib/config.js'
3+
import container from '../../lib/container.js'
4+
import event from '../../lib/event.js'
5+
import output from '../../lib/output.js'
6+
import recorder from '../../lib/recorder.js'
7+
8+
// Install the in-process handle that companion packages
9+
// (@codeceptjs/configure, @codeceptjs/expect-helper, @codeceptjs/helper)
10+
// read at runtime. Each test suite calls this from its own bootstrap:
11+
// mocha-driven tests via .mocharc → test/support/setup.mjs;
12+
// codeceptjs-runner-driven acceptance suites via the `bootstrap` field.
13+
export default function installCodeceptjs() {
14+
if (globalThis.codeceptjs) return
15+
globalThis.codeceptjs = { config: Config, container, event, output, recorder, Helper }
16+
}

test/support/setup.mjs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
11
import * as chai from 'chai'
2-
chai.should()
3-
4-
// Install the framework registry the same way the runner does in lib/codecept.js.
5-
// Unit tests import lib/* modules directly (bypassing the runner), so without
6-
// this companion packages reaching for `globalThis.codeceptjs` (e.g.
7-
// @codeceptjs/configure inside the browser plugin) get an undefined handle.
8-
import Helper from '@codeceptjs/helper'
9-
import Config from '../../lib/config.js'
10-
import container from '../../lib/container.js'
11-
import event from '../../lib/event.js'
12-
import output from '../../lib/output.js'
13-
import recorder from '../../lib/recorder.js'
2+
import installCodeceptjs from './install-codeceptjs.js'
143

15-
if (!globalThis.codeceptjs) {
16-
globalThis.codeceptjs = { config: Config, container, event, output, recorder, Helper }
17-
}
4+
chai.should()
5+
installCodeceptjs()

0 commit comments

Comments
 (0)