-
Notifications
You must be signed in to change notification settings - Fork 5
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
stub_fetch
clobbers jest-fetch-mock
#7
Comments
I would guess (without trying, but I will if this doesn't work), that something like this should work: import {makeEdgeEnv} from 'edge-mock'
import mockFetch from 'jest-fetch-mock'
describe('whatever', () => {
beforeEach(() => {
makeEdgeEnv()
mockFetch.enableMocks()
jest.resetModules()
})
test('my-test', async () => {
...
})
}) |
If it helps, you might want to have a look at https://github.com/samuelcolvin/edgerender/tree/main/tests, in particular |
We could also change the behaviours of import {makeEdgeEnv, SKIP_MOCK} from 'edge-mock'
...
makeEdgeEnv({ fetch: SKIP_MOCK }) PR welcome for this. |
I'm assuming this is fixed, but feel free to ask more questions if not. |
I'd almost go the other way, since the behaviour of your fetch mock is pretty narrow, I can see it needing to be configured: import {makeEdgeEnv, stubFetch} from 'edge-mock'
makeEdgeEnv({ fetch: stubFetch() }) That'd make it really easy to use import {makeEdgeEnv, stubFetch} from 'edge-mock'
makeEdgeEnv({ fetch: stubFetch({
200: ['https://api.my-domain.com/*'],
})}) I think you could keep the same behaviour as-is, or actually have no default fetcher if you don't pass in a In any case, passing |
This isn't fixed, btw. I've got a branch trying to get the two working together but no luck so far. Have put |
Maybe easiest just to remove the default mock of fetch. For individual cases it's pretty easy to mock it yourself. |
Just tried switching from
service-worker-mock
toegde-mock
, but ran into trouble with me trying to capture and verify the subrequests going from the worker.I've been using jest-fetch-mock, with the following line in
jest.setup.js
:No matter what I do, I can't seem to make it work with
makeEdgeEnv
, since that's injectingfetch
in a way that breaks it. I've tried:But I get:
Weirdly, sometimes running the tests a second time (in Jest's watcher) causes them to pass, so something strange is going on with the global namespace. But wondering if we could figure out a way that if...
globalThis.fetch
already exists, say, don't inject it? Only inject your example.com/node-fetch one if it isn't?Just FYI: I'm working on trying to upgrade
worker-typescript-template
to be a bit more modern (using module syntax for exports, in particular). Would love to get the testing story upgraded too. I've already got aTEST_ENV=edge
mode that useswrangler dev
under the hood, which is pretty neat. But if we can make this library a drop-in forservice-worker-mock
I'd be super happy to use it.The text was updated successfully, but these errors were encountered: