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

Jest All Asymmetric Matchers at Point #8776

Closed
nullxone opened this issue Aug 2, 2019 · 7 comments
Closed

Jest All Asymmetric Matchers at Point #8776

nullxone opened this issue Aug 2, 2019 · 7 comments

Comments

@nullxone
Copy link

nullxone commented Aug 2, 2019

🚀 Feature Proposal

Nest jest asymmetric matchers / use multiple of them at same point.

Preserve all error messages in each of nested matcher to quickly resolve errors.

Something like this?

expect.extend({
  all<T>(received: T, fn: (received: T) => void) {
    try {
      fn(received);
    } catch (e) {
      process.nextTick(() => {
        throw e;
      });
      const res = (e as any).matcherResult;
      return res || { pass: false, message: e.message };
    }
    return { pass: true, message: "" };
  },
});

it("pieces to compose", async () => {
  expect([1, { a: 1 }]).toMatchObject([1, expect.objectContaining({ a: 1 })]);

  expect([1, 2]).toEqual(expect.arrayContaining([1, expect.anything()]));

  expect({ a: 1, b: 2 }).toMatchObject({
    a: 1,
    b: expect.any(Number),
  });
});

/*
## Motivation

Please outline the motivation for the proposal.
*/

it("jest all asymmetric matcher", async () => {
  expect(1).toEqual(
    expect.all(v => {
      expect(v).toBe(1);
      expect(v + 1).toBe(2);
    })
  );
});

it("FAIL expect", async () => {
  expect(1).toEqual(
    expect.all(v => {
      expect(v).toBe(2);
      expect(v + 1).toBe(2);
    })
  );
});

it("FAIL error", async () => {
  expect(1).toEqual(
    expect.all(v => {
      throw new Error("foo!");
      expect(v).toBe(2);
      expect(v + 1).toBe(2);
    })
  );
});

/*
## Example

Please provide an example for how this feature would be used.
*/

describe("complex example", () => {
  it("use like this", async () => {
    const res = {
      users: [
        {
          id: "user1",
          posts: [{ id: "post1" }, { id: "post2" }],
        },
        {
          id: "user2",
          posts: [{ id: "post3" }],
        },
      ],
    };
    expect(res).toMatchObject({
      // users: expect.arrayContaining([
      users: expect.objectContaining([
        expect.objectContaining({
          id: "user1",
          posts: expect.all(v => {
            expect(v).toHaveLength(2);
            expect(v).toMatchObject([
              expect.objectContaining({ id: "post1" }),
              expect.objectContaining({ id: expect.anything() }),
            ]);
          }),
        }),
        expect.objectContaining({
          id: "user2",
          posts: expect.all(v => {
            expect(v).toHaveLength(1);
          }),
        }),
      ]),
    });
  });

  it("FAIL shows point where failed", async () => {
    const res = {
      users: [
        {
          id: "user1",
          posts: [{ id: "post1" }, { id: "post2" }],
        },
        {
          id: "user2",
          posts: [{ id: "post3" }],
        },
      ],
    };
    expect(res).toMatchObject({
      // users: expect.arrayContaining([
      users: expect.objectContaining([
        expect.objectContaining({
          id: "user1",
          posts: expect.all(v => {
            expect(v).toHaveLength(2);
            expect(v).toMatchObject([
              expect.objectContaining({ id: "post1" }),
              expect.objectContaining({ id: expect.anything() }),
            ]);
          }),
        }),
        expect.objectContaining({
          id: "user2",
          posts: expect.all(v => {
            expect(v).toHaveLength(2); // THIS WAS CHANGED
          }),
        }),
      ]),
    });
  });
});

Pitch

Why does this feature belong in the Jest core platform?

todo / to test

  • use with resolve rejects
  • pass async functions
  • nested expectation failures / thrown errors: should not print the test name again (but it should still print both error messages)
  • typescript types

references #5788

@nullxone
Copy link
Author

nullxone commented Aug 2, 2019

declare namespace jest {
  interface Matchers<R> {
    all(fn: (received: R) => void): R;
  }
  interface Expect {
    all(fn: (received: any) => any): any;
  }
}

@SimenB
Copy link
Member

SimenB commented Aug 3, 2019

Not sure I follow - something like https://github.com/jest-community/jest-extended/blob/master/README.md#tosatisfypredicate?

Preserve all error messages in each of nested matcher to quickly resolve errors.

That's not really possible as expect works by throwing errors

@nullxone
Copy link
Author

nullxone commented Aug 3, 2019

The code block in the opened issue above is an executable jest test file with these types.

This is not a matcher, I have only implemented it using the expect.extend api.

It is different from (jest community / tosatisfypredicate)

I have extracted a piece of the code block from above to better illustrate the point:

it("jest all asymmetric matcher", async () => {
  expect(1).toEqual(
    expect.all(v => { // -----------------> THIS IS WHAT THIS DOES
      expect(v).toBe(1);
      expect(v + 1).toBe(2);
    })
  );
});

It is possible to preserve nested error messages. The implementation above does this. Although, I am not sure if it is breaking any other useful semantics, seems to work for me.

@nullxone
Copy link
Author

nullxone commented Aug 3, 2019

This is what nested errors look like:
Screenshot 2019-08-03 at 15 46 11

@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 14 days.

@github-actions github-actions bot added the Stale label Feb 25, 2022
@github-actions
Copy link

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

@github-actions
Copy link

github-actions bot commented May 3, 2022

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 May 3, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants