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

The captor function causes type errors when used with calledWith. #117

Open
adam-arold opened this issue May 3, 2023 · 0 comments
Open

Comments

@adam-arold
Copy link

adam-arold commented May 3, 2023

If I try to use the captor function in a calledWith call (which accepts a Matcher) I get:

Argument of type '[CaptorMatcher<StateEntity<string, unknown>>]' is not assignable to parameter of type '[stateInstance: StateEntity<string, unknown>] | [stateInstance: StateEntity<string, unknown> | Matcher<StateEntity<string, unknown>>]'.
  Type '[CaptorMatcher<StateEntity<string, unknown>>]' is not assignable to type '[stateInstance: StateEntity<string, unknown> | Matcher<StateEntity<string, unknown>>]'.
    Type 'CaptorMatcher<StateEntity<string, unknown>>' is not assignable to type 'StateEntity<string, unknown> | Matcher<StateEntity<string, unknown>>'.
      Property 'description' is missing in type 'CaptorMatcher<StateEntity<string, unknown>>' but required in type 'Matcher<StateEntity<string, unknown>>'.ts(2345)
Matchers.d.ts(4, 22): 'description' is declared here.

To me it seems that CaptorMatcher for some reason doesn't implement description, so Typescript complains.

If I do this however:

const c = captor<StateEntity<string, unknown>>();

stateRepository.upsert
    .calledWith(c as unknown as Matcher<StateEntity<string, unknown>>) // 👈 this part
    .mockReturnValue(TE.right(c.value));

then it works as intended. I suggest implementing description properly. This is a working implementation:

import { Matcher } from "jest-mock-extended";

export class FixedCaptorMatcher<T> extends Matcher<T> {
    public readonly value!: T;
    public readonly values: T[] = [];

    constructor() {
        super((actualValue: T) => {
            // eslint-disable-next-line @typescript-eslint/ban-ts-comment
            // @ts-ignore
            this.value = actualValue;
            this.values.push(actualValue);
            return true;
        }, "captor");
    }

    override getExpectedType() {
        return "Object";
    }
}
@adam-arold adam-arold changed the title captor function is not working, it doesn't implement Matcher properly The captor function causes type errors when used with calledWith. May 3, 2023
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

1 participant