mcous / vitest-when

Stub behaviors of Vitest mock functions based on how they are called
MIT License
28 stars 3 forks source link

Test throws error if rejected mock is never called #7

Closed BeniRupp closed 9 months ago

BeniRupp commented 9 months ago

Bug description

When defining a mock that will return a rejected promise using .thenReject(new Error()) and this mock will not be called, the test succeeds but throws the error defined in the thenReject function.

Expected behavior

The test will succeed without throwing the error.

Example test that reproduces the problem

it('should fail minimal', () => {
    const spy = vi.fn()
    subject.when(spy).calledWith('/api/foo').thenReject(new Error('uh uhh'))
    // intentionally do not call the spy
    expect(true).toBe(true)
  })

The output of the failing test

 ✓ test/vitest-when.test.ts (24)
 ✓ example/meaning-of-life.test.ts (1)

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Errors ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

Vitest caught 1 unhandled error during the test run.
This might cause false positive tests. Resolve unhandled errors to make sure your tests are not affected.

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Rejection ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
Error: uh uhh
 ❯ test/vitest-when.test.ts:160:57
    158|   it('should fail minimal', () => {
    159|     const spy = vi.fn()
    160|     subject.when(spy).calledWith('/api/foo').thenReject(new Error('uh uhh'))
       |                                                         ^
    161|     // intentionally do not call the spy
    162|     expect(true).toBe(true)
 ❯ node_modules/.pnpm/@vitest+runner@1.0.1/node_modules/@vitest/runner/dist/index.js:127:14
 ❯ node_modules/.pnpm/@vitest+runner@1.0.1/node_modules/@vitest/runner/dist/index.js:59:26
 ❯ runTest node_modules/.pnpm/@vitest+runner@1.0.1/node_modules/@vitest/runner/dist/index.js:672:17
 ❯ runSuite node_modules/.pnpm/@vitest+runner@1.0.1/node_modules/@vitest/runner/dist/index.js:789:15
 ❯ runSuite node_modules/.pnpm/@vitest+runner@1.0.1/node_modules/@vitest/runner/dist/index.js:789:15
 ❯ runFiles node_modules/.pnpm/@vitest+runner@1.0.1/node_modules/@vitest/runner/dist/index.js:838:5
 ❯ startTests node_modules/.pnpm/@vitest+runner@1.0.1/node_modules/@vitest/runner/dist/index.js:847:3
 ❯ node_modules/.pnpm/vitest@1.0.1/node_modules/vitest/dist/entry.js:112:7
 ❯ withEnv node_modules/.pnpm/vitest@1.0.1/node_modules/vitest/dist/entry.js:79:5

This error originated in "test/vitest-when.test.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running.
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

 Test Files  2 passed (2)
      Tests  25 passed (25)
     Errors  1 error
   Start at  08:25:26
   Duration  237ms (transform 64ms, setup 0ms, collect 104ms, tests 11ms, environment 0ms, prepare 113ms)

Side note

Using thenDo will not reproduce the above problem:

it("should work", () => {
    const spy = vi.fn()
    subject.when(spy).calledWith('/api/foo').thenDo(() => new Error('uh uhh'))
    // intentionally do not call the spy
    expect(true).toBe(true)
  })
BeniRupp commented 9 months ago

@mcous do you have any idea what happens here? I couldn't figured it out.