Simplify implementation
Opened this issue · 1 comments
wheresrhys commented
Currently fetch-mock and jest are quite convolutedly entwined in this implementation. It could potentially be simplified with the following approach
- Retain the
requireActual
usage (which now lives infetch-mock
anyway) - Document that users shoudl use this pattern:
jest.mock('node-fetch', jest.fn())
const fetch = require('node-fetch');
const fetchMock = require('fetch-mock-jest').sandbox();
fetch.mockImplementation(fetchMock)
fetch-mock-jest
still adds extensions to jest, but that check to see if the mockImplementation is a fetch-mock instance, and error if not.
wheresrhys commented
Or maybe a pattern like require('fetch-mock-jest').inject(jest.fn())
Which sets up the mockImplementation etc
Which then may as well always be assumed on the import
jest.mock('node-fetch', require('fetch-mock-jest'))
const fetch = require('node-fetch');
Perhaps the key is to always use a sandbox(), and avoid the global mode of fetch-mock - leave it up to jest to do the stubbing.
Could make life a lot easier too by renaming .mock()
to something else on the sandbox. respond()
?