vite-test-utils-edge
Version:
Test utils for Vite application
202 lines (194 loc) • 6.54 kB
TypeScript
import * as playwright from 'playwright';
import { BrowserContextOptions, Browser, LaunchOptions } from 'playwright';
import { FetchOptions, $fetch as $fetch$1 } from 'ofetch';
import { ChildProcess } from 'node:child_process';
import { UserConfig } from 'vite';
/**
*
* @param {string} [path] - The path to the page, optional
* @param {BrowserContextOptions} [options] - The browser context options, optional
* @returns {Page} A page instance
*/
declare function createPage(path?: string, options?: BrowserContextOptions): Promise<playwright.Page>;
/**
* server module, forked from the below:
* - original repository url: https://github.com/nuxt/framework
* - npm package name: `@nuxt/test-utils`
* - code url: https://github.com/nuxt/framework/blob/main/packages/test-utils/src/server.ts
* - author: Nuxt Framework Team
* - license: MIT
*/
/**
* Start the vite server
*
* If `mode` option is `dev`, vite-test-utils will start vite dev server instance.
*
* Else `mode` option is `preview`, vite-test-tuils will build your vite test fixture, and start vite preview server instance.
*
* Vite dev server and vite preview server is started **with child process**.
*/
declare function startServer(): Promise<void>;
/**
* Stop the vite server
*
* vite-test-utils will stop vite server instan with {@link startServer}.
*/
declare function stopServer(): Promise<void>;
/**
* The url concatenating function
*
* @param {string} path - The path that is concatenated to the url
* @returns {string} The url with concating the path
*/
declare function url(path: string): string;
/**
* Low level fetch API
*
* @remarks
* This function is delegated with {@link ofetch https://github.com/unjs/ofetch}
*
* @param {string} path - The path of fetch request. you can specify a path starting from root (e.g. `/foo`)
* @param {any} [options] - The options of fetch request, optional
* @returns {Response} The response of fetch request
*/
declare function fetch(path: string, options?: any): Promise<Response>;
type _ResponseType = 'blob' | 'text' | 'arrayBuffer' | 'json';
/**
* Hight level fetch API
*
* @remarks
* This function is delegated with {@link ofetch https://github.com/unjs/ofetch}
*
* @param {string} path - The path of fetch request
* @param {FetchOptions} [options] - The options of fetch request, optional
*/
declare function $fetch<T = any, R extends _ResponseType = 'json'>(path: string, options?: FetchOptions<R>): ReturnType<typeof $fetch$1<T, R>>;
/**
* The Test Context that is used in test utils
*/
interface TestContext {
/**
* The options that is specified `setup` function
*/
options: TestOptions;
/**
* The browser instance that is created by playwright
*/
browser?: Browser;
/**
* The Vite server instance
*/
server?: ChildProcess;
/**
* The vite config inline filepath that is configured by `{@link TestOptions.viteConfig}` option
*/
viteConfigInline?: string;
/**
* The output directory
*/
buildDir?: string;
/**
* The port that is opened in server
*/
port?: number;
/**
* The url of the server
*/
url?: string;
}
/**
* The Test Options that is used in `setup`
*/
interface TestOptions {
/**
* The root directory path that is put in your vite application project
*
* If vite-test-utils could not find vite config in this option, it falls back to `process.cwd()`
*
* @default `process.cwd()`
*/
rootDir?: string;
/**
* The vite config **filename** which is used in test fixture.
*
* If vite config file is specified with this option, it will be respected over the default config file that will be resolved by vite.
*
* The file for this option is **relative** to the directory specified in the `rootDir` option.
*
* @default `'vite.config.ts' | 'vite.config.js' | 'vite.config.mjs' | 'vite.config.mts' | 'vite.config.cjs' | 'vite.config.cts'`
*/
configFile?: string;
/**
* The vite config that is overrided the config resolved by utils.
*
* Simply, use this option if you hope override the vite config with **javascript premitive value** such as object or string.
*
* If you hope set up programmaticaly overrides using like `import` syntax, you must prepare the vite config for the override and specify it with `viteConfigPath`.
*
* @default `{}`
*/
viteConfig?: UserConfig;
/**
* The vite config file path that is overrided the config resolved by utils.
*
* If this option is specified, it's respected than `viteConfig` option.
*
* @default `undefined`
*/
viteConfigFile?: string;
/**
* The vite server working mode
*
* If you use `'dev'`, vite-test-utils will start dev server, else you use `'preview'` vite-test-utils will build fixture and start preview server.
*
* @default 'dev'
*/
mode?: 'dev' | 'preview';
/**
* Whether to start the server while running `setup`
*
* @default false
*/
server?: boolean;
/**
* Whether to create the playwright `Browser` instance while running `setup`
*
* @default false
*/
browser?: boolean;
/**
* The playwright browser options.
*/
browserOptions?: {
/**
* The browser type.
*
* @default 'chromium'
*/
type?: 'chromium' | 'firefox' | 'webkit';
/**
* The browser launch options.
*/
launch?: LaunchOptions;
};
}
/**
* setup module, forked from the below:
* - original repository url: https://github.com/nuxt/framework
* - npm package name: `@nuxt/test-utils`
* - code url: https://github.com/nuxt/framework/blob/main/packages/test-utils/src/setup/index.ts
* - author: Nuxt Framework Team
* - license: MIT
*/
/**
* The test setup function
*
* @remarks
* This function will be hooked with using setup/teardown of vitest.
* In setup hook, start the vite dev server, create a browser instance of playwright, and load the vite app fixture.
* In the teardown hook, terminate the vite dev server and close the browser instance of playwright.
*
* @param {TestOptions} [options] - Optional, test Options that is used in `setup`, about details @see TestOptions
*/
declare function setup(options?: TestOptions): Promise<void>;
export { $fetch, TestContext, TestOptions, createPage, fetch, setup, startServer, stopServer, url };