Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

285
Views
jest.fn().mockReturnValue("hola") devuelve indefinido cuando se llama en beforeAll() pero no en beforeEach()

¿Cómo es que cuando creo una función jest.fn() y luego llamo a mockReturnValue("hello") dentro de beforeAll() me quedo undefined cuando intento consolar. Registrar el valor en una prueba pero no cuando llamar a mockReturnValue("hello") dentro de 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 imprimir indefinido

sin embargo, cuando llamo a mockReturnValue("hello") dentro de beforeEach , imprime 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 imprimir hola

almost 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Gracias a @jonrsharpe, pude rastrear mi problema.

Estoy ejecutando mi prueba a través de node_modules/react-scripts en lugar de a través de node_modules/jest

de forma predeterminada, de acuerdo con esta otra pregunta y respuesta de StackOverflow

de forma predeterminada en react-scripts node_modules/react-scripts/scripts/utils/createJestConfig.js la configuración de broma se establece con "resetMocks": true

Entonces, lo que hice, dentro de mi archivo package.json fue poner la configuración de broma de

 "jest": {
 "resetMocks": false
 }

Y volví a ejecutar mi prueba, y pasó como se esperaba

almost 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!