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

Better typing to SpyInstance type #8

Closed
abdulhadi-lababidi98 opened this issue Aug 2, 2023 · 1 comment · Fixed by #7
Closed

Better typing to SpyInstance type #8

abdulhadi-lababidi98 opened this issue Aug 2, 2023 · 1 comment · Fixed by #7

Comments

@abdulhadi-lababidi98
Copy link

I was working on adding tests to react component, and the component basically called FormSelectBranch and it has the following props:

repositoryURLField: FormField<string>,
branchField: FormField<string>,

So inside the test file I did:

    beforeEach(() => {
        mockReactComponents_FormSelectBranch.FormSelectBranch.calls(MorfiTestUtils.Field);
    });

And the test introduced weird behaviours, after hour of debugging I realised the problem with my code, and to solve the problem I need to wrap the MorfiTestUtils.Field function with another functions that pass the props properly:

        mockReactComponents_FormSelectBranch.FormSelectBranch.calls(
            ({ repositoryURLField }: any) => MorfiTestUtils.Field({ field: repositoryURLField }),
            ({ branchField }: any) => MorfiTestUtils.Field({ field: branchField })
        );

The expected behaviour:

I should get type error for the first code snippet

Suggested solution

We can update the types of SpyInstance to be more safe, something like this:

type SpyInstance<T extends (...args: any) => any> = {
    calls: (...funcs: Array<(...params: Parameters<T>) => ReturnType<T>>) => SpyInstance<T>;
};

And we can do so for the other functions

@vikingair
Copy link
Owner

I didn't add type safety in first place, because it also adds a burden on the writer of a test that the provided function needs to fulfill the interface. That would make some "any" casts necessary in case you want to be less accurate.

However, also looking into the behavioral change of other test frameworks, it seems more beneficial to have it. As it would be a breaking change, I'll include it into the pending v4 release.

@vikingair vikingair mentioned this issue Aug 2, 2023
@vikingair vikingair linked a pull request Jan 11, 2024 that will close this issue
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

Successfully merging a pull request may close this issue.

2 participants