This one was interesting.
Issue
Babel generates properties with only get defined for re-exported functions.
utils/serverRequests/index.ts re-exports functions from other modules so an error is thrown when jest.spyOn is used to spy on the re-exported functions.
Details
Given this code re-exporting everything from lib:
export * from './lib';
…Babel produces this:
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _lib = require('./lib');
Object.keys(_lib).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _lib[key];
}
});
});
Note that the properties are all defined with only get.
Trying to use jest.spyOn on any of those properties will generate the error you are seeing because jest.spyOn tries to replace the property with a spy wrapping the original function but can’t if the property is defined with only get.
Solution
Instead of importing ../../utils/serverRequests (which re-exports getRequest) into the test, import the module where getRequest is defined and use that module to create the spy.
Alternate Solution
Mock the entire utils/serverRequests module as suggested by @Volodymyr and @TheF