|
1 | 1 | import fs from 'node:fs' |
2 | 2 | import path from 'node:path' |
3 | | -import { describe, expect, onTestFinished, test } from 'vitest' |
| 3 | +import { describe, expect, onTestFinished, test, vi } from 'vitest' |
4 | 4 | import { FS_PREFIX } from '../../../constants' |
5 | 5 | import { createServer } from '../../../server' |
6 | 6 |
|
7 | 7 | const FIXTURE_DIR = path.resolve(import.meta.dirname, 'fixtures') |
8 | 8 | const HTML_PATH = path.resolve(FIXTURE_DIR, 'root/index.html') |
9 | 9 | const HTML_CONTENT = fs.readFileSync(HTML_PATH, 'utf-8') |
| 10 | +const OUTSIDE_HTML_PATH = path.resolve(FIXTURE_DIR, 'outside.html') |
| 11 | +const OUTSIDE_HTML_CONTENT = fs.readFileSync(OUTSIDE_HTML_PATH, 'utf-8') |
10 | 12 | const VITE_PACKAGE_DIR = path.resolve(import.meta.dirname, '../../../../..') |
11 | 13 |
|
12 | 14 | async function createTestServer(rootDir?: string) { |
@@ -107,6 +109,32 @@ describe('indexHtml middleware — inline script proxy cache', () => { |
107 | 109 | } |
108 | 110 | }) |
109 | 111 |
|
| 112 | +describe('indexHtml middleware — file watching', () => { |
| 113 | + test('watches an /@fs/ HTML file outside root', async () => { |
| 114 | + const server = await createTestServer() |
| 115 | + const addSpy = vi |
| 116 | + .spyOn(server.watcher, 'add') |
| 117 | + .mockImplementation(() => server.watcher) |
| 118 | + const fsUrl = path.posix.join(FS_PREFIX, OUTSIDE_HTML_PATH) |
| 119 | + |
| 120 | + await server.transformIndexHtml(fsUrl, OUTSIDE_HTML_CONTENT) |
| 121 | + |
| 122 | + expect(addSpy).toHaveBeenCalledWith(OUTSIDE_HTML_PATH) |
| 123 | + }) |
| 124 | + |
| 125 | + test('does not watch a directory for a root URL with a query', async () => { |
| 126 | + const server = await createTestServer() |
| 127 | + const addSpy = vi |
| 128 | + .spyOn(server.watcher, 'add') |
| 129 | + .mockImplementation(() => server.watcher) |
| 130 | + const html = '<style>body { color: red; }</style>' |
| 131 | + |
| 132 | + await server.transformIndexHtml('/?foo=bar', html) |
| 133 | + |
| 134 | + expect(addSpy).not.toHaveBeenCalledWith('/') |
| 135 | + }) |
| 136 | +}) |
| 137 | + |
110 | 138 | describe('indexHtml middleware — HMR timestamp injection with non-root base', () => { |
111 | 139 | test('entry script URL gets the lastHMRTimestamp query when base is not root', async () => { |
112 | 140 | const root = path.resolve(import.meta.dirname, 'fixtures/base-root') |
|
0 commit comments