I am currently testing something which uses http
and I have run into something which I really can’t figure out.
The problem:
I import the http
package, mock it, and then in my first test I mockImplementationOnce
for it, it works fine. But when I do it in another test, it says TypeError: http.request.mockImplementationOnce
is not a function.
The Code:
index.test.js
const http = require('http');
jest.mock('http');
describe('httpRequest', () => {
// This test passes
test('gets response correctly', () => {
http.request.mockImplementationOnce((opts, cb) => {
// unrelated logic
});
const response = someFunctionCallingHttpRequest();
expect(response).resolves.toBe('something')
});
test('handles something else correctly', () => {
// This test throws an error here
http.request.mockImplementationOnce((opts, cb) => {
// unrelated logic
});
const response = someFunctionCallingHttpRequest();
expect(response).resolves.toBe('something')
});
});
The error:
I get the following error in the second test suite
● httpRequest › handles something else correctly
TypeError: http.request.mockImplementationOnce is not a function
Any help would be really appreciated!