typescript - 如何在并发测试中获得独特的玩笑间谍?
问题描述
我正在模拟一个函数(反应钩子)以返回一些用于单元测试的东西,并且想用一个间谍来验证它只被调用过一次。
这适用于常规测试,但在尝试添加时test.concurrent.each
,间谍似乎在测试之间共享,并且 toHaveBeenCalledTimes 设置为运行的测试数。
我试图为每个测试创建一个唯一的间谍,但这似乎并不重要:
export const mockLazyQuery = (result: TQueryResult): jest.SpyInstance => {
const spy = jest.spyOn(Apollo, "useLazyQuery");
spy.mockImplementationOnce(() => {
return [jest.fn(), result];
});
return spy;
};
import { mockLazyQuery } from "../test-utils";
describe("useHasScope", () => {
it.concurrent.each([
[Role.STUDENT, fakeStudent],
[Role.TRAINER, fakeTrainer],
])("should redirect a %s user to the home page", async (role, me) => {
const querySpy = mockLazyQuery({
data: { me },
error: undefined,
loading: false,
});
myIdVar(me.id);
const wrapper: React.FC = ({ children }) => (
<MemoryRouter>{children}</MemoryRouter>
);
renderHook(() => useHasScope(""), { wrapper });
expect(querySpy).toHaveBeenCalledTimes(1); // <== Failing because other tests calling useLazyQuery are running concurrently
});
});
知道如何在测试中保持模拟和间谍状态私有并允许同时运行几个类似的测试吗?
解决方案
推荐阅读
- django - 如何在 Django 中创建多个表单更新(编辑)视图
- powershell - 您如何从 CMake 检测 PowerShell 版本?
- reactjs - 如何在 python 中使用 selenium webdriver 将内容设置为 mui-rte?
- c++ - 有没有办法检查按钮是否被选中?
- c++ - C++“'Shape': 无法实例化抽象类”找不到问题的根源
- c++ - 指向文字的指针?
- javascript - 如何在 NodeJs 中按块解析 Json 数据
- r - log10 y 轴标签不会出现在 R ggaluvial ggplot 上;无法将地层标签微移到垂直中心
- amazon-web-services - 使用 Terraform 为 AWS Cognito 用户池设置帐户恢复首选项
- ruby - Ruby 关键字参数说明