Unit testing for undici.fetch #1054
-
I am building Node.js http-client that uses undici.fetch under the hood. I am looking for a solution like nock. I see that undici has MockClient for that purpose. Should I use MockClient if I am using undici.fetch, or is it only for non-fetch undici? So far I tried 'fetch-mock-jest', it seems to do the job but not well. I would love to hear your recommendation about how to test properly. jest.mock('undici', () => ({
...jest.requireActual('undici'),
fetch: jest.fn(() => require('fetch-mock-jest').sandbox())
})); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I would recommend to follow https://undici.nodejs.org/#/docs/best-practices/mocking-request. All things apply to fetch as request as they are underpinned by the same logic. |
Beta Was this translation helpful? Give feedback.
I would recommend to follow https://undici.nodejs.org/#/docs/best-practices/mocking-request. All things apply to fetch as request as they are underpinned by the same logic.