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

Commit

Permalink
feat(test-utils): add mockFn and mockLogger utils (#6235)
Browse files Browse the repository at this point in the history
Co-authored-by: Pooya Parsa <[email protected]>
  • Loading branch information
ricardogobbosouza and pi0 authored Sep 5, 2022
1 parent 1abd5f1 commit b31405f
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 4 deletions.
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.mockTypes`](https://github.com/unjs/consola#mocktypes) and `mockFn()`. Returns an object of mocked logger types.

## Testing in a Browser

::alert{icon=🚧}
Expand Down
3 changes: 1 addition & 2 deletions packages/test-utils/build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ export default defineBuildConfig({
entries: [
'src/index'
],
dependencies: [
],
externals: [
'vitest',
'jest',
'playwright',
'playwright-core',
'listhen'
Expand Down
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.9",
"@nuxt/schema": "3.0.0-rc.9",
"consola": "^2.15.3",
"defu": "^6.1.0",
"execa": "^6.1.0",
"get-port-please": "^2.6.1",
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
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1743,6 +1743,7 @@ __metadata:
dependencies:
"@nuxt/kit": 3.0.0-rc.9
"@nuxt/schema": 3.0.0-rc.9
consola: ^2.15.3
defu: ^6.1.0
execa: ^6.1.0
get-port-please: ^2.6.1
Expand Down

0 comments on commit b31405f

Please sign in to comment.