-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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-jasmine2]: Test throws error when a non promise/undefined value is returned #6516
Comments
the reason behind adding this error was that sometimes code under test might return something other than promise when promise is expected.
if now if something in the code changes and This was a common and dangerous thing :) Things are pretty different now since we mostly use async functions, but i'm still not sure if it's worth removing this invariant. |
Ah I see why it was added now π While I agree itβs not good to allow tests to give false positives by passing when never running the async code, I think it would be great if we could offer an alternative solution than throwing. With I think overall I agree with the sentiment of the behaviour but it would be better to provide advice on best practises and the tools to check for user error rather than throwing an exception. @aaronabramov how would you feel about moving this to eslint rules etc or do you think itβs too big of a change? Happy to go with what you think dude :) |
i also remember than should we disallow explicit return in general? by suggesting to replace test('111', () => {
return makeHttpRequest();
}); with: test('111', async () => {
await makeHttpRequest();
}); @cpojer do you have any thoughts on that? |
Adding an eslint rule for "never return anything" is pretty easy as a first step, fwiw |
Good candidate for recommended |
there may be another use case. I'm an author of bdd-lazy-var tests interface which makes JS BDD test runners like mocha, jasmine and jest to look more like rspec. Rspec has very nice shortcuts like: describe Array do
subject { [1,2,3] }
it { is_expected.to include(1) }
end So, I'm in a process of adding support for describe('Array', () => {
subject(() => [1,2,3] })
it(() => is.expected.toContain(1))
}) As you can see In order to overcome this, I can create a wrapper function but then I will create a redundant wrapper function for each So, I guess the minium which may satisfy all parties is to allow to return What do you think guys? |
Adds a rule for not returning inside of a test. Related issue in Jest: jestjs/jest#6516
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. |
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. |
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. |
π Bug Report
I'm not sure if this is a bug report or feature request π€·ββοΈ
Currently Jest throws the following error if the return value from a test is not either a
Promise
orundefined
:I'm not sure what value this behaviour adds to Jest - while it does seem weird to return from inside a test (as what would you be returning to?) I don't think that it should fail the test by throwing an error.
In
jest-chain
I've had the following issue mattphillips/jest-chain#1 raised for when using arrow functions without a body i.e.This causes the error because
jest-chain
binds all of the available matchers together to be chainable, so the expect statement is no longer returningundefined
but rather itself (the expect object).It appears this behaviour was added a while back when adding support for promises in 644a512d and is not something that is supported in
Jasmine
itself - norjest-circus
from what I could see.Solution
I think a better solution to this would be to remove the error throwing code here and instead add a new rule to eslint-plugin-jest if we think this is something that we should advise against rather than throwing an error.
I'm happy to send a PR with the changes if you guys approve removing this error scenario?πEDIT: It was such a quick change and I wanted to see it working so I've sent a PR see #6517
To Reproduce
Expected behavior
I would expect this to not throw an error.
Link to repl or repo (highly encouraged)
https://repl.it/repls/SorrowfulStarkRouter
The text was updated successfully, but these errors were encountered: