gpt4 book ai didi

reactjs - Jest 进行 spy 事件时出现类型错误 : Cannot set property getRequest of# which has only a getter
转载 作者:行者123 更新时间:2023-12-03 13:07:29 35 4
gpt4 key购买 nike

我正在使用 TypeScript 编写 React 应用程序。我使用 Jest 进行单元测试。

我有一个进行 API 调用的函数:

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);

getRequest 函数如下所示:

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));
})
);

我想像这样使用 Jest 尝试 API 功能:

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);
});
});
});

问题是带有 spyOn 的行抛出以下错误:

  ● 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)

我用谷歌搜索了这个,只找到了有关热重载的帖子。那么在 Jest 测试期间什么可能导致这个问题呢?我怎样才能通过这个测试?

最佳答案

这个很有趣。

问题

Babel 生成仅为重新导出函数定义的 get 的属性。

utils/serverRequests/index.ts 从其他模块重新导出函数,因此当使用 jest.spyOn 监视重新导出的函数时会引发错误.

<小时/>

详细信息

鉴于此代码重新导出 lib 中的所有内容:

export * from './lib';

...Babel 产生以下内容:

'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];
}
});
});

请注意,所有属性均仅通过 get 定义。

尝试在任何这些属性上使用 jest.spyOn 都会生成您所看到的错误,因为 jest.spyOn 尝试用包装原始属性的 spy 来替换该属性函数,但如果仅使用 get 定义属性则不能。

<小时/>

解决方案

不要将 ../../utils/serverRequests(重新导出 getRequest)导入到测试中,而是导入 getRequest 所在的模块code> 已定义并使用该模块来创建 spy 。

替代解决方案

按照@Volodymyr和@TheF的建议模拟整个utils/serverRequests模块

关于reactjs - Jest 进行 spy 事件时出现类型错误 : Cannot set property getRequest of#<Object> which has only a getter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53162001/

35 4 0