diff --git a/packages/vitest/src/runtime/moduleRunner/moduleEvaluator.ts b/packages/vitest/src/runtime/moduleRunner/moduleEvaluator.ts index eddb5e5ec..9456221c5 100644 --- a/packages/vitest/src/runtime/moduleRunner/moduleEvaluator.ts +++ b/packages/vitest/src/runtime/moduleRunner/moduleEvaluator.ts @@ -522,19 +522,24 @@ function removeStyle(id: string) { } } +function createHotContext() { + return { + data: {}, + accept: () => {}, + acceptExports: () => {}, + prune: () => {}, + dispose: () => {}, + decline: () => {}, + invalidate: () => {}, + on: () => {}, + off: () => {}, + send: () => {}, + } +} + const defaultClientStub = { injectQuery: (id: string) => id, - createHotContext: () => { - return { - accept: () => {}, - prune: () => {}, - dispose: () => {}, - decline: () => {}, - invalidate: () => {}, - on: () => {}, - send: () => {}, - } - }, + createHotContext, updateStyle: () => {}, removeStyle: () => {}, } @@ -551,7 +556,7 @@ export function getDefaultRequestStubs(context?: vm.Context): Record ({ ...defaultClient, updateStyle: ${updateStyle.toString()}, removeStyle: ${removeStyle.toString()} })`, + `(defaultClient) => ({ ...defaultClient, createHotContext: ${createHotContext.toString()}, updateStyle: ${updateStyle.toString()}, removeStyle: ${removeStyle.toString()} })`, context, )(defaultClientStub) return { diff --git a/test/e2e/test/import-meta-hot.test.ts b/test/e2e/test/import-meta-hot.test.ts new file mode 100644 index 000000000..52cc76e5d --- /dev/null +++ b/test/e2e/test/import-meta-hot.test.ts @@ -0,0 +1,39 @@ +import { expect, test } from 'vitest' +import { runInlineTests } from '../../test-utils' + +test.for(['forks', 'vmThreads'] as const)( + 'import.meta.hot has a data object per module in %s', + async (pool) => { + const testFile = (environment: string) => ` + // @vitest-environment ${environment} + import { expect, test } from 'vitest' + import { hot } from './hot.js' + + test('import.meta.hot.data', () => { + expect(import.meta.hot.data).toStrictEqual({}) + expect(hot.data).toStrictEqual({}) + expect(hot.data).not.toBe(import.meta.hot.data) + }) + ` + const { stderr, testTree } = await runInlineTests( + { + 'hot.js': `export const hot = import.meta.hot`, + 'jsdom.test.js': testFile('jsdom'), + 'happy-dom.test.js': testFile('happy-dom'), + }, + { pool }, + ) + + expect(stderr).toBe('') + expect(testTree()).toMatchInlineSnapshot(` + { + "happy-dom.test.js": { + "import.meta.hot.data": "passed", + }, + "jsdom.test.js": { + "import.meta.hot.data": "passed", + }, + } + `) + }, +)