jest.fn().mockReturnValue("hello") returns undefined when called in a beforeAll() but not in a beforeEach()

Viewed 21

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
  })
})

beforeAll print 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
  })
})

beforeAll print hello

0 Answers
Related