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

How to access jest's assert functions in a custom matcher #10590

Closed
amirsaleem-kn opened this issue Oct 5, 2020 · 4 comments
Closed

How to access jest's assert functions in a custom matcher #10590

amirsaleem-kn opened this issue Oct 5, 2020 · 4 comments

Comments

@amirsaleem-kn
Copy link

amirsaleem-kn commented Oct 5, 2020

💬 Question

I am writing a custom matcher for my own API response schema. I want to use jest's built-in assert functions inside my custom matcher so that I don't have to write my own test functions inside my custom matcher. Here is my code

expect.extend({
    toContainValidAPIResponseBody(received, argument) {

        const pass = this.toMatchObject({
            data: expect.any(Array),
            errors: expect.any(Array),
            status: expect.any(String)
        });

        const message = () => (`expected ${this.utils.printReceived(received)} to be a valid Farmer Backend API Response`);

        if (pass) {
            return {
                message,
                pass: true
            };
        } else {
            return {
                message,
                pass: false
            };
        }
    }
});

The problem is that this.toMatchObject is not a valid statement. Is there a way to access functions like toMatchObject inside a custom matcher?

@amirsaleem-kn amirsaleem-kn changed the title How to access jest's custom assert functions in a custom matcher How to access jest's assert functions in a custom matcher Oct 5, 2020
@SimenB
Copy link
Member

SimenB commented Oct 12, 2021

that's not currently possible. However, I like the idea. Wanna send a PR for it? 🙂

@github-actions
Copy link

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label Oct 12, 2022
@github-actions
Copy link

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 11, 2022
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 12, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants