Skip to content
This repository was archived by the owner on Apr 6, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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