mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-25 15:15:19 +00:00
feat(test-utils): add mockFn
and mockLogger
utils (#6235)
Co-authored-by: Pooya Parsa <pooya@pi0.io>
This commit is contained in:
parent
1abd5f1959
commit
b31405f6fd
@ -184,6 +184,11 @@ describe('ssr', async () => {
|
|||||||
|
|
||||||
For more usage, please refer to our [tests for Nuxt 3 framework](https://github.com/nuxt/framework/blob/main/test/basic.test.ts).
|
For more usage, please refer to our [tests for Nuxt 3 framework](https://github.com/nuxt/framework/blob/main/test/basic.test.ts).
|
||||||
|
|
||||||
|
## Mock utils
|
||||||
|
|
||||||
|
- `mockFn()`: Returns a mocked function based on test runner.
|
||||||
|
- `mockLogger()`: Mocks logger using [`consola.mockTypes`](https://github.com/unjs/consola#mocktypes) and `mockFn()`. Returns an object of mocked logger types.
|
||||||
|
|
||||||
## Testing in a Browser
|
## Testing in a Browser
|
||||||
|
|
||||||
::alert{icon=🚧}
|
::alert{icon=🚧}
|
||||||
|
@ -5,10 +5,9 @@ export default defineBuildConfig({
|
|||||||
entries: [
|
entries: [
|
||||||
'src/index'
|
'src/index'
|
||||||
],
|
],
|
||||||
dependencies: [
|
|
||||||
],
|
|
||||||
externals: [
|
externals: [
|
||||||
'vitest',
|
'vitest',
|
||||||
|
'jest',
|
||||||
'playwright',
|
'playwright',
|
||||||
'playwright-core',
|
'playwright-core',
|
||||||
'listhen'
|
'listhen'
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@nuxt/kit": "3.0.0-rc.9",
|
"@nuxt/kit": "3.0.0-rc.9",
|
||||||
"@nuxt/schema": "3.0.0-rc.9",
|
"@nuxt/schema": "3.0.0-rc.9",
|
||||||
|
"consola": "^2.15.3",
|
||||||
"defu": "^6.1.0",
|
"defu": "^6.1.0",
|
||||||
"execa": "^6.1.0",
|
"execa": "^6.1.0",
|
||||||
"get-port-please": "^2.6.1",
|
"get-port-please": "^2.6.1",
|
||||||
|
@ -22,7 +22,9 @@ export function createTestContext (options: Partial<TestOptions>): TestContext {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
return setTestContext({ options: _options as TestOptions })
|
return setTestContext({
|
||||||
|
options: _options as TestOptions
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useTestContext (): TestContext {
|
export function useTestContext (): TestContext {
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
export * from './browser'
|
export * from './browser'
|
||||||
export * from './context'
|
export * from './context'
|
||||||
|
export * from './mock'
|
||||||
export * from './nuxt'
|
export * from './nuxt'
|
||||||
export * from './server'
|
export * from './server'
|
||||||
export * from './setup'
|
export * from './setup'
|
||||||
|
16
packages/test-utils/src/mock.ts
Normal file
16
packages/test-utils/src/mock.ts
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
import consola from 'consola'
|
||||||
|
import { useTestContext } from './context'
|
||||||
|
|
||||||
|
export function mockFn () {
|
||||||
|
const ctx = useTestContext()
|
||||||
|
return ctx.mockFn
|
||||||
|
}
|
||||||
|
|
||||||
|
export function mockLogger (): Record<string, Function> {
|
||||||
|
const mocks: any = {}
|
||||||
|
consola.mockTypes((type) => {
|
||||||
|
mocks[type] = mockFn()
|
||||||
|
return mocks[type]
|
||||||
|
})
|
||||||
|
return mocks
|
||||||
|
}
|
@ -1,6 +1,11 @@
|
|||||||
import type { TestHooks } from '../types'
|
import type { TestHooks } from '../types'
|
||||||
|
|
||||||
export default function setupJest (hooks: TestHooks) {
|
export default async function setupJest (hooks: TestHooks) {
|
||||||
|
// @ts-ignore
|
||||||
|
const jest = await import('jest')
|
||||||
|
|
||||||
|
hooks.ctx.mockFn = jest.fn
|
||||||
|
|
||||||
// TODO: add globals existing check to provide better error message
|
// TODO: add globals existing check to provide better error message
|
||||||
// @ts-expect-error jest types
|
// @ts-expect-error jest types
|
||||||
test('setup', hooks.setup, 120 * 1000)
|
test('setup', hooks.setup, 120 * 1000)
|
||||||
|
@ -2,6 +2,9 @@ import type { TestHooks } from '../types'
|
|||||||
|
|
||||||
export default async function setupVitest (hooks: TestHooks) {
|
export default async function setupVitest (hooks: TestHooks) {
|
||||||
const vitest = await import('vitest')
|
const vitest = await import('vitest')
|
||||||
|
|
||||||
|
hooks.ctx.mockFn = vitest.vi.fn
|
||||||
|
|
||||||
vitest.beforeAll(hooks.setup, 120 * 1000)
|
vitest.beforeAll(hooks.setup, 120 * 1000)
|
||||||
vitest.beforeEach(hooks.beforeEach)
|
vitest.beforeEach(hooks.beforeEach)
|
||||||
vitest.afterEach(hooks.afterEach)
|
vitest.afterEach(hooks.afterEach)
|
||||||
|
@ -31,6 +31,7 @@ export interface TestContext {
|
|||||||
browser?: Browser
|
browser?: Browser
|
||||||
url?: string
|
url?: string
|
||||||
serverProcess?: ExecaChildProcess
|
serverProcess?: ExecaChildProcess
|
||||||
|
mockFn?: Function
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface TestHooks {
|
export interface TestHooks {
|
||||||
|
@ -1743,6 +1743,7 @@ __metadata:
|
|||||||
dependencies:
|
dependencies:
|
||||||
"@nuxt/kit": 3.0.0-rc.9
|
"@nuxt/kit": 3.0.0-rc.9
|
||||||
"@nuxt/schema": 3.0.0-rc.9
|
"@nuxt/schema": 3.0.0-rc.9
|
||||||
|
consola: ^2.15.3
|
||||||
defu: ^6.1.0
|
defu: ^6.1.0
|
||||||
execa: ^6.1.0
|
execa: ^6.1.0
|
||||||
get-port-please: ^2.6.1
|
get-port-please: ^2.6.1
|
||||||
|
Loading…
Reference in New Issue
Block a user