From a3b3e35ffd750dda95322e8a0f0bb29c9a500215 Mon Sep 17 00:00:00 2001 From: Ricardo Gobbo de Souza Date: Fri, 29 Jul 2022 10:42:47 -0300 Subject: [PATCH 01/12] feat(test-utils): add mocks --- packages/test-utils/package.json | 1 + packages/test-utils/src/index.ts | 1 + packages/test-utils/src/mocks.ts | 30 ++++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 packages/test-utils/src/mocks.ts diff --git a/packages/test-utils/package.json b/packages/test-utils/package.json index ff2b59073c8..b16203c2766 100644 --- a/packages/test-utils/package.json +++ b/packages/test-utils/package.json @@ -15,6 +15,7 @@ "dependencies": { "@nuxt/kit": "^3.0.0-rc.6", "@nuxt/schema": "^3.0.0-rc.6", + "consola": "^2.15.2", "defu": "^6.0.0", "execa": "^6.1.0", "get-port-please": "^2.5.0", diff --git a/packages/test-utils/src/index.ts b/packages/test-utils/src/index.ts index 55d9ebae535..7186739acde 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 './mocks' export * from './nuxt' export * from './server' export * from './setup' diff --git a/packages/test-utils/src/mocks.ts b/packages/test-utils/src/mocks.ts new file mode 100644 index 00000000000..8e1a940a578 --- /dev/null +++ b/packages/test-utils/src/mocks.ts @@ -0,0 +1,30 @@ +import consola from 'consola' +import { useTestContext } from './context' + +const mockFnVitest = async () => { + const vitest = await import('vitest') + return vitest.vi.fn() +} + +const mockFnJest = async () =>{ + const jest = await import('jest') + return jest.fn() +} + +export const mockFnMaps = { + jest: mockFnJest, + vitest: mockFnVitest +} + +export function mockLogger (): typeof consola { + const mock = {} + const mockFn = mockFnMaps[useTestContext().options.runner] + + consola.mockTypes((type) => { + mock[type] = mock[type] || mockFn() + return mock[type] + }) + + // @ts-ignore + return mock +} From ca3414f83f8a208fde6d38571c5aff0434765300 Mon Sep 17 00:00:00 2001 From: Ricardo Gobbo de Souza Date: Fri, 29 Jul 2022 10:50:41 -0300 Subject: [PATCH 02/12] chore: bump consola --- packages/test-utils/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/test-utils/package.json b/packages/test-utils/package.json index b16203c2766..f0ca1deafb1 100644 --- a/packages/test-utils/package.json +++ b/packages/test-utils/package.json @@ -15,7 +15,7 @@ "dependencies": { "@nuxt/kit": "^3.0.0-rc.6", "@nuxt/schema": "^3.0.0-rc.6", - "consola": "^2.15.2", + "consola": "^2.15.3", "defu": "^6.0.0", "execa": "^6.1.0", "get-port-please": "^2.5.0", From c1d3206861e57daaeb0353ef051b170a9ce3b29d Mon Sep 17 00:00:00 2001 From: Ricardo Gobbo de Souza Date: Fri, 29 Jul 2022 10:58:03 -0300 Subject: [PATCH 03/12] fix: mock async --- packages/test-utils/src/mocks.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/test-utils/src/mocks.ts b/packages/test-utils/src/mocks.ts index 8e1a940a578..37e78bdf86b 100644 --- a/packages/test-utils/src/mocks.ts +++ b/packages/test-utils/src/mocks.ts @@ -16,12 +16,12 @@ export const mockFnMaps = { vitest: mockFnVitest } -export function mockLogger (): typeof consola { +export async function mockLogger (): typeof consola { const mock = {} - const mockFn = mockFnMaps[useTestContext().options.runner] + const mockFn = await mockFnMaps[useTestContext().options.runner]() consola.mockTypes((type) => { - mock[type] = mock[type] || mockFn() + mock[type] = mock[type] || mockFn return mock[type] }) From 2daf2c0b5684b254e61f252a4afc8c7404a4df19 Mon Sep 17 00:00:00 2001 From: Ricardo Gobbo de Souza Date: Fri, 29 Jul 2022 11:09:05 -0300 Subject: [PATCH 04/12] feat: create `mockFn` --- packages/test-utils/src/mocks.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/test-utils/src/mocks.ts b/packages/test-utils/src/mocks.ts index 37e78bdf86b..9be4c2ba214 100644 --- a/packages/test-utils/src/mocks.ts +++ b/packages/test-utils/src/mocks.ts @@ -6,7 +6,7 @@ const mockFnVitest = async () => { return vitest.vi.fn() } -const mockFnJest = async () =>{ +const mockFnJest = async () => { const jest = await import('jest') return jest.fn() } @@ -16,12 +16,18 @@ export const mockFnMaps = { vitest: mockFnVitest } +export async function mockFn () { + const { runner } = useTestContext().options + + return await mockFnMaps[runner]() +} + export async function mockLogger (): typeof consola { const mock = {} - const mockFn = await mockFnMaps[useTestContext().options.runner]() + const fn = await mockFn() consola.mockTypes((type) => { - mock[type] = mock[type] || mockFn + mock[type] = mock[type] || fn return mock[type] }) From 9556caca3ac138f0dbb567287e69cbed799e418c Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 23 Aug 2022 23:07:01 +0200 Subject: [PATCH 05/12] merge lockfile --- yarn.lock | 1 + 1 file changed, 1 insertion(+) diff --git a/yarn.lock b/yarn.lock index 903638c2d04..a9f9d56eca4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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 From 69be05ba5ca6cdc57f7a078db205097c8ed61d7d Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 23 Aug 2022 23:11:00 +0200 Subject: [PATCH 06/12] simplify mockFn and support uknown runner fallback --- packages/test-utils/src/index.ts | 2 +- packages/test-utils/src/{mocks.ts => mock.ts} | 28 ++++++++----------- 2 files changed, 13 insertions(+), 17 deletions(-) rename packages/test-utils/src/{mocks.ts => mock.ts} (56%) diff --git a/packages/test-utils/src/index.ts b/packages/test-utils/src/index.ts index 7186739acde..3238c8d5203 100644 --- a/packages/test-utils/src/index.ts +++ b/packages/test-utils/src/index.ts @@ -1,6 +1,6 @@ export * from './browser' export * from './context' -export * from './mocks' +export * from './mock' export * from './nuxt' export * from './server' export * from './setup' diff --git a/packages/test-utils/src/mocks.ts b/packages/test-utils/src/mock.ts similarity index 56% rename from packages/test-utils/src/mocks.ts rename to packages/test-utils/src/mock.ts index 9be4c2ba214..bd575c52791 100644 --- a/packages/test-utils/src/mocks.ts +++ b/packages/test-utils/src/mock.ts @@ -1,25 +1,21 @@ import consola from 'consola' import { useTestContext } from './context' -const mockFnVitest = async () => { - const vitest = await import('vitest') - return vitest.vi.fn() -} - -const mockFnJest = async () => { - const jest = await import('jest') - return jest.fn() -} - -export const mockFnMaps = { - jest: mockFnJest, - vitest: mockFnVitest -} - export async function mockFn () { const { runner } = useTestContext().options - return await mockFnMaps[runner]() + 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 () => {} } export async function mockLogger (): typeof consola { From 59c85a9f81818a77565017127d6d32d85b481471 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 23 Aug 2022 23:15:53 +0200 Subject: [PATCH 07/12] fix types --- packages/test-utils/src/mock.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/test-utils/src/mock.ts b/packages/test-utils/src/mock.ts index bd575c52791..1af0f42bb0f 100644 --- a/packages/test-utils/src/mock.ts +++ b/packages/test-utils/src/mock.ts @@ -1,4 +1,5 @@ import consola from 'consola' +import type { Consola } from 'consola' import { useTestContext } from './context' export async function mockFn () { @@ -18,15 +19,15 @@ export async function mockFn () { return () => {} } -export async function mockLogger (): typeof consola { - const mock = {} +export async function mockLogger (): Promise { + const mockedConsole: any = {} + const fn = await mockFn() consola.mockTypes((type) => { - mock[type] = mock[type] || fn - return mock[type] + mockedConsole[type] = mockedConsole[type] || fn + return mockedConsole[type] }) - // @ts-ignore - return mock + return mockedConsole } From 6221f72f06fc1ddc72673a1ba12d0a600b0a9ad5 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Mon, 5 Sep 2022 10:12:12 +0200 Subject: [PATCH 08/12] set mock to context --- packages/test-utils/src/context.ts | 4 ++- packages/test-utils/src/mock.ts | 33 ++++++------------------- packages/test-utils/src/setup/jest.ts | 7 +++++- packages/test-utils/src/setup/vitest.ts | 3 +++ packages/test-utils/src/types.ts | 4 +++ 5 files changed, 24 insertions(+), 27 deletions(-) diff --git a/packages/test-utils/src/context.ts b/packages/test-utils/src/context.ts index 3a09ecab5f1..ad987d1ec97 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/mock.ts b/packages/test-utils/src/mock.ts index 1af0f42bb0f..52843da05a8 100644 --- a/packages/test-utils/src/mock.ts +++ b/packages/test-utils/src/mock.ts @@ -1,33 +1,16 @@ 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 () => {} +export function mockFn () { + const ctx = useTestContext() + return ctx.mockFn } -export async function mockLogger (): Promise { - const mockedConsole: any = {} - - const fn = await mockFn() - +export function mockLogger (): Record { + const mocks: any = {} consola.mockTypes((type) => { - mockedConsole[type] = mockedConsole[type] || fn - return mockedConsole[type] + mocks[type] = mockFn() + return mocks[type] }) - - return mockedConsole + return mocks } diff --git a/packages/test-utils/src/setup/jest.ts b/packages/test-utils/src/setup/jest.ts index 67bec30a58b..01a9f654386 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 60a3b340ffb..5fa16f6d9ed 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 8e41fa4a9f2..62216f24769 100644 --- a/packages/test-utils/src/types.ts +++ b/packages/test-utils/src/types.ts @@ -23,6 +23,9 @@ export interface TestOptions { launch?: LaunchOptions } server: boolean + mock: { + fn: Function + } } export interface TestContext { @@ -31,6 +34,7 @@ export interface TestContext { browser?: Browser url?: string serverProcess?: ExecaChildProcess + mockFn?: Function } export interface TestHooks { From 886eccb8a1a669e4a3bdb51444e6e399a1b4ecc4 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Mon, 5 Sep 2022 10:12:49 +0200 Subject: [PATCH 09/12] revert extra change --- packages/test-utils/src/types.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/test-utils/src/types.ts b/packages/test-utils/src/types.ts index 62216f24769..ab8612ff216 100644 --- a/packages/test-utils/src/types.ts +++ b/packages/test-utils/src/types.ts @@ -23,9 +23,6 @@ export interface TestOptions { launch?: LaunchOptions } server: boolean - mock: { - fn: Function - } } export interface TestContext { From 8fd505445393545ed5c637b36ee3451951ca9f2e Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Mon, 5 Sep 2022 10:16:00 +0200 Subject: [PATCH 10/12] add berief docs --- docs/content/2.guide/6.going-further/7.testing.md | 5 +++++ 1 file changed, 5 insertions(+) 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 0f4406a7b29..c88f94dd032 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.mocklogs](https://github.com/unjs/consola#mocktypes) and `mockFn()`. Returns an object of mocked logger types. + ## Testing in a Browser ::alert{icon=🚧} From 785166e9244fe7a44be6826e490ed8c5bcfe9935 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Mon, 5 Sep 2022 10:18:01 +0200 Subject: [PATCH 11/12] add implicit dep --- packages/test-utils/build.config.ts | 3 +-- packages/test-utils/package.json | 1 + yarn.lock | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/test-utils/build.config.ts b/packages/test-utils/build.config.ts index 038925e9197..586a5fe31d7 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 71e8b52211c..49e8f472688 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/yarn.lock b/yarn.lock index f23a6f76582..720234deb97 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 From a72b345722702b35e40c7220a3a2b099c86b44fc Mon Sep 17 00:00:00 2001 From: pooya parsa Date: Mon, 5 Sep 2022 10:18:53 +0200 Subject: [PATCH 12/12] Update docs/content/2.guide/6.going-further/7.testing.md --- docs/content/2.guide/6.going-further/7.testing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 c88f94dd032..4fa4fb14cc0 100644 --- a/docs/content/2.guide/6.going-further/7.testing.md +++ b/docs/content/2.guide/6.going-further/7.testing.md @@ -187,7 +187,7 @@ For more usage, please refer to our [tests for Nuxt 3 framework](https://github. ## Mock utils - `mockFn()`: Returns a mocked function based on test runner. -- `mockLogger()`: Mocks logger using [consola.mocklogs](https://github.com/unjs/consola#mocktypes) and `mockFn()`. Returns an object of mocked logger types. +- `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