Skip to content

Commit 4d8a94e

Browse files
committed
feat: invoke onError hook when unable to parse tsconfig file
1 parent 5a31652 commit 4d8a94e

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

src/dev_server.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { fileURLToPath } from 'node:url'
1616
import { type FSWatcher } from 'chokidar'
1717
import { type ResultPromise } from 'execa'
1818
import string from '@poppinss/utils/string'
19+
import { RuntimeException } from '@poppinss/utils/exception'
1920
import { type UnWrapLazyImport } from '@poppinss/utils/types'
2021

2122
import debug from './debug.ts'
@@ -440,6 +441,7 @@ export class DevServer {
440441
async start(ts: typeof tsStatic) {
441442
const tsConfig = parseConfig(this.cwd, ts)
442443
if (!tsConfig) {
444+
this.#onError?.(new RuntimeException('Unable to parse tsconfig file'))
443445
return
444446
}
445447

@@ -480,6 +482,7 @@ export class DevServer {
480482
async startAndWatch(ts: typeof tsStatic, options?: { poll: boolean }) {
481483
const tsConfig = parseConfig(this.cwd, ts)
482484
if (!tsConfig) {
485+
this.#onError?.(new RuntimeException('Unable to parse tsconfig file'))
483486
return
484487
}
485488

src/test_runner.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { fileURLToPath } from 'node:url'
1414
import { type FSWatcher } from 'chokidar'
1515
import { type ResultPromise } from 'execa'
1616
import string from '@poppinss/utils/string'
17+
import { RuntimeException } from '@poppinss/utils/exception'
1718
import { type UnWrapLazyImport } from '@poppinss/utils/types'
1819

1920
import debug from './debug.ts'
@@ -320,6 +321,7 @@ export class TestRunner {
320321
async runAndWatch(ts: typeof tsStatic, options?: { poll: boolean }) {
321322
const tsConfig = parseConfig(this.cwd, ts)
322323
if (!tsConfig) {
324+
this.#onError?.(new RuntimeException('Unable to parse tsconfig file'))
323325
return
324326
}
325327

tests/dev_server.spec.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { test } from '@japa/runner'
1313
import { join, sep } from 'node:path'
1414
import { cliui } from '@poppinss/cliui'
1515
import { setTimeout as sleep } from 'node:timers/promises'
16+
import { RuntimeException } from '@poppinss/utils/exception'
1617

1718
import { DevServer } from '../index.ts'
1819

@@ -322,4 +323,52 @@ test.group('DevServer', () => {
322323
0
323324
)
324325
}).disableTimeout()
326+
327+
test('return error when tsconfig file is missing', async ({ fs, assert, cleanup }) => {
328+
let hooksStack: string[] = []
329+
let error: any
330+
331+
await fs.create(
332+
'bin/server.ts',
333+
`process.send({ isAdonisJS: true, environment: 'web', port: process.env.PORT, host: 'localhost' })`
334+
)
335+
await fs.create('.env', 'PORT=3335')
336+
337+
const devServer = new DevServer(fs.baseUrl, {
338+
nodeArgs: [],
339+
scriptArgs: [],
340+
metaFiles: [],
341+
suites: [],
342+
hooks: {
343+
devServerStarted: [
344+
async () => ({
345+
default: () => {
346+
hooksStack.push('devServerStarted')
347+
},
348+
}),
349+
],
350+
devServerStarting: [
351+
async () => ({
352+
default: () => {
353+
hooksStack.push('devServerStarting')
354+
},
355+
}),
356+
],
357+
},
358+
})
359+
360+
devServer.ui = cliui()
361+
devServer.ui.switchMode('raw')
362+
363+
devServer.onError((startError) => {
364+
error = startError
365+
})
366+
367+
await devServer.startAndWatch(ts)
368+
cleanup(() => devServer.close())
369+
370+
assert.instanceOf(error, RuntimeException)
371+
assert.deepEqual(devServer.ui.logger.getLogs(), [])
372+
assert.deepEqual(hooksStack, [])
373+
})
325374
})

0 commit comments

Comments
 (0)