nitro-test-utils
Version:
Testing environment and utilities for Nitro
53 lines (50 loc) • 1.97 kB
text/typescript
import * as ofetch from 'ofetch';
import { ResponseType, FetchOptions, FetchResponse, MappedResponseType } from 'ofetch';
import { T as TestOptions } from './shared/nitro-test-utils.JwyvVsQ9.mjs';
import 'listhen';
import 'nitropack';
interface TestFetchResponse<T> extends FetchResponse<T> {
/** Alias for `response._data` */
data?: T;
}
declare module 'vitest' {
interface ProvidedContext {
server?: {
url: string;
};
}
}
/**
* Creates a custom `ofetch` instance with the Nitro server URL as the base URL.
*
* @remarks
* The following additional fetch options have been set as defaults:
* - `ignoreResponseError: true` to prevent throwing errors on non-2xx responses.
* - `redirect: 'manual'` to prevent automatic redirects.
* - `headers: { accept: 'application/json' }` to force a JSON error response when Nitro returns an error.
*/
declare function createFetch(): ofetch.$Fetch;
/**
* Fetches the raw response from the Nitro server for the given path. `FetchOptions` can be passed to customize the request.
*
* @remarks
* The following additional fetch options have been set as defaults:
* - `ignoreResponseError: true` to prevent throwing errors on non-2xx responses.
* - `redirect: 'manual'` to prevent automatic redirects.
* - `headers: { accept: 'application/json' }` to force a JSON error response when Nitro returns an error.
*/
declare function $fetchRaw<T = any, R extends ResponseType = 'json'>(path: string, options?: FetchOptions<R>): Promise<TestFetchResponse<MappedResponseType<R, T>>>;
declare function injectServerUrl(): string;
/**
* Setup options for the Nitro test context.
*
* @example
* import { setup } from 'nitro-test-utils'
*
* await setup({
* rootDir: fileURLToPath(new URL('fixture', import.meta.url)),
* })
*/
declare function setup(options?: TestOptions): Promise<void>;
export { $fetchRaw, createFetch, injectServerUrl, setup };
export type { TestFetchResponse };