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

fetchMock is not defined after upgrading to 0.4.0 #28

Closed
DotCoyote opened this issue Oct 28, 2024 · 5 comments · Fixed by #29
Closed

fetchMock is not defined after upgrading to 0.4.0 #28

DotCoyote opened this issue Oct 28, 2024 · 5 comments · Fixed by #29
Assignees
Labels
bug Something isn't working fix available

Comments

@DotCoyote
Copy link

Hello!

Unfortunately the documentation does not include any migration and I assume fetchMock should be available as before without any changes, but in my NextJS/Typescript Project, I get ReferenceError: fetchMock is not defined for all tests using fetchMock.ANYMETHOD().

Anything I'm missing?

@dirkluijk
Copy link
Collaborator

dirkluijk commented Oct 28, 2024

I think it's because [email protected] was installing fetchMock also on the global object. I see this got removed in 0.0.4. Although I think it's not a good practice to work with globals, it's a breaking change. I will arrange a fix for this.

@dirkluijk
Copy link
Collaborator

dirkluijk commented Oct 28, 2024

A workaround could be to export your instance of fetchMock in the file where you create it:

export const fetchMock = createFetchMock(vi);

fetchMock.enableMocks();
import { fetchMock } from 'path/to/your/fetch-mock`;

// ...

fetchMock.ANYMETHOD()

Personally I would recommend this approach anyway, since it's a bad practice to mess around with globals.

Otherwise, you could wait for #29 to be merged and released, since it's actually a regression that breaks backwards compatibility.

@dirkluijk dirkluijk added bug Something isn't working fix available labels Oct 28, 2024
@DotCoyote
Copy link
Author

I think I can wait! Thanks for the blazing fast help!

@IanVS IanVS closed this as completed in #29 Oct 28, 2024
@IanVS IanVS closed this as completed in 17f9f68 Oct 28, 2024
@DotCoyote
Copy link
Author

Could you create a release for this fix? 🙂

@IanVS
Copy link
Owner

IanVS commented Oct 28, 2024

0.4.1 is now released.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fix available
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants