fetch-mock-jest
fetch-mock-jest copied to clipboard
Simplify implementation
Currently fetch-mock and jest are quite convolutedly entwined in this implementation. It could potentially be simplified with the following approach
- Retain the
requireActualusage (which now lives infetch-mockanyway) - 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-jeststill adds extensions to jest, but that check to see if the mockImplementation is a fetch-mock instance, and error if not.
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()?