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

Co-authored-by: Pooya Parsa <pooya@pi0.io>
This commit is contained in:
Ricardo Gobbo de Souza 2022-09-05 05:51:00 -03:00 committed by GitHub
parent 1abd5f1959
commit b31405f6fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 38 additions and 4 deletions

View File

@ -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=🚧}

View File

@ -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'

View File

@ -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",

View File

@ -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 {

View File

@ -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'

View 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
}

View File

@ -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)

View File

@ -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)

View File

@ -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 {

View File

@ -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