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

157
Views
Jest Burlándose de múltiples instancias de clase

Tengo una clase que instancia varias instancias del mismo objeto y quiero burlarme de esto en broma, pero sigo recibiendo un error por el segundo it(()=>{})('Debería crear un segundo saludo')

Error: expect(received).toBe(expected) // Object.is equal

Esperado: "Este es el-segundo-saludo"

Recibido: "Este es el-primer-saludo"

Esperado: "Hola y buenas noches"

Recibido: "Hola y Buenos Días"

 import {Greeting} from 'somefile/greeting' interface FooProps { myProps: string } class Foo { private greeting1: Greeting; private greeting2: Greeting; constructor(bar: Bar, id: string, props: FooProps) { this.greeting1 = new Greeting(bar, `${id}-first-greeting`, { prop1: 'Hello and Good Morning', prop2: { source: ['random'] } }) this.greeting2 = new Greeting(bar, `${id}-second-greeting`, { prop1: 'Hello and Good Evening', prop2: { source: ['anotherRandom'] } }) } }
 import Foo from 'somefile/foo' import {Greeting} from 'somefile/greeting' jest.mock('somefile/greeting'); const FirstMock = mocked(Greeting, true); const SecondMock = mocked(Greeting, true); const id = 'This is'; describe('Greeting', () => { let bar: Bar; let foo: Foo; let random1 = ['random']; let random2 = ['anotherRandom']; const props: FooProps = { myProps: 'myProps' } beforeEach( () => { bar = new Bar(); foo = new Foo(bar, id, props); }) afterEach( () => { jest.clearAllMocks(); }) it('should create first greeting', () => { expect(random.mock.calls[0][0]).toBe(bar) expect(random.mock.calls[0][1]).toBe(`${id}-first-greeting`) expect(random.mock.calls[0][2]?.prop1).toBe('Hello and Good Morning') expect(random.mock.calls[0][2]?.prop2.source).toBe(random1) }) it('should create second greeting', () => { expect(random.mock.calls[0][0]).toBe(bar) expect(random.mock.calls[0][1]).toBe(`${id}-second-greeting`) expect(random.mock.calls[0][2]?.prop1).toBe('Hello and Good Evening') expect(random.mock.calls[0][2]?.prop2.source).toBe(random2) }) })

La primera prueba pasa, pero la segunda siempre falla. Parece que está conservando los valores de la prueba anterior. ¿Se puede arreglar esto?

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

0

Deberías usar jest.resetAllMocks();

clearAllMocks elimina literalmente el estado de su simulacro, por lo que está perdiendo todos los valores que ha configurado.

Mientras que resetAllMocks devolverá el simulacro a su estado original.

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!