@nuxt/test-utils
Version:
Test utilities for Nuxt
207 lines • 8.41 kB
text/typescript
import { exec } from "tinyexec";
import { $Fetch } from "ofetch";
import { LogType } from "consola";
import { Nuxt, NuxtConfig } from "@nuxt/schema";
import { Browser, BrowserContextOptions, LaunchOptions, Page, Response as Response$1 } from "playwright-core";
//#region src/e2e/browser.d.ts
declare function createBrowser(): Promise<void>;
declare function getBrowser(): Promise<Browser>;
type _GotoOptions = NonNullable<Parameters<Page['goto']>[1]>;
interface GotoOptions extends Omit<_GotoOptions, 'waitUntil'> {
waitUntil?: 'hydration' | 'route' | _GotoOptions['waitUntil'];
}
interface NuxtPage extends Omit<Page, 'goto'> {
goto: (url: string, options?: GotoOptions) => Promise<Response$1 | null>;
}
declare function createPage(path?: string, options?: BrowserContextOptions): Promise<NuxtPage>;
declare function waitForHydration(page: Page, url: string, waitUntil?: GotoOptions['waitUntil']): Promise<void>;
//#endregion
//#region src/e2e/server.d.ts
interface StartServerOptions {
env?: Record<string, unknown>;
/**
* Overrides the consola log level for the server subprocess.
* Defaults to `TestOptions.logLevel` (which itself defaults to `1`).
*/
logLevel?: number;
}
declare function startServer(options?: StartServerOptions): Promise<void>;
declare function stopServer(): Promise<void>;
/**
* Returns the lines captured from the server subprocess's stdout/stderr since
* the last `startServer()` call (or `clearServerLogs()`).
* Only populated when `captureServerLogs` is `true` (the default).
*/
declare function getServerLogs(): string[];
/**
* Clears the captured server log lines. Useful between requests when you want
* to assert only on the logs produced by a specific operation.
*/
declare function clearServerLogs(): void;
declare function fetch$1(path: string, options?: RequestInit): Promise<Response>;
declare const $fetch: "$fetch" extends keyof typeof globalThis ? typeof globalThis.$fetch : $Fetch;
declare function url(path: string): string;
//#endregion
//#region src/e2e/types.d.ts
type TestRunner = 'vitest' | 'jest' | 'cucumber' | 'bun';
interface TestOptions {
testDir: string;
fixture: string;
/**
* Name of the configuration file.
* @default 'nuxt.config'
*/
configFile: string;
/**
* Path to a directory with a Nuxt app to be put under test.
* @default '.'
*/
rootDir: string;
buildDir: string;
nuxtConfig: NuxtConfig;
/**
* Whether to run a separate build step.
* @default true // (`false` if `browser` or `server` is disabled, or if a `host` is provided)
*/
build: boolean;
dev: boolean;
/**
* The amount of time (in milliseconds) to allow for `setupTest` to complete its work (which could include building or generating files for a Nuxt application, depending on the options that are passed).
* @default 120000 // or `240000` on windows
*/
setupTimeout: number;
/**
* The amount of time (in milliseconds) to allow tearing down the test environment, such as closing the browser.
* @default 30000
*/
teardownTimeout: number;
/**
* The amount of time (in milliseconds) to wait for the dev or built server to become ready (i.e. respond successfully on the configured base URL) before failing.
*
* This is bounded by `setupTimeout`, so increasing this is only useful in combination with a sufficiently large `setupTimeout`.
* @default 120000 // on windows; otherwise 60000
*/
serverStartTimeout: number;
waitFor: number;
/**
* Under the hood, Nuxt test utils uses [`playwright`](https://playwright.dev) to carry out browser testing. If this option is set, a browser will be launched and can be controlled in the subsequent test suite.
* @default false
*/
browser: boolean;
/**
* Specify the runner for the test suite. One of `'vitest' | 'jest' | 'cucumber' | 'bun'`.
* @default 'vitest'
*/
runner: TestRunner;
logLevel: number;
browserOptions: {
/** The type of browser to launch - either `chromium`, `firefox` or `webkit` */
type: 'chromium' | 'firefox' | 'webkit';
/** `object` of options that will be passed to playwright when launching the browser. See [full API reference](https://playwright.dev/docs/api/class-browsertype#browser-type-launch). */
launch?: LaunchOptions;
};
/**
* Whether to launch a server to respond to requests in the test suite.
* @default true // (`false` if a `host` is provided)
*/
server: boolean;
/**
* If provided, a URL to use as the test target instead of building and running a new server. Useful for running "real" end-to-end tests against a deployed version of your application, or against an already running local server.
* @default undefined
*/
host?: string;
/**
* If provided, set the launched test server port to the value.
* @default undefined
*/
port?: number;
env?: StartServerOptions['env'];
/**
* Whether to capture server process output instead of inheriting stdio.
* When `true` (default), server stdout/stderr is suppressed from the console
* and accessible via `getServerLogs()`. Set to `false` to restore the old
* inherit-stdio behaviour (useful when debugging a test locally).
* @default true
*/
captureServerLogs?: boolean;
}
interface TestContext {
options: TestOptions;
nuxt?: Nuxt;
browser?: Browser;
url?: string;
serverProcess?: ReturnType<typeof exec>;
mockFn?: (...args: unknown[]) => unknown;
/**
* Lines emitted to the server subprocess's stdout/stderr, in order.
* Only populated when `options.captureServerLogs` is `true`.
*/
serverLogs: string[];
/**
* Functions to run on the vitest `afterAll` hook.
* Useful for removing anything created during the test.
*/
teardown?: (() => void)[];
}
interface TestHooks {
beforeEach: () => void;
afterEach: () => void;
afterAll: () => Promise<void>;
beforeAll: () => Promise<void>;
/**
* @deprecated use `beforeAll` instead
*/
setup: () => Promise<void>;
ctx: TestContext;
}
//#endregion
//#region src/e2e/context.d.ts
declare function createTestContext(options: Partial<TestOptions>): TestContext;
declare function useTestContext(): TestContext;
declare function setTestContext(context: TestContext): TestContext;
declare function setTestContext(context?: TestContext): TestContext | undefined;
declare function isDev(): boolean;
declare function recoverContextFromEnv(): void;
declare function exposeContextToEnv(): void;
//#endregion
//#region src/e2e/mock.d.ts
declare function mockFn(): ((...args: unknown[]) => unknown) | undefined;
declare function mockLogger(): Record<LogType, (...args: unknown[]) => void>;
//#endregion
//#region src/e2e/nuxt.d.ts
declare function loadFixture(): Promise<void>;
declare function buildFixture(): Promise<void>;
//#endregion
//#region src/e2e/setup/bun.d.ts
declare function setupBun(hooks: TestHooks): Promise<void>;
//#endregion
//#region src/e2e/setup/cucumber.d.ts
declare function setupCucumber(hooks: TestHooks): Promise<void>;
//#endregion
//#region src/e2e/setup/jest.d.ts
declare function setupJest(hooks: TestHooks): Promise<void>;
//#endregion
//#region src/e2e/setup/vitest.d.ts
declare function setupVitest(hooks: TestHooks): Promise<void>;
//#endregion
//#region src/e2e/setup/index.d.ts
declare const setupMaps: {
bun: typeof setupBun;
cucumber: typeof setupCucumber;
jest: typeof setupJest;
vitest: typeof setupVitest;
};
declare function createTest(options: Partial<TestOptions>): TestHooks;
declare function setup(options?: Partial<TestOptions>): Promise<void>;
//#endregion
//#region src/e2e/run.d.ts
interface RunTestOptions {
rootDir: string;
dev?: boolean;
watch?: boolean;
runner?: 'vitest';
globalSetup?: boolean;
}
declare function runTests(opts: RunTestOptions): Promise<void>;
//#endregion
export { createBrowser as A, fetch$1 as C, url as D, stopServer as E, getBrowser as M, waitForHydration as N, GotoOptions as O, clearServerLogs as S, startServer as T, TestHooks as _, setupMaps as a, $fetch as b, mockFn as c, exposeContextToEnv as d, isDev as f, TestContext as g, useTestContext as h, setup as i, createPage as j, NuxtPage as k, mockLogger as l, setTestContext as m, runTests as n, buildFixture as o, recoverContextFromEnv as p, createTest as r, loadFixture as s, RunTestOptions as t, createTestContext as u, TestOptions as v, getServerLogs as w, StartServerOptions as x, TestRunner as y };