ReviewOS

also looking at this

stacks/rpx

test: stop leaking mocked module state across test files

#2274
Merged glennmichael123 wants to merge fix/2270-2271-test-state-leaks into main
8 files +77 -67
packages/vite-plugin/test/vite-plugin.test.tsmodified+10-38
Changes to packages/vite-plugin/test/vite-plugin.test.ts
@@ -33,9 +33,6 @@ function mockVitePluginRpx() {
3333 }
3434}
3535
36// Expose a named export for the mock to satisfy the import
37const VitePluginRpx = mock(mockVitePluginRpx)
38
3936// Mock dependencies
4037const mockStartProxies = mock(async (_opts?: any) => {})
4138const mockCleanup = mock(async (_opts?: any) => {})
@@ -61,41 +58,16 @@ const mockHttpServer = {
6158 }),
6259}
6360
64// Set up mocks
65mock.module('@stacksjs/rpx', () => ({
66 startProxies: mockStartProxies,
67 cleanup: mockCleanup,
68 checkExistingCertificates: mockCheckExistingCertificates,
69 checkHosts: mockCheckHosts,
70}))
71
72const processMock = {
73 ...process,
74 on: (event: string, cb: SignalHandler) => {
75 processListeners[event] = processListeners[event] || []
76 processListeners[event].push(cb)
77 },
78 once: (event: string, cb: SignalHandler) => {
79 processListeners[event] = processListeners[event] || []
80 processListeners[event].push(cb)
81 },
82 emit: (event: string) => {
83 if (processListeners[event]) {
84 processListeners[event].forEach(cb => cb())
85 }
86 },
87 listeners: (event: string) => processListeners[event] || [],
88 exit: mock(() => {}),
89}
90mock.module('node:process', () => ({
91 ...processMock,
92 default: processMock,
93}))
94
95// Mock the actual plugin import
96mock.module('../src', () => ({
97 VitePluginRpx,
98}))
61// No `mock.module` here on purpose. Nothing in this file imports `@stacksjs/rpx`,
62// `node:process` or `../src` — the tests drive the locally defined
63// `mockVitePluginRpx()`, which closes over the mock fns directly — so the three
64// module mocks that used to live here were dead weight. They were not harmless:
65// `mock.module` is global and permanent (`mock.restore()` does not undo it), and
66// the `node:process` one spread `{ ...process }`, dropping every
67// EventEmitter.prototype method. That only stayed invisible because `bun run
68// test` runs this directory as its own process; a plain `bun test` at the repo
69// root puts it in with the rpx suite, where ten src modules import
70// `node:process` (#2270).
9971
10072describe('Vite Plugin RPX', () => {
10173 let plugin: ReturnType<typeof mockVitePluginRpx>