Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

297
Visualizações
jest.fn().mockReturnValue("hello") returns undefined when called in a beforeAll() but not in a beforeEach()

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

almost 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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

almost 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda