NagRock / ts-mockito

Mocking library for TypeScript
MIT License
969 stars 93 forks source link

Optional method parameters are no matched with anything() #196

Open Necroskillz opened 4 years ago

Necroskillz commented 4 years ago

I have a small issue where setups are only match to exact number of parameters. So if there are optional parameters the setup is no matched and I need to create additional setups for each parameter count. It would be nice to have anything match an unspecified parameter, or have some other way to specify that it's optional (so you can have optional string, optional number etc).

class A {
    method(a: string, b?: string) {
        return a + b;
    }
}

describe('test', () => {
    let a: A;

    beforeEach(() => {
        a = mock(A);

        when(a.method(anything(), anything())).thenReturn('a');
        // when(a.method(anything())).thenReturn('a'); <-- this would be needed
    });

    it('should work', () => {
        expect(instance(a).method('x', 'y')).toBe('a'); // ok
        expect(instance(a).method('x')).toBe('a'); // null
    });
});
PhakornKiong commented 1 year ago

Just ran into this as well

fabiosangregorio commented 5 months ago

+1, any updates on this?