Skip to content

Commit

Permalink
fix(module): ensure vi.hoisted is injected after vi import (#1128)
Browse files Browse the repository at this point in the history
  • Loading branch information
OrbisK authored Feb 20, 2025
1 parent be7f642 commit ef4a74e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
1 change: 1 addition & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ export default createConfigForNuxt({
}).append({
rules: {
'@typescript-eslint/no-invalid-void-type': 'off',
'vue/multi-word-component-names': 'off',
},
})
2 changes: 1 addition & 1 deletion examples/app-vitest-full/components/WrapperTests.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function testExpose() {
const someRef = ref('thing')
const modelValue = defineModel({ default: false })
const modelValue = defineModel({ type: Boolean, default: false })
defineExpose({
testExpose,
Expand Down
4 changes: 2 additions & 2 deletions src/module/plugins/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,15 +228,15 @@ export const createMockPlugin = (ctx: MockPluginContext) => createUnplugin(() =>

if (!mockLines.length) return

s.prepend(`vi.hoisted(() => {
s.appendLeft(insertionPoint, `\nvi.hoisted(() => {
if(!globalThis.${HELPER_MOCK_HOIST}){
vi.stubGlobal(${JSON.stringify(HELPER_MOCK_HOIST)}, {})
}
});\n`)

if (!hasViImport) s.prepend(`import {vi} from "vitest";\n`)

s.appendLeft(insertionPoint, mockLines.join('\n') + '\n')
s.appendLeft(insertionPoint, '\n' + mockLines.join('\n') + '\n')

// do an import to trick vite to keep it
// if not, the module won't be mocked
Expand Down
36 changes: 35 additions & 1 deletion test/unit/mock-transform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@ describe('mocking', () => {
})
`)).toMatchInlineSnapshot(`
"import {vi} from "vitest";
vi.hoisted(() => {
if(!globalThis.__NUXT_VITEST_MOCKS){
vi.stubGlobal("__NUXT_VITEST_MOCKS", {})
}
});
vi.mock("bob", async (importOriginal) => {
const mocks = globalThis.__NUXT_VITEST_MOCKS
if (!mocks["bob"]) {
Expand All @@ -77,8 +79,38 @@ describe('mocking', () => {
from: 'bob',
}]
const code = await getResult(`
import { expect, vi } from 'vitest'
import { expect, vi, it } from 'vitest'
mockNuxtImport('useSomeExport', () => 'bob')
it('test', () => {
const a = vi.fn()
})
`)
expect(code).toMatchInlineSnapshot(`
"
import { expect, vi, it } from 'vitest'
vi.hoisted(() => {
if(!globalThis.__NUXT_VITEST_MOCKS){
vi.stubGlobal("__NUXT_VITEST_MOCKS", {})
}
});
vi.mock("bob", async (importOriginal) => {
const mocks = globalThis.__NUXT_VITEST_MOCKS
if (!mocks["bob"]) {
mocks["bob"] = { ...await importOriginal("bob") }
}
mocks["bob"]["useSomeExport"] = await (() => 'bob')();
return mocks["bob"]
});
it('test', () => {
const a = vi.fn()
})
import "bob";"
`)
expect(code).not.toContain('import {vi} from "vitest";')
})
Expand All @@ -101,11 +133,13 @@ describe('mocking', () => {
mockComponent('MyComponent', () => import('./MockComponent.vue'))
`)).toMatchInlineSnapshot(`
"import {vi} from "vitest";
vi.hoisted(() => {
if(!globalThis.__NUXT_VITEST_MOCKS){
vi.stubGlobal("__NUXT_VITEST_MOCKS", {})
}
});
vi.mock("MyComponent", async () => {
const factory = (() => import('./MockComponent.vue'));
const result = typeof factory === 'function' ? await factory() : await factory
Expand Down

0 comments on commit ef4a74e

Please sign in to comment.