Skip to content

Commit

Permalink
test: add tests for no full reload
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Feb 13, 2024
1 parent a2713ca commit 823df65
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/vite/src/node/ssr/runtime/moduleCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export class ModuleCacheMap extends Map<string, ModuleCache> {

const fileModule = this.getByModuleId(importedId)
const importers = fileModule?.importers
// console.log(this, { importedId, importers: fileModule?.importers, imports: fileModule?.imports })

if (!importers) return false

if (importers.has(importedBy)) return true
Expand Down
56 changes: 56 additions & 0 deletions playground/hmr-ssr/__tests__/hmr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,54 @@ describe('acceptExports', () => {
)
})
})

describe("doesn't reload if files not in the the entrypoint importers chain is changed", async () => {
const testFile = 'non-tested/index.js'

beforeAll(async () => {
// so it's in the module graph
await server.transformRequest(testFile, { ssr: true })
await server.transformRequest('non-tested/dep.js', { ssr: true })
})

test('does not full reload', async () => {
editFile(
testFile,
(code) => code + '\n\nexport const query5 = "query5"',
)
const start = Date.now()
// for 2 seconds check that there is no log about the file being reloaded
while (Date.now() - start < 2000) {
if (
clientLogs.some(
(log) =>
log.match(PROGRAM_RELOAD) ||
log.includes('non-tested/index.js'),
)
) {
throw new Error('File was reloaded')
}
await new Promise((r) => setTimeout(r, 100))
}
}, 5_000)

test('does not update', async () => {
editFile('non-tested/dep.js', (code) => code + '//comment')
const start = Date.now()
// for 2 seconds check that there is no log about the file being reloaded
while (Date.now() - start < 2000) {
if (
clientLogs.some(
(log) =>
log.match(PROGRAM_RELOAD) || log.includes('non-tested/dep.js'),
)
) {
throw new Error('File was updated')
}
await new Promise((r) => setTimeout(r, 100))
}
}, 5_000)
})
})

test('accepts itself when imported for side effects only (no bindings imported)', async () => {
Expand Down Expand Up @@ -1049,6 +1097,14 @@ async function setupViteRuntime(
noDiscovery: true,
include: [],
},
plugins: [
{
name: 'tessss',
handleHotUpdate({ file }) {
console.log('hot', file)
},
},
],
...serverOptions,
})

Expand Down
3 changes: 3 additions & 0 deletions playground/hmr-ssr/non-tested/dep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const test = 'true'

import.meta.hot.accept()
9 changes: 9 additions & 0 deletions playground/hmr-ssr/non-tested/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { test } from './dep.js'

function main() {
test()
}

main()

import.meta.hot.accept('./dep.js')

0 comments on commit 823df65

Please sign in to comment.