|
1 | 1 | import { existsSync, readdirSync } from 'node:fs'
|
2 | 2 | import { posix, win32 } from 'node:path'
|
3 | 3 | import { fileURLToPath } from 'node:url'
|
4 |
| -import { describe, expect } from 'vitest' |
| 4 | +import { describe, expect, vi } from 'vitest' |
5 | 5 | import { isWindows } from '../../../../shared/utils'
|
6 | 6 | import { createModuleRunnerTester } from './utils'
|
7 | 7 |
|
@@ -29,17 +29,36 @@ describe('module runner initialization', async () => {
|
29 | 29 | const mod = await runner.import('virtual:test')
|
30 | 30 | expect(mod.msg).toBe('virtual')
|
31 | 31 |
|
32 |
| - // virtual module query is not supported out of the box |
33 |
| - // (`?t=...` was working on Vite 5 ssrLoadModule as `transformRequest` strips off timestamp query) |
34 |
| - await expect(() => |
35 |
| - runner.import(`virtual:test?t=${Date.now()}`), |
36 |
| - ).rejects.toMatchObject({ |
37 |
| - message: expect.stringContaining('cannot find entry point module'), |
38 |
| - }) |
| 32 | + // already resolved id works similar to `transformRequest` |
| 33 | + expect(await runner.import(`\0virtual:normal`)).toMatchInlineSnapshot(` |
| 34 | + { |
| 35 | + "default": "ok", |
| 36 | + } |
| 37 | + `) |
| 38 | + |
| 39 | + // escaped virtual module id works |
| 40 | + expect(await runner.import(`/@id/__x00__virtual:normal`)) |
| 41 | + .toMatchInlineSnapshot(` |
| 42 | + { |
| 43 | + "default": "ok", |
| 44 | + } |
| 45 | + `) |
| 46 | + |
| 47 | + // timestamp query works |
| 48 | + expect(await runner.import(`virtual:normal?t=${Date.now()}`)) |
| 49 | + .toMatchInlineSnapshot(` |
| 50 | + { |
| 51 | + "default": "ok", |
| 52 | + } |
| 53 | + `) |
| 54 | + |
| 55 | + // other arbitrary queries don't work |
39 | 56 | await expect(() =>
|
40 |
| - runner.import('virtual:test?abcd=1234'), |
| 57 | + runner.import('virtual:normal?abcd=1234'), |
41 | 58 | ).rejects.toMatchObject({
|
42 |
| - message: expect.stringContaining('cannot find entry point module'), |
| 59 | + message: expect.stringContaining( |
| 60 | + 'Failed to load url virtual:normal?abcd=1234', |
| 61 | + ), |
43 | 62 | })
|
44 | 63 | })
|
45 | 64 |
|
@@ -338,3 +357,38 @@ describe('resolveId absolute path entry', async () => {
|
338 | 357 | expect(mod.name).toMatchInlineSnapshot(`"virtual:basic"`)
|
339 | 358 | })
|
340 | 359 | })
|
| 360 | + |
| 361 | +describe('virtual module hmr', async () => { |
| 362 | + let state = 'init' |
| 363 | + |
| 364 | + const it = await createModuleRunnerTester({ |
| 365 | + plugins: [ |
| 366 | + { |
| 367 | + name: 'test-resolevId', |
| 368 | + enforce: 'pre', |
| 369 | + resolveId(source) { |
| 370 | + if (source === 'virtual:test') { |
| 371 | + return '\0' + source |
| 372 | + } |
| 373 | + }, |
| 374 | + load(id) { |
| 375 | + if (id === '\0virtual:test') { |
| 376 | + return `export default ${JSON.stringify(state)}` |
| 377 | + } |
| 378 | + }, |
| 379 | + }, |
| 380 | + ], |
| 381 | + }) |
| 382 | + |
| 383 | + it('full reload', async ({ server, runner }) => { |
| 384 | + const mod = await runner.import('virtual:test') |
| 385 | + expect(mod.default).toBe('init') |
| 386 | + state = 'reloaded' |
| 387 | + server.environments.ssr.moduleGraph.invalidateAll() |
| 388 | + server.environments.ssr.hot.send({ type: 'full-reload' }) |
| 389 | + await vi.waitFor(() => { |
| 390 | + const mod = runner.evaluatedModules.getModuleById('\0virtual:test') |
| 391 | + expect(mod?.exports.default).toBe('reloaded') |
| 392 | + }) |
| 393 | + }) |
| 394 | +}) |
0 commit comments