Skip to content

Commit 330bc1d

Browse files
committed
test: improve CLI test reliability and organization
- Add 10s timeout to prevent test timeouts - Extract resetMockProcess helper for better code reuse - Simplify individual file test logic
1 parent 7a1c288 commit 330bc1d

File tree

1 file changed

+19
-25
lines changed

1 file changed

+19
-25
lines changed

src/bin/tcx2webvtt.spec.ts

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,14 @@ import { main, ProcessLike } from './tcx2webvtt.js'
66

77
const __dirname = fileURLToPath(new URL('.', import.meta.url))
88

9-
describe('tcx2webvtt CLI', () => {
9+
describe('tcx2webvtt CLI', { timeout: 10000 }, () => {
1010
let mockProcess: ProcessLike
1111
let stdout: string
1212
let stderr: string
1313

14-
beforeEach(async () => {
15-
// Reset output capture variables
14+
const resetMockProcess = () => {
1615
stdout = ''
1716
stderr = ''
18-
19-
// Mock process with string buffers
2017
mockProcess = {
2118
argv: ['node', 'tcx2webvtt.js'],
2219
stdout: {
@@ -31,6 +28,10 @@ describe('tcx2webvtt CLI', () => {
3128
},
3229
exit: vi.fn(),
3330
}
31+
}
32+
33+
beforeEach(async () => {
34+
resetMockProcess()
3435
})
3536

3637
it('should display help when --help flag is used', async () => {
@@ -114,29 +115,22 @@ describe('tcx2webvtt CLI', () => {
114115
const file2 = join(__dirname, '../../fixtures/tcx/honeybee-canyon-hike.tcx')
115116

116117
// First verify each file individually doesn't contain the other's data
117-
let individualStdout = ''
118-
const individualMockProcess = {
119-
...mockProcess,
120-
stdout: {
121-
write(data: string) {
122-
individualStdout += data
123-
},
124-
},
125-
}
126-
127118
// Test cycling file alone doesn't contain hiking data
128-
individualMockProcess.argv = ['node', 'tcx2webvtt.js', file1]
129-
individualStdout = ''
130-
await main(individualMockProcess)
131-
expect(individualStdout).toContain('"latitude":32.42404016739394')
132-
expect(individualStdout).not.toContain('"latitude":32.44296776872435')
119+
resetMockProcess()
120+
mockProcess.argv.push(file1)
121+
await main(mockProcess)
122+
expect(stdout).toContain('"latitude":32.42404016739394')
123+
expect(stdout).not.toContain('"latitude":32.44296776872435')
133124

134125
// Test hiking file alone doesn't contain cycling data
135-
individualMockProcess.argv = ['node', 'tcx2webvtt.js', file2]
136-
individualStdout = ''
137-
await main(individualMockProcess)
138-
expect(individualStdout).toContain('"latitude":32.44296776872435')
139-
expect(individualStdout).not.toContain('"latitude":32.42404016739394')
126+
resetMockProcess()
127+
mockProcess.argv.push(file2)
128+
await main(mockProcess)
129+
expect(stdout).toContain('"latitude":32.44296776872435')
130+
expect(stdout).not.toContain('"latitude":32.42404016739394')
131+
132+
// Reset for the combined test
133+
resetMockProcess()
140134

141135
// Now test combining both files
142136
mockProcess.argv.push(file1, file2)

0 commit comments

Comments
 (0)