Skip to content

Commit a842fbe

Browse files
authored
tests: scope MSW server and db reset to the one spec that uses them (#3335)
We were curious why node tests would take longer than browser tests. Turns out we were setting up and tearing down the mock API and resetting the database after every unit test. This PR moves that out of global setup into the one file that actually needs it. ### Before `npm test run -- ip` <img width="492" height="91" alt="image" src="https://github.com/user-attachments/assets/3f6549f3-0a1a-4e67-b6ac-8483b60a5491" /> ### After `npm test run -- ip` <img width="492" height="89" alt="image" src="https://github.com/user-attachments/assets/fe905db6-66b5-4c6e-9535-10cc6a9c0d7c" />
1 parent 218f3c0 commit a842fbe

3 files changed

Lines changed: 16 additions & 14 deletions

File tree

app/api/__tests__/client.spec.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,25 @@
55
*
66
* Copyright Oxide Computer Company
77
*/
8-
import { describe, expect, it } from 'vitest'
8+
import { afterAll, afterEach, beforeAll, describe, expect, it } from 'vitest'
99

1010
import { project } from '@oxide/api-mocks'
1111

1212
import { api, q } from '..'
13-
import { overrideOnce } from '../../../test/unit/server'
13+
import { resetDb } from '../../../mock-api/msw/db'
14+
import { overrideOnce, server } from '../../../test/unit/server'
1415
import { processServerError } from '../errors'
1516

17+
// These are the only unit tests that make requests, so the MSW server
18+
// lifecycle lives here rather than in the global setup file — resetDb clones
19+
// the whole mock db, which is too slow to run after every test suite-wide.
20+
beforeAll(() => server.listen())
21+
afterEach(() => {
22+
resetDb()
23+
server.resetHandlers()
24+
})
25+
afterAll(() => server.close())
26+
1627
// useApiQuery and useApiMutation are almost entirely typed wrappers around React
1728
// Query's useQuery and useMutation, so they're exercised end-to-end by the
1829
// Playwright suite (every error toast goes through this path). The logic worth

app/api/__tests__/safety.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,13 @@ it('mock-api is only referenced in test files', () => {
6767
[
6868
"AGENTS.md",
6969
"README.md",
70+
"app/api/__tests__/client.spec.ts",
7071
"app/main.tsx",
7172
"app/msw-mock-api.ts",
7273
"docs/mock-api-differences.md",
7374
"package.json",
7475
"test/e2e/utils.ts",
7576
"test/unit/server.ts",
76-
"test/unit/setup.ts",
7777
"tools/start_mock_api.ts",
7878
"tsconfig.json",
7979
]

test/unit/setup.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@
1212
*/
1313
import '@testing-library/jest-dom/vitest'
1414
import { cleanup } from '@testing-library/react'
15-
import { afterAll, afterEach, beforeAll, vi } from 'vitest'
16-
17-
import { resetDb } from '../../mock-api/msw/db'
18-
import { server } from './server'
15+
import { afterEach, vi } from 'vitest'
1916

2017
// xterm calls this when it's imported, so defining it here suppresses
2118
// an error that the method is not implemented
@@ -43,10 +40,4 @@ globalThis.ResizeObserver = class {
4340
disconnect() {}
4441
}
4542

46-
beforeAll(() => server.listen())
47-
afterEach(() => {
48-
resetDb()
49-
cleanup()
50-
server.resetHandlers()
51-
})
52-
afterAll(() => server.close())
43+
afterEach(() => cleanup())

0 commit comments

Comments
 (0)