Skip to content

Commit 64420d8

Browse files
committed
fix(test): use vi.fn() for TTY method stubs instead of plain functions
Changed stub creation from plain functions `() => {}` to `vi.fn()` to prevent double-counting of mock calls. When spying on a plain function stub, some test environments were counting both the spy call and the underlying stub call, causing tests to fail with "expected 2 calls, got 5" errors. Using vi.fn() creates a proper mock function that integrates correctly with vitest's spy system, ensuring spy call counts are accurate across all platforms. Fixes test failures in test/stdio/stderr.test.ts and test/stdio/stdout.test.ts that occurred in CI environments.
1 parent 3f72964 commit 64420d8

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

test/stdio/stderr.test.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,20 @@ describe('stdio/stderr', () => {
3232
originalColumns = stderr.columns
3333
originalRows = stderr.rows
3434

35-
// Add TTY methods if they don't exist (for non-TTY environments)
36-
if (!stderr.cursorTo) {
37-
;(stderr as any).cursorTo = () => {}
38-
}
39-
if (!stderr.clearLine) {
40-
;(stderr as any).clearLine = () => {}
41-
}
42-
43-
// Create spies
35+
// Create spies (add methods if they don't exist in non-TTY environments)
4436
// @ts-expect-error - Vitest spy type doesn't match ReturnType<typeof vi.spyOn>
4537
writeSpy = vi.spyOn(stderr, 'write').mockImplementation(() => true)
38+
39+
// Create stubs for TTY methods only if they don't exist, then spy on them
40+
if (!stderr.cursorTo) {
41+
;(stderr as any).cursorTo = vi.fn()
42+
}
4643
// @ts-expect-error - Vitest spy type doesn't match ReturnType<typeof vi.spyOn>
4744
cursorToSpy = vi.spyOn(stderr, 'cursorTo').mockImplementation(() => {})
45+
46+
if (!stderr.clearLine) {
47+
;(stderr as any).clearLine = vi.fn()
48+
}
4849
// @ts-expect-error - Vitest spy type doesn't match ReturnType<typeof vi.spyOn>
4950
clearLineSpy = vi.spyOn(stderr, 'clearLine').mockImplementation(() => {})
5051
})

test/stdio/stdout.test.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,29 @@ describe('stdio/stdout', () => {
3636
originalColumns = stdout.columns
3737
originalRows = stdout.rows
3838

39-
// Add TTY methods if they don't exist (for non-TTY environments)
40-
if (!stdout.cursorTo) {
41-
;(stdout as any).cursorTo = () => {}
42-
}
43-
if (!stdout.clearLine) {
44-
;(stdout as any).clearLine = () => {}
45-
}
46-
if (!stdout.clearScreenDown) {
47-
;(stdout as any).clearScreenDown = () => {}
48-
}
49-
5039
// Make stdout appear as a WriteStream instance for hide/showCursor tests
5140
Object.setPrototypeOf(stdout, WriteStream.prototype)
5241

53-
// Create spies
42+
// Create spies (add methods if they don't exist in non-TTY environments)
5443
// @ts-expect-error - Vitest spy type doesn't match ReturnType<typeof vi.spyOn>
5544
writeSpy = vi.spyOn(stdout, 'write').mockImplementation(() => true)
45+
46+
// Create stubs for TTY methods only if they don't exist, then spy on them
47+
if (!stdout.cursorTo) {
48+
;(stdout as any).cursorTo = vi.fn()
49+
}
5650
// @ts-expect-error - Vitest spy type doesn't match ReturnType<typeof vi.spyOn>
5751
cursorToSpy = vi.spyOn(stdout, 'cursorTo').mockImplementation(() => {})
52+
53+
if (!stdout.clearLine) {
54+
;(stdout as any).clearLine = vi.fn()
55+
}
5856
// @ts-expect-error - Vitest spy type doesn't match ReturnType<typeof vi.spyOn>
5957
clearLineSpy = vi.spyOn(stdout, 'clearLine').mockImplementation(() => {})
58+
59+
if (!stdout.clearScreenDown) {
60+
;(stdout as any).clearScreenDown = vi.fn()
61+
}
6062
// @ts-expect-error - Vitest spy type doesn't match ReturnType<typeof vi.spyOn>
6163
clearScreenDownSpy = vi
6264
.spyOn(stdout, 'clearScreenDown')

0 commit comments

Comments
 (0)