Skip to content

Commit ef7c4bd

Browse files
DavertMikclaude
andcommitted
refactor: start webServer as part of Codecept bootstrap/teardown
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 228ca7b commit ef7c4bd

5 files changed

Lines changed: 11 additions & 16 deletions

File tree

‎docs/configuration.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ webServer: [
123123
],
124124
```
125125

126-
The server starts once in the main process for `run`, `run-workers`, `run-multiple` and `run-rerun`, before `bootstrapAll`/`bootstrap`, and stops after `teardownAll`/`teardown`. Workers and child processes reuse it. `dry-run`, `list`, `check`, `def` and `info` don't start it. CodeceptJS stops the command and every process it spawned, including when the run is interrupted with Ctrl+C.
126+
The server is started as part of bootstrap, right before the `bootstrap` hook (or `bootstrapAll` for `run-workers` and `run-multiple`), and stopped after `teardown` (`teardownAll`). It starts once in the main process; workers and child processes reuse it. The interactive `shell` starts it too, `dry-run` and `check` only with `--bootstrap`. CodeceptJS stops the command and every process it spawned, including when the run is interrupted with Ctrl+C.
127127

128128
## Dynamic configuration
129129

‎docs/plugins/aiTrace.md‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
---
2-
permalink: /plugins/aiTrace
3-
editLink: false
4-
sidebar: auto
5-
title: aiTrace
6-
---
7-
81
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
92

103
## aiTrace
@@ -46,4 +39,4 @@ Possible config options:
4639

4740
### Parameters
4841

49-
* `config` **any**
42+
* `config` **any** (optional, default `{}`)

‎lib/codecept.js‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { resolve } from 'path'
66
import { fileURLToPath, pathToFileURL } from 'url'
77
import { dirname } from 'path'
88
import { createRequire } from 'module'
9+
import { isMainThread } from 'worker_threads'
910

1011
const __filename = fileURLToPath(import.meta.url)
1112
const __dirname = dirname(__filename)
@@ -23,6 +24,7 @@ import loadTests from './mocha/loadTests.js'
2324
import { validateTypeScriptSetup, getTSNodeESMWarning } from './utils/loaderCheck.js'
2425
import recorder from './recorder.js'
2526
import store from './store.js'
27+
import WebServer from './webServer.js'
2628

2729
import storeListener from './listener/store.js'
2830
import stepsListener from './listener/steps.js'
@@ -159,6 +161,7 @@ class Codecept {
159161
* @returns {Promise<void>}
160162
*/
161163
async bootstrap() {
164+
if (isMainThread && !this.opts?.child) await WebServer.startAll(this.config.webServer)
162165
return runHook(this.config.bootstrap, 'bootstrap')
163166
}
164167

@@ -168,7 +171,11 @@ class Codecept {
168171
* @returns {Promise<void>}
169172
*/
170173
async teardown() {
171-
return runHook(this.config.teardown, 'teardown')
174+
try {
175+
return await runHook(this.config.teardown, 'teardown')
176+
} finally {
177+
await WebServer.stopAll()
178+
}
172179
}
173180

174181
/**

‎lib/command/run-rerun.js‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { getConfig, getTestRoot, printError, createOutputDir } from './utils.js'
22
import Config from '../config.js'
33
import Codecept from '../rerun.js'
4-
import WebServer from '../webServer.js'
54

65
export default async function (test, options) {
76
// registering options globally to use in config
@@ -22,7 +21,7 @@ export default async function (test, options) {
2221

2322
try {
2423
await codecept.init(testRoot)
25-
await WebServer.startAll(config.webServer)
24+
2625
await codecept.bootstrap()
2726
codecept.loadTests(test)
2827
await codecept.run()
@@ -31,6 +30,5 @@ export default async function (test, options) {
3130
process.exitCode = 1
3231
} finally {
3332
await codecept.teardown()
34-
await WebServer.stopAll()
3533
}
3634
}

‎lib/command/run.js‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { getConfig, printError, getTestRoot, createOutputDir, autoExit } from '.
22
import Config from '../config.js'
33
import store from '../store.js'
44
import Codecept from '../codecept.js'
5-
import WebServer from '../webServer.js'
65

76
export default async function (test, options) {
87
// registering options globally to use in config
@@ -28,7 +27,6 @@ export default async function (test, options) {
2827

2928
try {
3029
await codecept.init(testRoot)
31-
if (!options.child) await WebServer.startAll(config.webServer)
3230
await codecept.bootstrap()
3331
codecept.loadTests(test)
3432

@@ -44,7 +42,6 @@ export default async function (test, options) {
4442
process.exitCode = 1
4543
} finally {
4644
await codecept.teardown()
47-
await WebServer.stopAll()
4845
await autoExit()
4946
}
5047
}

0 commit comments

Comments
 (0)