我正在尝试从 Firebase 进行模拟测试获取数据。在Jest 文档中,据说
// The return value of the first call to the function was 42
expect(mockCallback.mock.results[0].value).toBe(42);
但我用下面的代码console.log(mockCallback.mock)证明了这一点。mockCallback.mock.results[0].value === undefined将jest.fn()定义移入测试后,它是 42。
这种行为是这样设计的吗?如果是这样,我在哪里可以编辑文档以使其更清晰?
function forEach(items: number[], callback: CallableFunction) {
for (let index = 0; index < items.length; index++) {
callback(items[index]);
}
}
const mockCallback = jest.fn((x) => 42);
test("f", () => {
// const mockCallback = jest.fn((x) => 42);
// If mockCallback is defined here, then everything is correct
forEach([0, 1], mockCallback);
console.log(mockCallback.mock);
expect(mockCallback.mock.results[0].value).toBe(42);
});
我的笑话版本:
> npm view jest version
27.2.4
但我yarn test在 VSCode 终端中使用,带有反应版本17.0.2,并且我的 中没有jest依赖项package.json,只有:
{
"dependencies": {"@types/jest": "^26.0.15"}
"scripts": {"test": "react-scripts test"}
}
我的setupTests.ts(在@testing-library/jest-dom/package.jsonnode_modules 中说{"name": "@testing-library/jest-dom", "version": "5.14.1",...}):
import '@testing-library/jest-dom';