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

187
Views
función simulada de broma que devuelve un objeto

Tengo una función que devuelve un objeto.

 function getSettings() { return { foo: 'bar' } }

Y tengo una función que usa la función anterior internamente

 function myFunction() { const { foo } = getSettings() return foo }

Y tengo una prueba que quiero cambiar el valor de foo

 it('', async () => { const value = '123' const fooObj = {value: '123'} jest.spyOn(getSettings, 'foo').mockReturnValue(fooObj ) const result = myFunction() expect(result).toBe(value) })

ingrese la descripción de la imagen aquí

Pero esto no funciona. ¿Alguna idea de cómo burlarse del valor de retorno de getSettings ?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Después de editar la pregunta (adjuntar la imagen de error) y actualizar el código

No puede hacer jest.spyOn(getSettings, 'foo') , debe colocar el objeto/módulo en el primer argumento y el segundo argumento debe ser el nombre de la función.

Puedes probarlo cambiándolo a:

 const fileWithGetSetting = require('<your-path>'); it('', async () => { // the key should be foo and not value const fooObj = {foo: '123'} jest.spyOn(fileWithGetSetting, 'getSettings').mockReturnValue(fooObj) const result = myFunction() expect(result).toBe(fooObj.foo) })

Si foo es primitivo (como en su prueba), debe actualizar la propiedad del objeto si el valor

 function myFunction() { const settings = getSettings() // operation on setting.foo (if foo is primitive) return setting.foo }

Y pasar el objeto en la prueba y no el valor primitivo

 it('', async () => { const obj = {foo: '123'} jest.spyOn(getSettings, 'foo').mockReturnValue(obj) const result = myFunction() expect(result).toBe(obj.foo) })

Cuando pasó foo en sus pruebas, lo pasó por valor, debe pasarlo por referencia

about 4 years ago · Juan Pablo Isaza 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!