0

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';
Pablo LION
  • 1,294
  • 6
  • 20
  • 1
    Could not recreate (in Jest 26 or 27), once I `@ts-ignore` the unused `x` parameter that test passes for me. `mockCallback.mock.results` is `[ { type: 'return', value: 42 }, { type: 'return', value: 42 } ]`. – jonrsharpe Sep 30 '21 at 09:06
  • Thank you for editing the question. I wanted a minimal problem so I changed `x+42` to `x` and deleted `export {}`. My file `App.test.tsx` is just the code I give. Also I tried removing the `x` and `results: [ { type: 'return', value: undefined }, { type: 'return', value: undefined } ]`. I'll add version info in the question. – Pablo LION Sep 30 '21 at 15:08
  • If you're using `react-scripts` to run the tests I'd guess it's because the mocks are cleared between tests by their default config (so when your mock function is called in the test, it _does_ return `undefined`); possible dupe of e.g. https://stackoverflow.com/q/65626653/3001761, https://stackoverflow.com/q/66984091/3001761. – jonrsharpe Sep 30 '21 at 15:30
  • I add my setupTest.ts. I don't think its a dupe because we are using different methods. – Pablo LION Sep 30 '21 at 15:35
  • Why would that matter? The point is still that the default Jest config used by `react-scripts` clears the mocks. – jonrsharpe Sep 30 '21 at 15:37
  • You mean the `react-scripts` only clears `value` in `...mockCallback.mock.calls.result` but `mockCallback.mock.calls` are kept? Although this seems wierd for me, maybe it is the reason. I'm very new to this. – Pablo LION Sep 30 '21 at 15:41
  • No, it doesn't clear the value. It clears the _mock implementation_, `(x) => 42`, so you get the default `jest.fn` implementation (which does indeed return `undefined`, which is then correctly stored as the value). – jonrsharpe Sep 30 '21 at 15:47
  • Can you add this as a comment so I can close this question? – Pablo LION Sep 30 '21 at 18:15

0 Answers0