Skip to content

Commit 5a3be25

Browse files
DavertMikclaude
andcommitted
test: install framework registry once via mocha require, drop per-test setup
Same idea as codeceptjs's own \`config.require\` option: list a setup script in mocha's config and let it run once before any test. The unit-test world is the only place that needs to fake \`globalThis.codeceptjs\` (unit tests bypass the runner that normally installs it), so park the five lines there and stop sprinkling them into individual test files. - test/support/setup.mjs — was just chai.should(); now also installs the framework registry (Config, container, event, output, recorder, Helper) on globalThis.codeceptjs the same way lib/codecept.js does for the live runner. - .mocharc.mjs → .mocharc.cjs — mocha's config auto-discovery is inconsistent with .mocharc.mjs across CLI shapes (recursive runs picked it up, single-file runs didn't). The CJS form auto-loads reliably whether you point mocha at a directory or a single file. The exposed config is unchanged: \`require\` + \`extension\`. - test/unit/plugin/browser_test.js — drop the per-test inline \`globalThis.codeceptjs = ...\`; setup.mjs now covers it. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent f4268c0 commit 5a3be25

4 files changed

Lines changed: 23 additions & 22 deletions

File tree

.mocharc.cjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const path = require('path')
2+
3+
module.exports = {
4+
require: [path.join(__dirname, 'test', 'support', 'setup.mjs')],
5+
extension: ['js', 'mjs'],
6+
}

.mocharc.mjs

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

test/support/setup.mjs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,17 @@
11
import * as chai from 'chai'
2-
chai.should()
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'
14+
15+
if (!globalThis.codeceptjs) {
16+
globalThis.codeceptjs = { config: Config, container, event, output, recorder, Helper }
17+
}

test/unit/plugin/browser_test.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
import { expect } from 'chai'
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-
// This test imports the plugin directly, bypassing the runner. The runner
9-
// (lib/codecept.js) is what normally installs globalThis.codeceptjs so that
10-
// @codeceptjs/configure can reach framework singletons; do the same here.
11-
globalThis.codeceptjs = { config: Config, container, event, output, recorder }
12-
132
import browser from '../../../lib/plugin/browser.js'
3+
import Config from '../../../lib/config.js'
144

155
async function applyAndCreate(args, base = {}) {
166
Config.reset()

0 commit comments

Comments
 (0)