Skip to content

Commit fae8923

Browse files
test: adopt @doist/cli-core/testing shared helpers (#6)
Replace per-file test scaffolding with the published cli-core helpers (cli-core#49): createTestProgram(register) collapses the 19 identical local createProgram() definitions to a one-line wrapper, and captureConsole(method?) replaces ~200 inline vi.spyOn(console, ...) spies plus their manual mockRestore() calls (it auto-restores via onTestFinished). Test-only change across 21 files; no runtime behaviour affected. Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 3bf7434 commit fae8923

21 files changed

Lines changed: 249 additions & 603 deletions

src/commands/account/account.test.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Command } from 'commander'
1+
import { captureConsole, createTestProgram } from '@doist/cli-core/testing'
22
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
33

44
const storeMocks = vi.hoisted(() => ({
@@ -28,12 +28,7 @@ import { TOKEN_ENV_VAR } from '../../lib/auth.js'
2828
import { CliError } from '../../lib/errors.js'
2929
import { registerAccountCommand } from './index.js'
3030

31-
function createProgram() {
32-
const program = new Command()
33-
program.exitOverride()
34-
registerAccountCommand(program)
35-
return program
36-
}
31+
const createProgram = () => createTestProgram(registerAccountCommand)
3732

3833
/**
3934
* Seed an in-memory store that mirrors the real keyring store closely enough
@@ -68,13 +63,11 @@ describe('account command', () => {
6863

6964
beforeEach(() => {
7065
vi.clearAllMocks()
71-
consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
72-
errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
66+
consoleSpy = captureConsole('log')
67+
errorSpy = captureConsole('error')
7368
})
7469

7570
afterEach(() => {
76-
consoleSpy.mockRestore()
77-
errorSpy.mockRestore()
7871
vi.unstubAllEnvs()
7972
})
8073

src/commands/auth/auth.test.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { captureConsole, createTestProgram } from '@doist/cli-core/testing'
12
import { Command } from 'commander'
23
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
34

@@ -89,12 +90,7 @@ const mockGetAuthMetadata = vi.mocked(getAuthMetadata)
8990
const mockCreateWrappedCommsClient = vi.mocked(createWrappedCommsClient)
9091
const mockAttachLoginCommand = vi.mocked(attachLoginCommand)
9192

92-
function createProgram() {
93-
const program = new Command()
94-
program.exitOverride()
95-
registerAuthCommand(program)
96-
return program
97-
}
93+
const createProgram = () => createTestProgram(registerAuthCommand)
9894

9995
const TEST_USER: User = {
10096
id: 1,
@@ -114,13 +110,8 @@ describe('auth command', () => {
114110
vi.clearAllMocks()
115111

116112
// Mock console.log to capture output
117-
consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
118-
errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
119-
})
120-
121-
afterEach(() => {
122-
consoleSpy.mockRestore()
123-
errorSpy.mockRestore()
113+
consoleSpy = captureConsole('log')
114+
errorSpy = captureConsole('error')
124115
})
125116

126117
const STORED_ACCOUNT: CommsAccount = {

src/commands/changelog.test.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Command } from 'commander'
1+
import { captureConsole, createTestProgram } from '@doist/cli-core/testing'
22
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
33

44
vi.mock('node:fs/promises')
@@ -32,18 +32,13 @@ const SAMPLE_CHANGELOG = `# Changelog
3232
* prior release with a level-2 heading
3333
`
3434

35-
function createProgram() {
36-
const program = new Command()
37-
program.exitOverride()
38-
registerChangelogCommand(program)
39-
return program
40-
}
35+
const createProgram = () => createTestProgram(registerChangelogCommand)
4136

4237
describe('changelog wrapper', () => {
4338
let logSpy: ReturnType<typeof vi.spyOn>
4439

4540
beforeEach(() => {
46-
logSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
41+
logSpy = captureConsole('log')
4742
})
4843

4944
afterEach(() => {

0 commit comments

Comments
 (0)