I'm trying to do a mock test fetch data from Firebase. In Jest documentation, it is said
// The return value of the first call to the function was 42
expect(mockCallback.mock.results[0].value).toBe(42);
But my console.log(mockCallback.mock)
shows that mockCallback.mock.results[0].value === undefined
with the code below. After moving the jest.fn()
definition into the test, it is 42.
Is this behavior designed like this? If so, where can I edit the documentation to make it clearer?
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);
});
my jest version:
> npm view jest version
27.2.4
but I'm using yarn test
in VSCode Terminal, with react version 17.0.2
and I don't have jest
in dependencies in my package.json
, only:
{
"dependencies": {"@types/jest": "^26.0.15"}
"scripts": {"test": "react-scripts test"}
}
My setupTests.ts
(in @testing-library/jest-dom/package.json
node_modules it says {"name": "@testing-library/jest-dom", "version": "5.14.1",...}
):
import '@testing-library/jest-dom';