Skip to content

Commit 3a8289a

Browse files
authored
fix: add data to the import.meta.hot stub (#11590)
1 parent ca08135 commit 3a8289a

2 files changed

Lines changed: 56 additions & 12 deletions

File tree

‎packages/vitest/src/runtime/moduleRunner/moduleEvaluator.ts‎

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -522,19 +522,24 @@ function removeStyle(id: string) {
522522
}
523523
}
524524

525+
function createHotContext() {
526+
return {
527+
data: {},
528+
accept: () => {},
529+
acceptExports: () => {},
530+
prune: () => {},
531+
dispose: () => {},
532+
decline: () => {},
533+
invalidate: () => {},
534+
on: () => {},
535+
off: () => {},
536+
send: () => {},
537+
}
538+
}
539+
525540
const defaultClientStub = {
526541
injectQuery: (id: string) => id,
527-
createHotContext: () => {
528-
return {
529-
accept: () => {},
530-
prune: () => {},
531-
dispose: () => {},
532-
decline: () => {},
533-
invalidate: () => {},
534-
on: () => {},
535-
send: () => {},
536-
}
537-
},
542+
createHotContext,
538543
updateStyle: () => {},
539544
removeStyle: () => {},
540545
}
@@ -551,7 +556,7 @@ export function getDefaultRequestStubs(context?: vm.Context): Record<string, any
551556
}
552557
}
553558
const clientStub = vm.runInContext(
554-
`(defaultClient) => ({ ...defaultClient, updateStyle: ${updateStyle.toString()}, removeStyle: ${removeStyle.toString()} })`,
559+
`(defaultClient) => ({ ...defaultClient, createHotContext: ${createHotContext.toString()}, updateStyle: ${updateStyle.toString()}, removeStyle: ${removeStyle.toString()} })`,
555560
context,
556561
)(defaultClientStub)
557562
return {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { expect, test } from 'vitest'
2+
import { runInlineTests } from '../../test-utils'
3+
4+
test.for(['forks', 'vmThreads'] as const)(
5+
'import.meta.hot has a data object per module in %s',
6+
async (pool) => {
7+
const testFile = (environment: string) => `
8+
// @vitest-environment ${environment}
9+
import { expect, test } from 'vitest'
10+
import { hot } from './hot.js'
11+
12+
test('import.meta.hot.data', () => {
13+
expect(import.meta.hot.data).toStrictEqual({})
14+
expect(hot.data).toStrictEqual({})
15+
expect(hot.data).not.toBe(import.meta.hot.data)
16+
})
17+
`
18+
const { stderr, testTree } = await runInlineTests(
19+
{
20+
'hot.js': `export const hot = import.meta.hot`,
21+
'jsdom.test.js': testFile('jsdom'),
22+
'happy-dom.test.js': testFile('happy-dom'),
23+
},
24+
{ pool },
25+
)
26+
27+
expect(stderr).toBe('')
28+
expect(testTree()).toMatchInlineSnapshot(`
29+
{
30+
"happy-dom.test.js": {
31+
"import.meta.hot.data": "passed",
32+
},
33+
"jsdom.test.js": {
34+
"import.meta.hot.data": "passed",
35+
},
36+
}
37+
`)
38+
},
39+
)

0 commit comments

Comments
 (0)