golevelup / nestjs

A collection of badass modules and utilities to help you level up your NestJS applications 🚀
MIT License
2.26k stars 259 forks source link

fix(ts-sinon): fixed match all issue of jest mocks #752

Closed VonRehberg closed 3 months ago

VonRehberg commented 3 months ago

This PR fixes issue #703

VonRehberg commented 3 months ago

Thanks for maintaining these tools and for merging!

I was considering to mark my commit as a breaking change instead of a patch. In my opinion it fixes an issue, but it could cause a lot of tests to fail in packages using renovate. Please consider this when publishing to npm

rbnayax commented 1 month ago

Hi guys, this causes a major regression. For example, we can't use jest-when lib anymore as the returned mock is now a proxy and not jest.fn() and as such we can't set any properties to it. Please undo this and find a better solution for the issue that initiated this PR.

rbnayax commented 1 month ago

The following test passes in 0.5.0, but fails in 0.5.1. Still trying to figure out why it fails.

import { createMock } from '@golevelup/ts-jest';

describe(`test`, () => {
  it('mock called with another mock', async () => {
    interface Test {
      foo(): number | undefined;
    }

    const mock = createMock<Test>();
    mock.foo.mockImplementation(() => {
      return 6;
    });
    expect(mock.foo()).toEqual(6);
    mock.foo.mockImplementation(() => {
      return 7;
    });
    expect(mock.foo()).toEqual(7);
    mock.foo.mockImplementation(() => {
      return 0;
    });
    expect(mock.foo()).toEqual(0); // Fails
    mock.foo.mockImplementation(() => {
      return undefined;
    });
    expect(mock.foo()).toEqual(undefined); //Fails
  });
});
Error: Unexpected return from a matcher function.
Matcher functions should return an object in the following format:
  {message?: string | function, pass: boolean}
'{"actual": [Function mockConstructor], "expected": 0, "message": [Function anonymous], "name": "toEqual", "pass": [Function mockConstructor]}' was returned
rbnayax commented 1 month ago

The new apply trap is causing the issue, working on a fix