eratio08 / vitest-mock-extended

Type safe mocking extensions for Vitest
https://www.npmjs.com/package/vitest-mock-extended
MIT License
140 stars 4 forks source link

deep mocking freezes the test runner #427

Open Mnigos opened 3 months ago

Mnigos commented 3 months ago

good:

  describe('updateOrCreateHistoryByUser', () => {
    let historyTrack: MockProxy<HistoryTrack>
    let historyTracks: MockProxy<HistoryTrack[]>
    let playHistoryMock: DeepMockProxy<PlayHistory[]>

    beforeEach(() => {
      historyTrack = mock<HistoryTrack>()
      historyTracks = [historyTrack]
    })

    test('should be true', () => {
      expect(true).toBeTruthy()
    })

This test completes almost instantly

bad:

  describe('updateOrCreateHistoryByUser', () => {
    let historyTrack: MockProxy<HistoryTrack>
    let historyTracks: MockProxy<HistoryTrack[]>
    let playHistoryMock: DeepMockProxy<PlayHistory[]>

    beforeEach(() => {
      historyTrack = mock<HistoryTrack>()
      historyTracks = [historyTrack]

      playHistoryMock = mockDeep<PlayHistory[]>(
        sdkTracksMock.map(track => ({
          track,
          played_at: new Date().toISOString(),
        }))
      )
    })

    test('should be true', () => {
      expect(true).toBeTruthy()
    })

Takes up to 25s to complete the test or freezes test runner and doesn't complete at all

I'm using vitest v1.4.0, but issue also apears on v1.0.1. I guess it might work with 0.34.0, but not with V1

eratio08 commented 3 months ago

I will try to verify if this an issue of this lib or vitest it self. Thx for raising.

mthenw commented 2 months ago

Hey @eratio08 have you had a chance to take a look? I'm experiencing the same issue with vitest-mock-extended.

eratio08 commented 2 months ago

@mthenw sadly not, I need to make time for this, sorry!

eratio08 commented 2 months ago

@Mnigos Could you provide me a minimal repo to debug this? Would be high appreciated.

eratio08 commented 2 months ago

Using the following I could not reproduce any freezing behavior

interface HistoryTrack { }
interface SdkTrack { }
interface PlayHistory {
  track: SdkTrack
  played_at: string
}

describe('updateOrCreateHistoryByUser', () => {
  let historyTrack: MockProxy<HistoryTrack>
  let historyTracks: MockProxy<HistoryTrack[]>
  let playHistoryMock: DeepMockProxy<PlayHistory[]>

  beforeEach(() => {
    historyTrack = mock<HistoryTrack>()
    historyTracks = [historyTrack]
  })

  it('should be true', () => {
    expect(true).toBeTruthy()
  })
})

describe('updateOrCreateHistoryByUserDeep', () => {
  let historyTrack: MockProxy<HistoryTrack>
  let historyTracks: MockProxy<HistoryTrack[]>
  let playHistoryMock: DeepMockProxy<PlayHistory[]>
  const sdkTracksMock: MockProxy<SdkTrack>[] = [mock()]

  beforeEach(() => {
    historyTrack = mock<HistoryTrack>()
    historyTracks = [historyTrack]

    playHistoryMock = mockDeep<PlayHistory[]>(
      sdkTracksMock.map((track) => ({
        track,
        played_at: new Date().toISOString(),
      })),
    )
  })

  it('should be true', () => {
    expect(true).toBeTruthy()
  })
})
Mnigos commented 1 month ago

@Mnigos Could you provide me a minimal repo to debug this? Would be high appreciated.

Sure I will try to create some minimal reproduction soon.