2023-12-19 20:26:13 +00:00
|
|
|
import { fileURLToPath } from 'node:url'
|
2025-01-11 23:53:40 +00:00
|
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
2023-12-19 20:26:13 +00:00
|
|
|
import { normalize } from 'pathe'
|
|
|
|
import { withoutTrailingSlash } from 'ufo'
|
2025-01-11 23:53:40 +00:00
|
|
|
import { asyncNameStorage, logger, useNuxt } from '@nuxt/kit'
|
2023-12-19 20:26:13 +00:00
|
|
|
import { loadNuxt } from '../src'
|
2025-01-11 23:53:40 +00:00
|
|
|
|
2023-12-19 20:26:13 +00:00
|
|
|
const repoRoot = withoutTrailingSlash(normalize(fileURLToPath(new URL('../../../', import.meta.url))))
|
|
|
|
|
2024-05-21 08:18:36 +00:00
|
|
|
vi.stubGlobal('console', {
|
|
|
|
...console,
|
|
|
|
error: vi.fn(console.error),
|
|
|
|
warn: vi.fn(console.warn),
|
|
|
|
})
|
|
|
|
|
2025-01-11 23:50:07 +00:00
|
|
|
const loggerWarn = vi.spyOn(logger, 'warn')
|
2024-05-21 08:18:36 +00:00
|
|
|
vi.mock('pkg-types', async (og) => {
|
|
|
|
const originalPkgTypes = (await og<typeof import('pkg-types')>())
|
|
|
|
return {
|
|
|
|
...originalPkgTypes,
|
|
|
|
readPackageJSON: vi.fn(originalPkgTypes.readPackageJSON),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2025-01-11 23:50:07 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
loggerWarn.mockClear()
|
|
|
|
})
|
2024-05-21 08:18:36 +00:00
|
|
|
afterEach(() => {
|
|
|
|
vi.clearAllMocks()
|
|
|
|
})
|
|
|
|
|
2023-12-19 20:26:13 +00:00
|
|
|
describe('loadNuxt', () => {
|
|
|
|
it('respects hook overrides', async () => {
|
|
|
|
let hookRan = false
|
|
|
|
const nuxt = await loadNuxt({
|
|
|
|
cwd: repoRoot,
|
|
|
|
ready: true,
|
|
|
|
overrides: {
|
|
|
|
hooks: {
|
2025-01-09 14:46:41 +00:00
|
|
|
ready () {
|
2023-12-19 20:26:13 +00:00
|
|
|
hookRan = true
|
2024-04-05 18:08:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-12-19 20:26:13 +00:00
|
|
|
})
|
|
|
|
await nuxt.close()
|
|
|
|
expect(hookRan).toBe(true)
|
|
|
|
})
|
2025-01-09 14:04:13 +00:00
|
|
|
it('load multiple nuxt', async () => {
|
|
|
|
await Promise.all([
|
|
|
|
loadNuxt({
|
|
|
|
cwd: repoRoot,
|
2025-01-09 14:46:41 +00:00
|
|
|
}),
|
2025-01-09 14:04:13 +00:00
|
|
|
loadNuxt({
|
|
|
|
cwd: repoRoot,
|
2025-01-09 14:46:41 +00:00
|
|
|
}),
|
2025-01-09 14:04:13 +00:00
|
|
|
])
|
2025-01-11 23:50:07 +00:00
|
|
|
expect(loggerWarn).not.toHaveBeenCalled()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('expect hooks to get the correct context outside of initNuxt', async () => {
|
|
|
|
const nuxt = await loadNuxt({
|
|
|
|
cwd: repoRoot,
|
|
|
|
})
|
|
|
|
|
|
|
|
// @ts-expect-error - random hook
|
|
|
|
await nuxt.hook('test', () => {
|
|
|
|
const nuxt = useNuxt()
|
|
|
|
expect(asyncNameStorage.getStore()).toBe(nuxt.__name)
|
|
|
|
})
|
|
|
|
|
2025-01-11 23:55:13 +00:00
|
|
|
expect(asyncNameStorage.getStore()).toBeUndefined()
|
|
|
|
|
2025-01-11 23:50:07 +00:00
|
|
|
// @ts-expect-error - random hook
|
|
|
|
await nuxt.callHook('test')
|
|
|
|
|
|
|
|
expect(loggerWarn).not.toHaveBeenCalled()
|
2025-01-09 14:04:13 +00:00
|
|
|
})
|
2023-12-19 20:26:13 +00:00
|
|
|
})
|