Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getMiniflareFetchMock global type not defined #348

Closed
lourd opened this issue Aug 25, 2022 · 4 comments
Closed

getMiniflareFetchMock global type not defined #348

lourd opened this issue Aug 25, 2022 · 4 comments

Comments

@lourd
Copy link

lourd commented Aug 25, 2022

When trying to use the new fetch mocking capability, it doesn't appear that the getMiniflareFetchMock global function has a type definition that's set up for end users to consume. I would expect that adding 'jest-environment-miniflare' to the compilerOptions.types array of my tsconfig would add that function to the global types, but it does not. Which makes sense because looking at the index.d.ts in the published npm module, getMiniflareFetchMock is nowhere to be found.

@mrbbot
Copy link
Contributor

mrbbot commented Aug 25, 2022

Hey! 👋 I agree these should be declared somewhere. They're actually here...

declare global {
function getMiniflareBindings<Bindings = Context>(): Bindings;
function getMiniflareDurableObjectStorage(
id: DurableObjectId
): Promise<DurableObjectStorage>;
function getMiniflareFetchMock(): MockAgent;
function flushMiniflareDurableObjectAlarms(
ids: DurableObjectId[]
): Promise<void>;
}

...but API Extractor, the tool we use for bundling type definitions, doesn't include them. 😢

I would suggest you declare them yourself like this: https://github.com/mrbbot/miniflare-typescript-esbuild-jest/blob/master/test/globals.d.ts
...but I've just tried to upgrade that template and hit a bunch of errors due to conflicting @cloudflare/workers-types and @types/node (required by undici) types. 😅

Will have a think about this, but I suspect the quickest solution will be to manually define the MockAgent types yourself. 😢

I'm also going to close this as a duplicate of #94.

@mrbbot mrbbot closed this as not planned Won't fix, can't repro, duplicate, stale Aug 25, 2022
@aeneasr
Copy link

aeneasr commented Dec 21, 2022

Could this be fixed? Looks like it's just some configuration you need to do in the API Extractor. Anyone using this library with TypeScript hits this sooner or later.

@tizmagik
Copy link

tizmagik commented Jan 26, 2023

I just hit this today (in vitest). For now I just did this:

import type { MockAgent } from "undici";
declare const getMiniflareFetchMock: () => MockAgent;

Which, yes, unfortunately means I had to add undici as a direct devDependency

@lucaslago
Copy link

+1 a bit of a hassle to setup the types for jest environment ourselves

mrbbot added a commit that referenced this issue Feb 7, 2023
Miniflare's Jest and Vitest environments define a few global helper
functions. Previously, the global type augmentations for these
functions were included with the implementations. Unfortunately,
API extractor (the tool we use to bundle `*.d.ts` files), doesn't
support bundling global augmentations (microsoft/rushstack#1176).

This change extracts those augmentations into separate `globals.d.ts`
files. This means users will be able to add
`jest-environment-miniflare/globals` or
`vitest-environment-miniflare/globals` to their `types` configuration
and get these types in their environment.

Ref #348
mrbbot added a commit that referenced this issue Feb 13, 2023
Miniflare's Jest and Vitest environments define a few global helper
functions. Previously, the global type augmentations for these
functions were included with the implementations. Unfortunately,
API extractor (the tool we use to bundle `*.d.ts` files), doesn't
support bundling global augmentations (microsoft/rushstack#1176).

This change extracts those augmentations into separate `globals.d.ts`
files. This means users will be able to add
`jest-environment-miniflare/globals` or
`vitest-environment-miniflare/globals` to their `types` configuration
and get these types in their environment.

Ref #348
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants