Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Necroskillz opened this issue Jul 6, 2020 · 2 comments
Open

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

Necroskillz opened this issue Jul 6, 2020 · 2 comments

Comments

@Necroskillz
Copy link

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
Copy link

Just ran into this as well

@fabiosangregorio
Copy link

+1, any updates on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants