From 27bfdb5ba395fd04a0551767cc3150843cdea383 Mon Sep 17 00:00:00 2001 From: bcoll Date: Wed, 31 Jan 2024 14:32:12 +0000 Subject: [PATCH] [pre] Include `cloudflare:test` types in package These can be referenced by adding `@cloudflare/vitest-pool-workers` to the `types` array in `tsconfig.json`. --- packages/vitest-pool-workers/package.json | 10 +- .../src/worker/tsconfig.json | 1 - .../test/cloudflare-test.d.ts | 285 +++++++++--------- 3 files changed, 152 insertions(+), 144 deletions(-) diff --git a/packages/vitest-pool-workers/package.json b/packages/vitest-pool-workers/package.json index e30e4813b940..88b967b45d75 100644 --- a/packages/vitest-pool-workers/package.json +++ b/packages/vitest-pool-workers/package.json @@ -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", diff --git a/packages/vitest-pool-workers/src/worker/tsconfig.json b/packages/vitest-pool-workers/src/worker/tsconfig.json index a1ef6f7e154f..34a43941739d 100644 --- a/packages/vitest-pool-workers/src/worker/tsconfig.json +++ b/packages/vitest-pool-workers/src/worker/tsconfig.json @@ -8,6 +8,5 @@ } }, "include": ["./**/*.ts"], - // "include": ["./**/*.ts", "../../../miniflare/src/workers/node.d.ts"], "exclude": [] } diff --git a/packages/vitest-pool-workers/test/cloudflare-test.d.ts b/packages/vitest-pool-workers/test/cloudflare-test.d.ts index 85a62a93819e..d63e6d28cf00 100644 --- a/packages/vitest-pool-workers/test/cloudflare-test.d.ts +++ b/packages/vitest-pool-workers/test/cloudflare-test.d.ts @@ -29,160 +29,163 @@ declare module "cloudflare:test" { messages: ServiceBindingQueueMessage[] ): MessageBatch; export function getQueueResult( - batch: QueueController, + batch: MessageBatch, ctx: ExecutionContext ): Promise; -} - -// 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; + // 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 { - /** Delay a reply by a set amount of time in ms. */ - delay(waitInMs: number): MockScope; - /** Persist the defined mock data for the associated reply. It will return the defined mock data indefinitely. */ - persist(): MockScope; - /** Define a reply for a set amount of matching requests. */ - times(repeatTimes: number): MockScope; -} + type IncomingHttpHeaders = Record; + type Buffer = Uint8Array; -/** The interceptor for a Mock. */ -declare abstract class MockInterceptor { - /** Mock an undici request with the defined reply. */ - reply( - replyOptionsCallback: MockInterceptor.MockReplyOptionsCallback - ): MockScope; - reply( - statusCode: number, - data?: - | TData - | Buffer - | string - | MockInterceptor.MockResponseDataHandler, - responseOptions?: MockInterceptor.MockResponseOptions - ): MockScope; - /** Mock an undici request by throwing the defined reply error. */ - replyWithError(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): 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 boolean)> - | ((headers: Record) => boolean); - /** Query params to intercept on */ - query?: Record; + /** The scope associated with a mock dispatch. */ + abstract class MockScope { + /** Delay a reply by a set amount of time in ms. */ + delay(waitInMs: number): MockScope; + /** Persist the defined mock data for the associated reply. It will return the defined mock data indefinitely. */ + persist(): MockScope; + /** Define a reply for a set amount of matching requests. */ + times(repeatTimes: number): MockScope; } - export interface MockDispatch< - TData extends object = object, - TError extends Error = Error - > extends Options { - times: number | null; - persist: boolean; - consumed: boolean; - data: MockDispatchData; + + /** The interceptor for a Mock. */ + abstract class MockInterceptor { + /** Mock an undici request with the defined reply. */ + reply( + replyOptionsCallback: MockInterceptor.MockReplyOptionsCallback + ): MockScope; + reply( + statusCode: number, + data?: + | TData + | Buffer + | string + | MockInterceptor.MockResponseDataHandler, + responseOptions?: MockInterceptor.MockResponseOptions + ): MockScope; + /** Mock an undici request by throwing the defined reply error. */ + replyWithError(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): 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 boolean)> + | ((headers: Record) => boolean); + /** Query params to intercept on */ + query?: Record; + } + export interface MockDispatch< + TData extends object = object, + TError extends Error = Error + > extends Options { + times: number | null; + persist: boolean; + consumed: boolean; + data: MockDispatchData; + } + 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; + } + export interface MockResponseCallbackOptions { + path: string; + origin: string; + method: string; + body?: BodyInit; + headers: Headers | Record; + maxRedirections: number; + } + export type MockResponseDataHandler = ( + opts: MockResponseCallbackOptions + ) => TData | Buffer | string; + export type MockReplyOptionsCallback = ( + opts: MockResponseCallbackOptions + ) => { + statusCode: number; + data?: TData | Buffer | string; + responseOptions?: MockResponseOptions; + }; } - export interface MockResponseOptions { - headers?: IncomingHttpHeaders; - trailers?: Record; + + 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; - maxRedirections: number; } - export type MockResponseDataHandler = ( - opts: MockResponseCallbackOptions - ) => TData | Buffer | string; - export type MockReplyOptionsCallback = ( - 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; + } }