|
1 | 1 | import assert from 'node:assert/strict'; |
2 | 2 | import { describe, it } from 'node:test'; |
3 | | -import { isMiddlewarePath } from '../../../dist/core/middleware/vite-plugin.js'; |
| 3 | +import { |
| 4 | + isMiddlewarePath, |
| 5 | + vitePluginMiddleware, |
| 6 | +} from '../../../dist/core/middleware/vite-plugin.js'; |
| 7 | +import { createBasicSettings } from '../test-utils.ts'; |
4 | 8 |
|
5 | 9 | describe('middleware HMR path matching', () => { |
6 | 10 | it('matches middleware.ts (single-file pattern)', () => { |
@@ -35,3 +39,90 @@ describe('middleware HMR path matching', () => { |
35 | 39 | assert.ok(!isMiddlewarePath('components/Header.astro')); |
36 | 40 | }); |
37 | 41 | }); |
| 42 | + |
| 43 | +describe('middleware HMR hotUpdate handler', () => { |
| 44 | + async function getHandler() { |
| 45 | + const settings = await createBasicSettings(); |
| 46 | + const plugin = vitePluginMiddleware({ settings }); |
| 47 | + const hook = plugin.hotUpdate; |
| 48 | + const handler = typeof hook === 'function' ? hook : hook?.handler; |
| 49 | + assert.ok(handler, 'plugin should expose a hotUpdate handler'); |
| 50 | + return handler; |
| 51 | + } |
| 52 | + |
| 53 | + function fakeEnvironment(name = 'ssr') { |
| 54 | + const virtualMod = { id: '\0virtual:astro:middleware' }; |
| 55 | + const invalidated: unknown[] = []; |
| 56 | + const sent: string[] = []; |
| 57 | + return { |
| 58 | + environment: { |
| 59 | + name, |
| 60 | + moduleGraph: { |
| 61 | + getModuleById: () => virtualMod, |
| 62 | + invalidateModule: (mod: unknown) => invalidated.push(mod), |
| 63 | + }, |
| 64 | + hot: { send: (event: string) => sent.push(event) }, |
| 65 | + }, |
| 66 | + invalidated, |
| 67 | + sent, |
| 68 | + }; |
| 69 | + } |
| 70 | + |
| 71 | + it('invalidates the middleware when the change matched modules', async () => { |
| 72 | + const handler = await getHandler(); |
| 73 | + const { environment, invalidated, sent } = fakeEnvironment(); |
| 74 | + |
| 75 | + await handler.call( |
| 76 | + { environment } as any, |
| 77 | + { |
| 78 | + modules: [{ id: '/src/middleware.ts' }], |
| 79 | + } as any, |
| 80 | + ); |
| 81 | + |
| 82 | + assert.equal(invalidated.length, 1); |
| 83 | + assert.deepEqual(sent, ['astro:middleware-updated']); |
| 84 | + }); |
| 85 | + |
| 86 | + it('invalidates the middleware for unrelated in-graph changes too', async () => { |
| 87 | + const handler = await getHandler(); |
| 88 | + const { environment, invalidated, sent } = fakeEnvironment(); |
| 89 | + |
| 90 | + await handler.call( |
| 91 | + { environment } as any, |
| 92 | + { |
| 93 | + // A change that matched modules in the graph is not filtered: it |
| 94 | + // could be a transitive import of the middleware, so the handler |
| 95 | + // invalidates unless the change matched no modules at all. |
| 96 | + modules: [{ id: '/src/pages/index.astro' }], |
| 97 | + } as any, |
| 98 | + ); |
| 99 | + |
| 100 | + assert.equal(invalidated.length, 1); |
| 101 | + assert.deepEqual(sent, ['astro:middleware-updated']); |
| 102 | + }); |
| 103 | + |
| 104 | + it("doesn't invalidate the middleware when the change matched no modules", async () => { |
| 105 | + const handler = await getHandler(); |
| 106 | + const { environment, invalidated, sent } = fakeEnvironment(); |
| 107 | + |
| 108 | + await handler.call({ environment } as any, { modules: [] } as any); |
| 109 | + |
| 110 | + assert.equal(invalidated.length, 0); |
| 111 | + assert.equal(sent.length, 0); |
| 112 | + }); |
| 113 | + |
| 114 | + it('ignores hotUpdates outside server environments', async () => { |
| 115 | + const handler = await getHandler(); |
| 116 | + const { environment, invalidated, sent } = fakeEnvironment('client'); |
| 117 | + |
| 118 | + await handler.call( |
| 119 | + { environment } as any, |
| 120 | + { |
| 121 | + modules: [{ id: '/src/middleware.ts' }], |
| 122 | + } as any, |
| 123 | + ); |
| 124 | + |
| 125 | + assert.equal(invalidated.length, 0); |
| 126 | + assert.equal(sent.length, 0); |
| 127 | + }); |
| 128 | +}); |
0 commit comments