Skip to content

Commit 8e7652d

Browse files
committed
feat: expose methods to close test runner and dev server
1 parent 318e9f7 commit 8e7652d

File tree

3 files changed

+51
-19
lines changed

3 files changed

+51
-19
lines changed

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,10 @@
106106
"exclude": [
107107
"tests/**",
108108
"build/**",
109-
"examples/**"
109+
"examples/**",
110+
"src/dev_server.ts",
111+
"src/test_runner.ts",
112+
"src/assets_dev_server.ts"
110113
]
111114
},
112115
"eslintConfig": {

src/dev_server.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,17 +125,18 @@ export class DevServer {
125125

126126
this.#httpServer
127127
.then((result) => {
128-
this.#logger.warning(`underlying HTTP server closed with status code "${result.exitCode}"`)
129128
if (mode === 'nonblocking') {
130129
this.#onClose?.(result.exitCode)
131130
this.#watcher?.close()
132131
this.#assetsServer?.stop()
133132
}
134133
})
135134
.catch((error) => {
136-
this.#onError?.(error)
137-
this.#watcher?.close()
138-
this.#assetsServer?.stop()
135+
if (mode === 'nonblocking') {
136+
this.#onError?.(error)
137+
this.#watcher?.close()
138+
this.#assetsServer?.stop()
139+
}
139140
})
140141
}
141142

@@ -220,6 +221,18 @@ export class DevServer {
220221
return this
221222
}
222223

224+
/**
225+
* Close watchers and running child processes
226+
*/
227+
async close() {
228+
await this.#watcher?.close()
229+
this.#assetsServer?.stop()
230+
if (this.#httpServer) {
231+
this.#httpServer.removeAllListeners()
232+
this.#httpServer.kill('SIGKILL')
233+
}
234+
}
235+
223236
/**
224237
* Start the development server
225238
*/

src/test_runner.ts

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -149,27 +149,32 @@ export class TestRunner {
149149
.then((result) => {
150150
if (mode === 'nonblocking') {
151151
this.#onClose?.(result.exitCode)
152-
this.#watcher?.close()
153-
this.#assetsServer?.stop()
152+
this.close()
154153
}
155154
})
156155
.catch((error) => {
157-
/**
158-
* Since the tests runner set the `process.exitCode = 1`, execa will
159-
* throw an exception. However, we should ignore it and keep the
160-
* watcher on.
161-
*/
162156
if (mode === 'nonblocking') {
163157
this.#onError?.(error)
164-
this.#watcher?.close()
165-
this.#assetsServer?.stop()
158+
this.close()
166159
}
167160
})
168161
.finally(() => {
169162
this.#isBusy = false
170163
})
171164
}
172165

166+
/**
167+
* Restarts the HTTP server
168+
*/
169+
#rerunTests(port: string, filtersArgs: string[]) {
170+
if (this.#testScript) {
171+
this.#testScript.removeAllListeners()
172+
this.#testScript.kill('SIGKILL')
173+
}
174+
175+
this.#runTests(port, filtersArgs, 'blocking')
176+
}
177+
173178
/**
174179
* Starts the assets server
175180
*/
@@ -190,7 +195,7 @@ export class TestRunner {
190195
if (isDotEnvFile(relativePath) || this.#isMetaFile(relativePath)) {
191196
this.#clearScreen()
192197
this.#logger.log(`${this.#colors.green(action)} ${relativePath}`)
193-
this.#runTests(port, filters, 'blocking')
198+
this.#rerunTests(port, filters)
194199
}
195200
}
196201

@@ -210,18 +215,17 @@ export class TestRunner {
210215
* then only run that file
211216
*/
212217
if (this.#isTestFile(relativePath)) {
213-
this.#runTests(
218+
this.#rerunTests(
214219
port,
215220
this.#convertFiltersToArgs({
216221
...this.#options.filters,
217222
files: [relativePath],
218-
}),
219-
'blocking'
223+
})
220224
)
221225
return
222226
}
223227

224-
this.#runTests(port, filters, 'blocking')
228+
this.#rerunTests(port, filters)
225229
}
226230

227231
/**
@@ -251,6 +255,18 @@ export class TestRunner {
251255
return this
252256
}
253257

258+
/**
259+
* Close watchers and running child processes
260+
*/
261+
async close() {
262+
await this.#watcher?.close()
263+
this.#assetsServer?.stop()
264+
if (this.#testScript) {
265+
this.#testScript.removeAllListeners()
266+
this.#testScript.kill('SIGKILL')
267+
}
268+
}
269+
254270
/**
255271
* Runs tests
256272
*/

0 commit comments

Comments
 (0)