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
1 change: 1 addition & 0 deletions packages/test-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"dependencies": {
"@nuxt/kit": "3.0.0-rc.8",
"@nuxt/schema": "3.0.0-rc.8",
"consola": "^2.15.3",
"defu": "^6.1.0",
"execa": "^6.1.0",
"get-port-please": "^2.6.1",
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
33 changes: 33 additions & 0 deletions packages/test-utils/src/mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import consola from 'consola'
import type { Consola } from 'consola'
import { useTestContext } from './context'

export async function mockFn () {
const { runner } = useTestContext().options

if (runner === 'jest') {
// @ts-ignore jest is not installed
const jest = await import('jest')
return jest.fn()
}

if (runner === 'vitest') {
const vitest = await import('vitest')
return vitest.vi.fn()
}

return () => {}
}
pi0 marked this conversation as resolved.
Show resolved Hide resolved

export async function mockLogger (): Promise<Consola> {
const mockedConsole: any = {}

const fn = await mockFn()

consola.mockTypes((type) => {
mockedConsole[type] = mockedConsole[type] || fn
return mockedConsole[type]
})

return mockedConsole
}
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1807,6 +1807,7 @@ __metadata:
dependencies:
"@nuxt/kit": 3.0.0-rc.8
"@nuxt/schema": 3.0.0-rc.8
consola: ^2.15.3
defu: ^6.1.0
execa: ^6.1.0
get-port-please: ^2.6.1
Expand Down