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?
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