Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

feat(test-utils): add mockFn and mockLogger utils #6235

Merged
merged 15 commits into from
Sep 5, 2022
5 changes: 5 additions & 0 deletions docs/content/2.guide/6.going-further/7.testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

## Mock utils

- `mockFn()`: Returns a mocked function based on test runner.
- `mockLogger()`: Mocks logger using [consola.mocklogs](https://github.com/unjs/consola#mocktypes) and `mockFn()`. Returns an object of mocked logger types.
pi0 marked this conversation as resolved.
Show resolved Hide resolved

## Testing in a Browser

::alert{icon=🚧}
Expand Down
4 changes: 3 additions & 1 deletion packages/test-utils/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions packages/test-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './browser'
export * from './context'
export * from './mock'
export * from './nuxt'
export * from './server'
export * from './setup'
Expand Down
16 changes: 16 additions & 0 deletions packages/test-utils/src/mock.ts
Original file line number Diff line number Diff line change
@@ -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
}
7 changes: 6 additions & 1 deletion packages/test-utils/src/setup/jest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
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
// @ts-expect-error jest types
test('setup', hooks.setup, 120 * 1000)
Expand Down
3 changes: 3 additions & 0 deletions packages/test-utils/src/setup/vitest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import type { TestHooks } from '../types'

export default async function setupVitest (hooks: TestHooks) {
const vitest = await import('vitest')

hooks.ctx.mockFn = vitest.vi.fn

vitest.beforeAll(hooks.setup, 120 * 1000)
vitest.beforeEach(hooks.beforeEach)
vitest.afterEach(hooks.afterEach)
Expand Down
1 change: 1 addition & 0 deletions packages/test-utils/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface TestContext {
browser?: Browser
url?: string
serverProcess?: ExecaChildProcess
mockFn?: Function
}

export interface TestHooks {
Expand Down