Skip to content

Commit

Permalink
[pre] Include cloudflare:test types in package
Browse files Browse the repository at this point in the history
These can be referenced by adding `@cloudflare/vitest-pool-workers` to
the `types` array in `tsconfig.json`.
  • Loading branch information
mrbbot committed Jan 31, 2024
1 parent d774698 commit 27bfdb5
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 144 deletions.
10 changes: 8 additions & 2 deletions packages/vitest-pool-workers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@
"version": "0.0.1",
"private": true,
"main": "dist/pool/index.mjs",
"types": "test/cloudflare-test.d.ts",
"exports": {
".": "./dist/pool/index.mjs",
".": {
"import": "./dist/pool/index.mjs",
"types": "./test/cloudflare-test.d.ts"
},
"./config": {
"import": "./dist/config/index.cjs",
"require": "./dist/config/index.cjs",
"types": "./dist/config/index.d.ts"
}
},
"files": [
"dist"
"dist",
"test/cloudflare-test.d.ts"
],
"scripts": {
"build": "node scripts/bundle.mjs && tsc -p tsconfig.emit.json",
Expand Down
1 change: 0 additions & 1 deletion packages/vitest-pool-workers/src/worker/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@
}
},
"include": ["./**/*.ts"],
// "include": ["./**/*.ts", "../../../miniflare/src/workers/node.d.ts"],
"exclude": []
}
285 changes: 144 additions & 141 deletions packages/vitest-pool-workers/test/cloudflare-test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,160 +29,163 @@ declare module "cloudflare:test" {
messages: ServiceBindingQueueMessage[]
): MessageBatch;
export function getQueueResult(
batch: QueueController,
batch: MessageBatch,
ctx: ExecutionContext
): Promise<FetcherQueueResult>;
}

// Taken from `undici` (https://github.com/nodejs/undici/tree/main/types) with
// no dependency on `@types/node` and with unusable functions removed
//
// MIT License
//
// Copyright (c) Matteo Collina and Undici contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

type IncomingHttpHeaders = Record<string, string | string[] | undefined>;
// Taken from `undici` (https://github.com/nodejs/undici/tree/main/types) with
// no dependency on `@types/node` and with unusable functions removed
//
// MIT License
//
// Copyright (c) Matteo Collina and Undici contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

/** The scope associated with a mock dispatch. */
declare abstract class MockScope<TData extends object = object> {
/** Delay a reply by a set amount of time in ms. */
delay(waitInMs: number): MockScope<TData>;
/** Persist the defined mock data for the associated reply. It will return the defined mock data indefinitely. */
persist(): MockScope<TData>;
/** Define a reply for a set amount of matching requests. */
times(repeatTimes: number): MockScope<TData>;
}
type IncomingHttpHeaders = Record<string, string | string[] | undefined>;
type Buffer = Uint8Array;

