TypeError during Jest's spyOn: Cannot set property getRequest of #<Object> which has only a getter
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
As suggested in the comments, jest requires a setter on the tested object which es6 module objects don't have. jest.mock()
allows you solving this by mocking your required module after the import.
Try mocking the exports from your serverRequests file
import * as serverRequests from './../../utils/serverRequests';
jest.mock('./../../utils/serverRequests', () => ({
getRequest: jest.fn()
}));
// ...
// ...
it("should get the int question list", () => {
const getRequestMock = jest.spyOn(serverRequests, "getRequest")
fetch.mockResponseOnce(JSON.stringify(response));
expect.assertions(2);
return getIntQuestionList().then(res => {
expect(res).toEqual(expected);
expect(getRequestMock).toHaveBeenCalledWith(ROUTE_INT_QUESTIONS, intQuestionListSchema);
});
});
Here are some useful links:
https://jestjs.io/docs/en/es6-class-mocks
https://jestjs.io/docs/en/mock-functions
Tested with ts-jest
as compiler, it will work if you mock the module in this way:
import * as serverRequests from "./../../utils/serverRequests";
jest.mock('./../../utils/serverRequests', () => ({
__esModule: true,
...jest.requireActual('./../../utils/serverRequests')
}));
const getRequestMock = jest.spyOn(serverRequests, "getRequest");
Jest oficial doc for __esModule