feat: programmatic API with getSuites(), executeSuite(), executeTest()#5518
feat: programmatic API with getSuites(), executeSuite(), executeTest()#5518
Conversation
…est() Add clean programmatic API to Codecept class that wraps Mocha internals, eliminating duplicated boilerplate across dryRun, workers, and custom scripts. - getSuites(pattern?) returns parsed suites with tests as plain objects - executeSuite(suite) runs all tests in a suite - executeTest(test) runs a single test by fullTitle - Refactor workers.js to use getSuites() (removes ~30 lines of Mocha boilerplate) - Refactor dryRun.js to use getSuites() (removes Container.mocha() dependency) - Export Result class from lib/index.js - Rewrite docs/internal-api.md with full programmatic API reference Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
f28103a to
c8a7be8
Compare
- Create lib/runner.js with Runner class that owns all Mocha interactions: getSuites(), run(), runSuite(), runTest() backed by a single _execute() core - Codecept.run/runSuite/runTest/getSuites now delegate to this.runner - Remove duplicated executeTest() that was copy of run() - Refactor check.js to use codecept.getSuites() instead of manual Mocha - Export Runner from lib/index.js Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…yRunConfig - Remove dead code: activeWorkers Map, maxWorkers, empty recorder placeholder - Simplify _initializeTestPool to 4 lines - Remove redundant configWithoutFunctions variable in WorkerObject.addConfig - Remove hardcoded mochawesome/mocha-junit-reporter path manipulation from createWorkerObjects — fragile, incomplete, and redundant with output dir override - Extract getOverridenConfig to Config.applyRunConfig() in lib/config.js, shared by both workers.js and run-multiple.js Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
| } | ||
|
|
||
| async runTest(test) { | ||
| return this._execute({ grep: test.fullTitle }) |
There was a problem hiding this comment.
test.fullTitle is passed directly to mocha.grep() below, but Mocha treats string grep as a regular expression pattern. Test titles with regex metacharacters like [brackets] or (parens) will not match themselves and runTest() ends up running 0 tests.
| url: 'http://localhost' | ||
| } | ||
| } | ||
| import { Codecept, container } from 'codeceptjs'; |
There was a problem hiding this comment.
This example does not work with the current exports: Codecept is available on the default export object, but it is not a named export. import { Codecept, container } from 'codeceptjs' throws does not provide an export named 'Codecept'
| const workerConfig = worker.getConfig() | ||
| workersToExecute.push(getOverridenConfig(workerName, workerConfig, _config)) | ||
| const workerName = worker.name.replace(':', '_') | ||
| const _config = mainConfig.applyRunConfig(config, worker.getConfig()) |
There was a problem hiding this comment.
This no longer preserves the previous reporter path overrides for multiple worker runs. With reporterOptions configured, multiple browser/worker runs can write to the same report path and overwrite or mix reports. Was that removal intentional?
- runner.js: escape regex metacharacters in runTest() grep so titles with
brackets/parens match literally and exactly (anchored ^…$)
- index.js: add Codecept and Helper as named exports so
`import { Codecept } from 'codeceptjs'` works as documented
- workers.js: restore per-worker report path overrides for multiple runs,
using the generic replaceValueDeep approach already in run-multiple.js.
Without this, mochawesome/mocha-junit-reporter overwrite each other
across browser workers.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The Runner extraction created a runtime object cycle: Codecept owned a Runner, and Runner held a back-reference to Codecept for config, opts, testFiles, requiringModules, loadTests, and the event.all.before/after payload. Since Runner had one creation site, no independent state, and its only collaborator was the object it pointed back to, the class wasn't carrying its weight. Moving the methods back onto Codecept puts behavior next to its data and eliminates the cycle by construction. - lib/codecept.js: absorbs getSuites, run, runSuite, runTest, _execute - lib/runner.js: deleted - lib/index.js: Runner removed from public exports (unreleased API) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Summary
getSuites(pattern?)toCodeceptclass — returns parsed suites with tests as plain objects, using a temporary Mocha instance to avoid polluting container stateexecuteSuite(suite)andexecuteTest(test)— clean execution methods that accept objects returned bygetSuites()workers.js—createGroupsOfTests()andcreateGroupsOfSuites()now usegetSuites()instead of duplicated Mocha boilerplate (~30 lines removed)dryRun.js—printTests()now usesgetSuites(), removing directContainer.mocha()dependencyResultclass fromlib/index.jsdocs/internal-api.mdas full "Programmatic API" referenceUsage
Test plan
dry-runcommand works with refactoredgetSuites()(tested with grep filtering)run-workers 2🤖 Generated with Claude Code