Skip to content

Commit 7644ff8

Browse files
committed
Simplify testing setup
1 parent ee18239 commit 7644ff8

3 files changed

Lines changed: 67 additions & 44 deletions

File tree

Lines changed: 63 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,35 @@
11
import { describe, test, expect } from 'vitest'
2-
import { Config } from '@oclif/core'
32
import { runCommand } from '@oclif/test'
43
import nock from 'nock'
54

65
nock.disableNetConnect()
76

87
describe('extremes', async () => {
9-
const config = await Config.load(new URL('../..', import.meta.url).pathname)
8+
function run(args: string[]) {
9+
return runCommand(['extremes', ...args])
10+
}
11+
12+
test('--help', async () => {
13+
const { stdout } = await run(['--help'])
14+
expect(stdout).toMatch(/extremes/)
15+
expect(stdout).toMatch(/--station/)
16+
expect(stdout).toMatch(/--start/)
17+
expect(stdout).toMatch(/--end/)
18+
expect(stdout).toMatch(/--format/)
19+
})
1020

1121
test('--station <id>', async () => {
12-
const { stdout } = await runCommand(
13-
['extremes', '--station', '9414290', '--start', '2026-01-01'],
14-
config
15-
)
22+
const { stdout } = await run([
23+
'--station',
24+
'9414290',
25+
'--start',
26+
'2026-01-01'
27+
])
1628
expect(stdout).toMatch(/SAN FRANCISCO/)
1729
})
1830

1931
test('--near 22.24,-75.75', async () => {
20-
const { stdout } = await runCommand(
21-
['extremes', '--near', '22.24,-75.75'],
22-
config
23-
)
32+
const { stdout } = await run(['--near', '22.24,-75.75'])
2433
expect(stdout).toMatch(/Nurse Channel/)
2534
})
2635

@@ -30,7 +39,7 @@ describe('extremes', async () => {
3039
longitude: -77.3524
3140
})
3241

33-
const { stdout, error } = await runCommand(['extremes', '--ip'], config)
42+
const { stdout, error } = await run(['--ip'])
3443
expect(error).toBeUndefined()
3544
expect(stdout).toMatch(/Nassau, New Providence Island/)
3645
})
@@ -39,62 +48,73 @@ describe('extremes', async () => {
3948
test('defaults to today', async () => {
4049
const today = new Date().toISOString().split('T')[0]
4150

42-
const { stdout } = await runCommand(
43-
['extremes', '--station', '9414290'],
44-
config
45-
)
51+
const { stdout } = await run(['--station', '9414290'])
4652
expect(stdout).toMatch(new RegExp(today))
4753
})
4854

4955
test('accepts partial date', async () => {
50-
const { stdout } = await runCommand(
51-
['extremes', '--station', '9414290', '--start', '2025-12-25'],
52-
config
53-
)
56+
const { stdout } = await run([
57+
'--station',
58+
'9414290',
59+
'--start',
60+
'2025-12-25'
61+
])
5462
expect(stdout).toMatch(/2025-12-25/)
5563
})
5664

5765
test('accepts full date', async () => {
58-
const { stdout } = await runCommand(
59-
['extremes', '--station', '9414290', '--start', '2025-12-25T00:00:00Z'],
60-
config
61-
)
66+
const { stdout } = await run([
67+
'--station',
68+
'9414290',
69+
'--start',
70+
'2025-12-25T00:00:00Z'
71+
])
6272
expect(stdout).toMatch(/2025-12-25/)
6373
})
6474

6575
test('with invalid input', async () => {
66-
const { error } = await runCommand(
67-
['extremes', '--station', '9414290', '--start', 'invalid-date'],
68-
config
69-
)
76+
const { error } = await run([
77+
'--station',
78+
'9414290',
79+
'--start',
80+
'invalid-date'
81+
])
7082
expect(error?.message).toMatch(/Invalid time value/)
7183
})
7284
})
7385

7486
describe('--end', () => {
7587
test('defaults to 72 hours from start', async () => {
76-
const { stdout } = await runCommand(
77-
['extremes', '--station', '9414290', '--start', '2025-12-25'],
78-
config
79-
)
88+
const { stdout } = await run([
89+
'--station',
90+
'9414290',
91+
'--start',
92+
'2025-12-25'
93+
])
8094
expect(stdout).toMatch(/2025-12-27/)
8195
})
8296

8397
test('accepts partial date', async () => {
84-
const { stdout } = await runCommand(
85-
[
86-
'extremes',
87-
'--station',
88-
'9414290',
89-
'--start',
90-
'2025-12-25',
91-
'--end',
92-
'2025-12-26'
93-
],
94-
config
95-
)
98+
const { stdout } = await run([
99+
'--station',
100+
'9414290',
101+
'--start',
102+
'2025-12-25',
103+
'--end',
104+
'2025-12-26'
105+
])
96106
expect(stdout).toMatch(/2025-12-25/)
97107
expect(stdout).not.toMatch(/2025-12-26/)
98108
})
99109
})
110+
111+
describe('--format', () => {
112+
test('json', async () => {
113+
const { stdout } = await run(['--station', '9414290', '--format', 'json'])
114+
const result = JSON.parse(stdout)
115+
expect(result.station.name).toMatch(/SAN FRANCISCO/i)
116+
expect(result.datum).toEqual('MLLW')
117+
expect(result.extremes.length).toBeGreaterThan(0)
118+
})
119+
})
100120
})

packages/cli/test/setup.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Force to use cli directory as root for oclif
2+
process.env.OCLIF_TEST_ROOT = new URL('..', import.meta.url).pathname

packages/cli/vitest.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { defineProject } from 'vitest/config'
22

33
export default defineProject({
44
test: {
5-
disableConsoleIntercept: true
5+
disableConsoleIntercept: true,
6+
setupFiles: ['./test/setup.ts']
67
}
78
})

0 commit comments

Comments
 (0)