diff --git a/docs/content/2.guide/6.going-further/7.testing.md b/docs/content/2.guide/6.going-further/7.testing.md index 0f4406a7b2..4fa4fb14cc 100644 --- a/docs/content/2.guide/6.going-further/7.testing.md +++ b/docs/content/2.guide/6.going-further/7.testing.md @@ -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=🚧} diff --git a/packages/test-utils/build.config.ts b/packages/test-utils/build.config.ts index 038925e919..586a5fe31d 100644 --- a/packages/test-utils/build.config.ts +++ b/packages/test-utils/build.config.ts @@ -5,10 +5,9 @@ export default defineBuildConfig({ entries: [ 'src/index' ], - dependencies: [ - ], externals: [ 'vitest', + 'jest', 'playwright', 'playwright-core', 'listhen' diff --git a/packages/test-utils/package.json b/packages/test-utils/package.json index 71e8b52211..49e8f47268 100644 --- a/packages/test-utils/package.json +++ b/packages/test-utils/package.json @@ -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", diff --git a/packages/test-utils/src/context.ts b/packages/test-utils/src/context.ts index 3a09ecab5f..ad987d1ec9 100644 --- a/packages/test-utils/src/context.ts +++ b/packages/test-utils/src/context.ts @@ -22,7 +22,9 @@ export function createTestContext (options: Partial): TestContext { } }) - return setTestContext({ options: _options as TestOptions }) + return setTestContext({ + options: _options as TestOptions + }) } export function useTestContext (): TestContext { diff --git a/packages/test-utils/src/index.ts b/packages/test-utils/src/index.ts index 55d9ebae53..3238c8d520 100644 --- a/packages/test-utils/src/index.ts +++ b/packages/test-utils/src/index.ts @@ -1,5 +1,6 @@ export * from './browser' export * from './context' +export * from './mock' export * from './nuxt' export * from './server' export * from './setup' diff --git a/packages/test-utils/src/mock.ts b/packages/test-utils/src/mock.ts new file mode 100644 index 0000000000..52843da05a --- /dev/null +++ b/packages/test-utils/src/mock.ts @@ -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 { + const mocks: any = {} + consola.mockTypes((type) => { + mocks[type] = mockFn() + return mocks[type] + }) + return mocks +} diff --git a/packages/test-utils/src/setup/jest.ts b/packages/test-utils/src/setup/jest.ts index 67bec30a58..01a9f65438 100644 --- a/packages/test-utils/src/setup/jest.ts +++ b/packages/test-utils/src/setup/jest.ts @@ -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) diff --git a/packages/test-utils/src/setup/vitest.ts b/packages/test-utils/src/setup/vitest.ts index 60a3b340ff..5fa16f6d9e 100644 --- a/packages/test-utils/src/setup/vitest.ts +++ b/packages/test-utils/src/setup/vitest.ts @@ -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) diff --git a/packages/test-utils/src/types.ts b/packages/test-utils/src/types.ts index 8e41fa4a9f..ab8612ff21 100644 --- a/packages/test-utils/src/types.ts +++ b/packages/test-utils/src/types.ts @@ -31,6 +31,7 @@ export interface TestContext { browser?: Browser url?: string serverProcess?: ExecaChildProcess + mockFn?: Function } export interface TestHooks { diff --git a/yarn.lock b/yarn.lock index f23a6f7658..720234deb9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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