How come when I create a jest.fn() function and then call mockReturnValue("hello") on it inside of a beforeAll() I get undefined when I try to console.log the value of it in a test but not when I call the mockReturnValue("hello") inside of a beforeEach()?
import App from "./App"
describe("App", () => {
const mock = jest.fn()
beforeAll(() => {
mock.mockReturnValue("hello")
})
beforeEach(() => {
})
afterEach(() => {
})
afterAll(() => {
})
it("should console.log the value 'hello'", () => {
console.log(mock()) // undefined
})
})
however, when I call the mockReturnValue("hello") inside a beforeEach, it prints hello.
import App from "./App"
describe("App", () => {
const mock = jest.fn()
beforeAll(() => {
})
beforeEach(() => {
mock.mockReturnValue("hello")
})
afterEach(() => {
})
afterAll(() => {
})
it("should console.log the value 'hello'", () => {
console.log(mock()) // hello
})
})
Thanks to @jonrsharpe, I was able to track my issue down.
I am running my test via node_modules/react-scripts instead of via node_modules/jest
by default, according to this other StackOverflow question and answer
by default in react-scripts node_modules/react-scripts/scripts/utils/createJestConfig.js the jest configuration is set with "resetMocks": true
So what I did, inside of my package.json file was put the jest configuration of
"jest": {
"resetMocks": false
}
And re-ran my test, and it passed as expected