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

656
Visualizações
Jest spyOn method without mockImplementation invocation is mocking implementation

So according to Jest documentation and multiple posts on the web and so was my belief, using jest.spyOn(foo, "bar") would wrap the specified function with methods that allow us to perform assertions over foo.bar without changing the actual implementation. To change the implementation we would need to use mockImplementation etc...

However, I am experiencing an issue where using jest.spyOn(foo, "bar") is clearly behaving like a jest.fn() mock.

/** 
 * Test causing the issue
 */

    test("when invoked with custom options does not override include locations setting", async () => {
      const defaultQueryConfig = { include: { locations: true } }
      const findFirstSpy = jest.spyOn(prismaConnection.event, "findFirst")

      await findOneEventByIdWithLocation(666, { include: { locations: false } })

      expect(findFirstSpy).toHaveBeenCalledTimes(1)
      expect(findFirstSpy).toHaveBeenCalledWith(expect.objectContaining(defaultQueryConfig))
    })
    

/**
 * Another test that runs in the suite at a later point in time fails
 * because instead the object it is trying the result is `undefined`
 */
    test("when event locations are requested it returns events with related locations", async () => {
      const events = await findManyEventsWithLocations()
      const locations = events.data.find((e) => e.id === 4)?.locations[0]

      expect(locations).toEqual({
        ...eventLocation,
        id: 1,
        eventId: 4,
        createdAt: expect.any(Date),
        updatedAt: expect.any(Date)
      })
    })

However, if I add a statement: findFirstSpy.mockRestore() after the expectations of the first test the second test will pass fine and dandy... But this is only valid if the first test passes since the test will not get a chance to "mockRestore" if a previous assertion fails.

I could always add a "describe" with a "beforeEach"/"afterEach" around this first test, but I think this should not be the solution, considering jest.spyOn should not change implementation unless explicitly requested.

Does anybody know why this happens?

Result without findFirstSpy.mockRestore() Result without findFirstSpy.mockRestore()

Result with findFirstSpy.mockRestore() Result with findFirstSpy.mockRestore()

about 4 years ago · Santiago Gelvez
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