/** The interceptor for a Mock. */
declare abstract class MockInterceptor {
/** Mock an undici request with the defined reply. */
reply<TData extends object = object>(
replyOptionsCallback: MockInterceptor.MockReplyOptionsCallback<TData>
): MockScope<TData>;
reply<TData extends object = object>(
statusCode: number,
data?:
| TData
| Buffer
| string
| MockInterceptor.MockResponseDataHandler<TData>,
responseOptions?: MockInterceptor.MockResponseOptions
): MockScope<TData>;
/** Mock an undici request by throwing the defined reply error. */
replyWithError<TError extends Error = Error>(error: TError): MockScope;
/** Set default reply headers on the interceptor for subsequent mocked replies. */
defaultReplyHeaders(headers: IncomingHttpHeaders): MockInterceptor;
/** Set default reply trailers on the interceptor for subsequent mocked replies. */
defaultReplyTrailers(trailers: Record<string, string>): MockInterceptor;
/** Set automatically calculated content-length header on subsequent mocked replies. */
replyContentLength(): MockInterceptor;
}
declare namespace MockInterceptor {
/** MockInterceptor options. */
export interface Options {
/** Path to intercept on. */
path: string | RegExp | ((path: string) => boolean);
/** Method to intercept on. Defaults to GET. */
method?: string | RegExp | ((method: string) => boolean);
/** Body to intercept on. */
body?: string | RegExp | ((body: string) => boolean);
/** Headers to intercept on. */
headers?: // eslint-disable-next-line unused-imports/no-unused-vars
| Record<string, string | RegExp | ((body: string) => boolean)>
| ((headers: Record<string, string>) => boolean);
/** Query params to intercept on */
query?: Record<string, unknown>;
/** The scope associated with a mock dispatch. */
abstract class MockScope<TData extends object = object> {
/** Delay a reply by a set amount of time in ms. */
delay(waitInMs: number): MockScope<TData>;
/** Persist the defined mock data for the associated reply. It will return the defined mock data indefinitely. */
persist(): MockScope<TData>;
/** Define a reply for a set amount of matching requests. */
times(repeatTimes: number): MockScope<TData>;
}
export interface MockDispatch<
TData extends object = object,
TError extends Error = Error
> extends Options {
times: number | null;
persist: boolean;
consumed: boolean;
data: MockDispatchData<TData, TError>;

/** The interceptor for a Mock. */
abstract class MockInterceptor {
/** Mock an undici request with the defined reply. */
reply<TData extends object = object>(
replyOptionsCallback: MockInterceptor.MockReplyOptionsCallback<TData>
): MockScope<TData>;
reply<TData extends object = object>(
statusCode: number,
data?:
| TData
| Buffer
| string
| MockInterceptor.MockResponseDataHandler<TData>,
responseOptions?: MockInterceptor.MockResponseOptions
): MockScope<TData>;
/** Mock an undici request by throwing the defined reply error. */
replyWithError<TError extends Error = Error>(error: TError): MockScope;
/** Set default reply headers on the interceptor for subsequent mocked replies. */
defaultReplyHeaders(headers: IncomingHttpHeaders): MockInterceptor;
/** Set default reply trailers on the interceptor for subsequent mocked replies. */
defaultReplyTrailers(trailers: Record<string, string>): MockInterceptor;
/** Set automatically calculated content-length header on subsequent mocked replies. */
replyContentLength(): MockInterceptor;
}
export interface MockDispatchData<
TData extends object = object,
TError extends Error = Error
> extends MockResponseOptions {
error: TError | null;
statusCode?: number;
data?: TData | string;
namespace MockInterceptor {
/** MockInterceptor options. */
export interface Options {
/** Path to intercept on. */
path: string | RegExp | ((path: string) => boolean);
/** Method to intercept on. Defaults to GET. */
method?: string | RegExp | ((method: string) => boolean);
/** Body to intercept on. */
body?: string | RegExp | ((body: string) => boolean);
/** Headers to intercept on. */
headers?: // eslint-disable-next-line unused-imports/no-unused-vars
| Record<string, string | RegExp | ((body: string) => boolean)>
| ((headers: Record<string, string>) => boolean);
/** Query params to intercept on */
query?: Record<string, unknown>;
}
export interface MockDispatch<
TData extends object = object,
TError extends Error = Error
> extends Options {
times: number | null;
persist: boolean;
consumed: boolean;
data: MockDispatchData<TData, TError>;
}
export interface MockDispatchData<
TData extends object = object,
TError extends Error = Error
> extends MockResponseOptions {
error: TError | null;
statusCode?: number;
data?: TData | string;
}
export interface MockResponseOptions {
headers?: IncomingHttpHeaders;
trailers?: Record<string, string>;
}
export interface MockResponseCallbackOptions {
path: string;
origin: string;
method: string;
body?: BodyInit;
headers: Headers | Record<string, string>;
maxRedirections: number;
}
export type MockResponseDataHandler<TData extends object = object> = (
opts: MockResponseCallbackOptions
) => TData | Buffer | string;
export type MockReplyOptionsCallback<TData extends object = object> = (
opts: MockResponseCallbackOptions
) => {
statusCode: number;
data?: TData | Buffer | string;
responseOptions?: MockResponseOptions;
};
}
export interface MockResponseOptions {
headers?: IncomingHttpHeaders;
trailers?: Record<string, string>;

interface Interceptable {
/** Intercepts any matching requests that use the same origin as this mock client. */
intercept(options: MockInterceptor.Options): MockInterceptor;
}
export interface MockResponseCallbackOptions {
path: string;

interface PendingInterceptor extends MockInterceptor.MockDispatch {
origin: string;
method: string;
body?: BodyInit;
headers: Headers | Record<string, string>;
maxRedirections: number;
}
export type MockResponseDataHandler<TData extends object = object> = (
opts: MockResponseCallbackOptions
) => TData | Buffer | string;
export type MockReplyOptionsCallback<TData extends object = object> = (
opts: MockResponseCallbackOptions
) => {
statusCode: number;
data?: TData | Buffer | string;
responseOptions?: MockResponseOptions;
};
}

interface Interceptable {
/** Intercepts any matching requests that use the same origin as this mock client. */
intercept(options: MockInterceptor.Options): MockInterceptor;
}

interface PendingInterceptor extends MockInterceptor.MockDispatch {
origin: string;
}
interface PendingInterceptorsFormatter {
format(pendingInterceptors: readonly PendingInterceptor[]): string;
}
interface PendingInterceptorsFormatter {
format(pendingInterceptors: readonly PendingInterceptor[]): string;
}

/** A mocked Agent class that implements the Agent API. It allows one to intercept HTTP requests made through undici and return mocked responses instead. */
declare abstract class MockAgent {
/** Creates and retrieves mock Dispatcher instances which can then be used to intercept HTTP requests. If the number of connections on the mock agent is set to 1, a MockClient instance is returned. Otherwise a MockPool instance is returned. */
// eslint-disable-next-line no-shadow
get(origin: string | RegExp | ((origin: string) => boolean)): Interceptable;
/** A mocked Agent class that implements the Agent API. It allows one to intercept HTTP requests made through undici and return mocked responses instead. */
abstract class MockAgent {
/** Creates and retrieves mock Dispatcher instances which can then be used to intercept HTTP requests. If the number of connections on the mock agent is set to 1, a MockClient instance is returned. Otherwise a MockPool instance is returned. */
// eslint-disable-next-line no-shadow
get(origin: string | RegExp | ((origin: string) => boolean)): Interceptable;

/** Disables mocking in MockAgent. */
deactivate(): void;
/** Enables mocking in a MockAgent instance. When instantiated, a MockAgent is automatically activated. Therefore, this method is only effective after MockAgent.deactivate has been called. */
activate(): void;
/** Disables mocking in MockAgent. */
deactivate(): void;
/** Enables mocking in a MockAgent instance. When instantiated, a MockAgent is automatically activated. Therefore, this method is only effective after MockAgent.deactivate has been called. */
activate(): void;

/** Define host matchers so only matching requests that aren't intercepted by the mock dispatchers will be attempted. */
// eslint-disable-next-line no-shadow
enableNetConnect(host?: string | RegExp | ((host: string) => boolean)): void;
/** Causes all requests to throw when requests are not matched in a MockAgent intercept. */
disableNetConnect(): void;
/** Define host matchers so only matching requests that aren't intercepted by the mock dispatchers will be attempted. */
enableNetConnect(
// eslint-disable-next-line no-shadow
host?: string | RegExp | ((host: string) => boolean)
): void;
/** Causes all requests to throw when requests are not matched in a MockAgent intercept. */
disableNetConnect(): void;

pendingInterceptors(): PendingInterceptor[];
assertNoPendingInterceptors(options?: {
pendingInterceptorsFormatter?: PendingInterceptorsFormatter;
}): void;
pendingInterceptors(): PendingInterceptor[];
assertNoPendingInterceptors(options?: {
pendingInterceptorsFormatter?: PendingInterceptorsFormatter;
}): void;
}
}

0 comments on commit 27bfdb5

Please sign in to comment.