also looking at this
test: stop leaking mocked module state across test files
#2274
8 files
+77
-67
| @@ -33,9 +33,6 @@ function mockVitePluginRpx() { | ||
| 33 | 33 | } |
| 34 | 34 | } |
| 35 | 35 | |
| 36 | // Expose a named export for the mock to satisfy the import | |
| 37 | const VitePluginRpx = mock(mockVitePluginRpx) | |
| 38 | ||
| 39 | 36 | // Mock dependencies |
| 40 | 37 | const mockStartProxies = mock(async (_opts?: any) => {}) |
| 41 | 38 | const mockCleanup = mock(async (_opts?: any) => {}) |
| @@ -61,41 +58,16 @@ const mockHttpServer = { | ||
| 61 | 58 | }), |
| 62 | 59 | } |
| 63 | 60 | |
| 64 | // Set up mocks | |
| 65 | mock.module('@stacksjs/rpx', () => ({ | |
| 66 | startProxies: mockStartProxies, | |
| 67 | cleanup: mockCleanup, | |
| 68 | checkExistingCertificates: mockCheckExistingCertificates, | |
| 69 | checkHosts: mockCheckHosts, | |
| 70 | })) | |
| 71 | ||
| 72 | const 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 | } | |
| 90 | mock.module('node:process', () => ({ | |
| 91 | ...processMock, | |
| 92 | default: processMock, | |
| 93 | })) | |
| 94 | ||
| 95 | // Mock the actual plugin import | |
| 96 | mock.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). | |
| 99 | 71 | |
| 100 | 72 | describe('Vite Plugin RPX', () => { |
| 101 | 73 | let plugin: ReturnType<typeof mockVitePluginRpx> |