test: refactor hmr-ssr tests (#18075)

This commit is contained in:
翠 / green 2024-09-12 18:34:07 +09:00 committed by GitHub
parent 41180d0273
commit e7d51a361f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 731 additions and 853 deletions

File diff suppressed because it is too large Load Diff

View File

@ -8,4 +8,12 @@ if (import.meta.hot) {
rerender({ child: newMod.value })
})
import.meta.hot.dispose(() => {
log('file-delete-restore/child.js is disposed')
})
import.meta.hot.prune(() => {
log('file-delete-restore/child.js is pruned')
})
}

View File

@ -142,7 +142,9 @@ export function editFile(
}
export function addFile(filename: string, content: string): void {
fs.writeFileSync(path.resolve(testDir, filename), content)
const resolvedFilename = path.resolve(testDir, filename)
fs.mkdirSync(path.dirname(resolvedFilename), { recursive: true })
fs.writeFileSync(resolvedFilename, content)
}
export function removeFile(filename: string): void {

View File

@ -82,6 +82,14 @@ export function setViteUrl(url: string): void {
beforeAll(async (s) => {
const suite = s as File
testPath = suite.filepath!
testName = slash(testPath).match(/playground\/([\w-]+)\//)?.[1]
testDir = path.dirname(testPath)
if (testName) {
testDir = path.resolve(workspaceRoot, 'playground-temp', testName)
}
// skip browser setup for non-playground tests
// TODO: ssr playground?
if (
@ -124,15 +132,9 @@ beforeAll(async (s) => {
browserErrors.push(error)
})
testPath = suite.filepath!
testName = slash(testPath).match(/playground\/([\w-]+)\//)?.[1]
testDir = path.dirname(testPath)
// if this is a test placed under playground/xxx/__tests__
// start a vite server in that directory.
if (testName) {
testDir = path.resolve(workspaceRoot, 'playground-temp', testName)
// when `root` dir is present, use it as vite's root
const testCustomRoot = path.resolve(testDir, 'root')
rootDir = fs.existsSync(testCustomRoot) ? testCustomRoot : testDir