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

655
Views
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
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!