I'm writing a React application with TypeScript. I do my unit tests using Jest.
I have a function that makes an API call:
import { ROUTE_INT_QUESTIONS } from "../../../config/constants/routes";
import { intQuestionSchema } from "../../../config/schemas/intQuestions";
import { getRequest } from "../../utils/serverRequests";
const intQuestionListSchema = [intQuestionSchema];
export const getIntQuestionList = () => getRequest(ROUTE_INT_QUESTIONS, intQuestionListSchema);
The getRequest
function looks like this:
import { Schema } from "normalizr";
import { camelizeAndNormalize } from "../../core";
export const getRequest = (fullUrlRoute: string, schema: Schema) =>
fetch(fullUrlRoute).then(response =>
response.json().then(json => {
if (!response.ok) {
return Promise.reject(json);
}
return Promise.resolve(camelizeAndNormalize(json, schema));
})
);
I wanted to try the API function using Jest like this:
import fetch from "jest-fetch-mock";
import { ROUTE_INT_QUESTIONS } from "../../../config/constants/routes";
import {
normalizedIntQuestionListResponse as expected,
rawIntQuestionListResponse as response
} from "../../../config/fixtures";
import { intQuestionSchema } from "../../../config/schemas/intQuestions";
import * as serverRequests from "./../../utils/serverRequests";
import { getIntQuestionList } from "./intQuestions";
const intQuestionListSchema = [intQuestionSchema];
describe("getIntQuestionList", () => {
beforeEach(() => {
fetch.resetMocks();
});
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);
});
});
});
The problem is that the line with spyOn
throws the following error:
● getRestaurantList › should get the restaurant list
TypeError: Cannot set property getRequest of #<Object> which has only a getter
17 |
18 | it("should get the restaurant list", () => {
> 19 | const getRequestMock = jest.spyOn(serverRequests, "getRequest");
| ^
20 | fetch.mockResponseOnce(JSON.stringify(response));
21 |
22 | expect.assertions(2);
at ModuleMockerClass.spyOn (node_modules/jest-mock/build/index.js:706:26)
at Object.spyOn (src/services/api/IntQuestions/intQuestions.test.ts:19:33)
I googled this and only found posts about hot reloading. So what could cause this during Jest test? How can I get this test to pass?
Symbolic definite integral expression does not yield the same result as numerical evaluation
I resolved the definite integral of an expression using sympy in order to get the symbolic expression of the integral. However, when I use the yielded expression in a function, I do not get the same result as given by the numerical evaluation of the integral:
>> from sympy import *
>> x, y, a, b, c, d, k = symbols ('x y a b c d k', positive=True)
>> res = integrate(exp(-k*abs(x-y)), (x, a, b), (y, c, d))
>> res
(-exp(a*k) + exp(b*k))*exp(-b*k)*exp(-k*(a - d))/k**2 - (-exp(a*k) + exp(b*k))*exp(-b*k)*exp(-k*(a - c))/k**2
>> def integral_1(k1, a1, b1, c1, d1):
>> return (-exp(a1*k1) + exp(b1*k1))*exp(-b1*k1)*exp(-k1*(a1 - d1))/k1**2 - (-exp(a1*k1) + exp(b1*k1))*exp(-b1*k1)*exp(-k1*(a1 - c1))/k1**2
>> integral_1(0.6, 0, 1, 0, 1)
1.0303623235681536
>> integrate(exp(-0.6*abs(x-y)), (x, 0, 1), (y, 0, 1))
0.826731311633480
Why do I get such difference?