Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

293
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda