UNPKG

@nuxt/test-utils

Version:
121 lines (116 loc) 4.79 kB
import { Browser, Page, Response as Response$1, BrowserContextOptions, LaunchOptions } from 'playwright-core'; import { NuxtConfig, Nuxt } from '@nuxt/schema'; import { exec } from 'tinyexec'; import { $Fetch } from 'ofetch'; 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>; interface StartServerOptions { env?: Record<string, unknown>; } declare function startServer(options?: StartServerOptions): Promise<void>; declare function stopServer(): Promise<void>; declare function fetch(path: string, options?: RequestInit): Promise<Response>; declare const $fetch: "$fetch" extends keyof typeof globalThis ? typeof globalThis.$fetch : $Fetch; declare function url(path: string): string; 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; 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']; } interface TestContext { options: TestOptions; nuxt?: Nuxt; browser?: Browser; url?: string; serverProcess?: ReturnType<typeof exec>; mockFn?: (...args: unknown[]) => unknown; /** * 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; } export { $fetch as $, createBrowser as c, createPage as d, stopServer as e, fetch as f, getBrowser as g, startServer as s, url as u, waitForHydration as w }; export type { GotoOptions as G, NuxtPage as N, StartServerOptions as S, TestOptions as T, TestHooks as a, TestContext as b, TestRunner as h };