Skip to content

Commit 4e19799

Browse files
committed
fix(test): discover project tests before running
1 parent 20aed41 commit 4e19799

4 files changed

Lines changed: 48 additions & 49 deletions

File tree

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,3 @@
1-
import process from 'node:process'
2-
import { log } from '@stacksjs/cli'
3-
import { projectPath } from '@stacksjs/path'
1+
import { runTestSuites } from './runner'
42

5-
const proc = Bun.spawn(['sh', '-c', 'bun test ./tests/feature/**'], {
6-
cwd: projectPath(),
7-
stdio: ['inherit', 'inherit', 'inherit'], // Inherit stdio to see the output in the console
8-
// env: process.env, // Pass the environment variables
9-
})
10-
11-
const exitCode = await proc.exited
12-
13-
if (exitCode !== 0) {
14-
log.error('Tests failed')
15-
process.exit(exitCode ?? 1)
16-
}
3+
await runTestSuites(['feature'])
Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import process from 'node:process'
2-
import { log } from '@stacksjs/cli'
3-
import { projectPath } from '@stacksjs/path'
1+
import { runTestSuites } from './runner'
42

53
// Test runner shells out to `bun test` rather than running tests in-process so
64
// each test file gets a fresh module graph (no leakage between suites).
@@ -11,19 +9,6 @@ import { projectPath } from '@stacksjs/path'
119
// had loaded once but the test runner spun a fresh subprocess per file
1210
// and re-paid the cost.
1311
//
14-
// - The glob list includes `./tests/integration/**` so that suite ships
15-
// alongside `feature/` + `unit/` instead of being silently ignored.
16-
const proc = Bun.spawn(
17-
['sh', '-c', 'bun test --timeout 30000 ./tests/feature/** ./tests/integration/** ./tests/unit/**'],
18-
{
19-
cwd: projectPath(),
20-
stdio: ['inherit', 'inherit', 'inherit'], // Inherit stdio to see the output in the console
21-
},
22-
)
23-
24-
const exitCode = await proc.exited
25-
26-
if (exitCode !== 0) {
27-
log.error('Tests failed')
28-
process.exit(exitCode ?? 1)
29-
}
12+
// The project-wide filter intentionally includes root-level tests as well as
13+
// feature, integration, and unit suites.
14+
await runTestSuites([''], 30000)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import process from 'node:process'
2+
import { existsSync } from 'node:fs'
3+
import { join } from 'node:path'
4+
import { log } from '@stacksjs/cli'
5+
import { projectPath } from '@stacksjs/path'
6+
7+
const testFiles = new Bun.Glob('**/*.{test,spec}.{js,jsx,ts,tsx,mjs,mts,cjs,cts}')
8+
9+
export async function runTestSuites(suites: string[], timeout?: number): Promise<void> {
10+
const project = projectPath()
11+
const filters = suites.flatMap((suite) => {
12+
const suiteDirectory = join(project, 'tests', suite)
13+
if (!existsSync(suiteDirectory))
14+
return []
15+
16+
return [...testFiles.scanSync({ cwd: suiteDirectory, onlyFiles: true })]
17+
.map(file => suite ? `./tests/${suite}/${file}` : `./tests/${file}`)
18+
})
19+
20+
if (filters.length === 0) {
21+
log.info('No matching tests found.')
22+
return
23+
}
24+
25+
const args = ['bun', 'test']
26+
if (timeout)
27+
args.push('--timeout', String(timeout))
28+
args.push(...filters)
29+
30+
const proc = Bun.spawn(args, {
31+
cwd: project,
32+
stdio: ['inherit', 'inherit', 'inherit'],
33+
})
34+
const exitCode = await proc.exited
35+
36+
if (exitCode !== 0) {
37+
log.error('Tests failed')
38+
process.exit(exitCode ?? 1)
39+
}
40+
}
Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,3 @@
1-
import process from 'node:process'
2-
import { log } from '@stacksjs/cli'
3-
import { projectPath } from '@stacksjs/path'
1+
import { runTestSuites } from './runner'
42

5-
const proc = Bun.spawn(['sh', '-c', 'bun test ./tests/unit/**'], {
6-
cwd: projectPath(),
7-
stdio: ['inherit', 'inherit', 'inherit'], // Inherit stdio to see the output in the console
8-
// env: process.env, // Pass the environment variables
9-
})
10-
11-
const exitCode = await proc.exited
12-
13-
if (exitCode !== 0) {
14-
log.error('Tests failed')
15-
process.exit(exitCode ?? 1)
16-
}
3+
await runTestSuites(['unit'])

0 commit comments

Comments
 (0)