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

Fix inconsistency in rejects.toThrow() #6678

Closed
wants to merge 1 commit into from
Closed

Conversation

lstkz
Copy link
Contributor

@lstkz lstkz commented Jul 11, 2018

fixes #6675

With this fix sync and async versions of toThrow are consistent.

However, throwing non-errors is not a good practice because we don't have the stack trace.

Signed-off-by: Łukasz Sentkiewicz <sirmims@gmail.com>
@SimenB
Copy link
Member

SimenB commented Jul 11, 2018

Seems like a reasonable change to me 🙂 While I agree everything thrown should be a subclass of Error, we shouldn't make assumptions.

Mind taking a look at the failing tests?

This should also get a changelog entry 🙂

@lstkz
Copy link
Contributor Author

lstkz commented Jul 11, 2018

I am checking failed tests and this is confusing.
Example failing test:

// jest/packages/expect/src/__tests__/to_throw_matchers.test.js

test('passes', async () => {
   await jestExpect(asyncFn(false, true)).resolves.not[toThrow]();
});

asyncFn(false, true) resolves to Promise.resolve('apple')

My fix

 if (fromPromise && /*isError(actual)*/) {
                  // ^^^^^^ removed this
    error = actual;
  }

It doesn't make sense to use resolve and toThrow and the same time because if there is an error then the promise is always rejected.
In other words, assertion expect(promise).resolve.not.toThrow() should never fail for any code.

My suggestion would be to support toThrow only in rejects and remove it from resolve.

@SimenB
Copy link
Member

SimenB commented Jul 11, 2018

My suggestion would be to support toThrow only in rejects and remove it from resolve.

I think that makes sense! Or throw a useful "toThrow should only be used with rejects, not resolves".

@SimenB
Copy link
Member

SimenB commented Jul 11, 2018

@thymikee @rickhanlonii thoughts?

@Yaojian
Copy link

Yaojian commented Jul 11, 2018

@BetterCallSKY I use custom objects instead of subclasses of Error becuase there is a limitation in TypeScript when compiling to ES5

@thymikee
Copy link
Collaborator

Seems like a reasonable change to me 🙂 While I agree everything thrown should be a subclass of Error, we shouldn't make assumptions.

👍 If we can be consistent, let's do it.

@lstkz
Copy link
Contributor Author

lstkz commented Jul 19, 2018

Does it make sense to split it to rejects and resolves? It would be enough to have only promise property.

Examples

// new
expect(value).promise.toEqual(expected); 
// old
expect(value).resolves.toEqual(expected);

// new
expect(value).promise.toThrow(error); 
// old
expect(value).rejects.toThrow(expected);

https://jestjs.io/docs/en/expect
Currently, all thoThrowXYZ methods can be used only with rejects, all other assertions should be used with resolves. Hence, it can be combined because there no conflicts.

I can submit a PR, but it will be a breaking change in the API.

@thymikee
Copy link
Collaborator

@SimenB wdyt? My take is we shouldn't drop rejects/resolves because they were designed to read nicely. I'm fine with adding .promise or something next to it

@github-actions
Copy link

github-actions bot commented Sep 8, 2022

This PR is stale because it has been open 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 Sep 8, 2022
@github-actions
Copy link

github-actions bot commented Oct 8, 2022

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

@github-actions github-actions bot closed this Oct 8, 2022
@github-actions
Copy link

github-actions bot commented Nov 8, 2022

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

Successfully merging this pull request may close these issues.

Inconsistency in rejects.toThrow()
5 participants