-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Error trace cryptic when a method reference not passed to afterEach #6789
Comments
Would you like to contribute this change? We're happy to review once ready :) |
@thymikee Hey! I'd love to take a look into this. Could you possibly point me in the right direction to get started? :) |
Hey @SimenB, thanks for pointing me in the right direction. From what I've observed so far, this extends to any of the hook functions. IE, What I've tried so far is do a type check in this.beforeEach = function(beforeEachFunction, timeout) {
if (typeof beforeEachFunction !== 'function') {
throw new Error(
`Invalid argument ${beforeEachFunction}. It must be a callback function.`,
);
}
currentDeclarationSuite.beforeEach({
fn: beforeEachFunction,
timeout() {
return timeout || j$._DEFAULT_TIMEOUT_INTERVAL;
},
});
}; So far in some cases where the inputs are a string or undefined, it works as expected. But when the input is a number or a boolean, it instead goes into Having said that, how strict should we be with checking the argument being provided into the hook function since the behaviour differs based on arguments passed. Here are some sample tests to test the Env.js changes. describe('hooks error testing', () => {
// Works
it('it throws error when a callback function is not passed in beforeEach', () => {
const input = 'sdfdsf';
expect(() => beforeEach(input)).toThrowError(
`Invalid argument ${input}. It must be a callback function.`,
);
});
// Doest throw anything
it('it throws error when a callback function is not passed in beforeEach', () => {
const input = 1;
expect(() => beforeEach(input)).toThrowError(
`Invalid argument ${input}. It must be a callback function.`,
);
});
}); Actual usage of the updated code // Throws TypeError: wrappedFn.call is not a function
// at Object.asyncFn (../jest/packages/jest-jasmine2/build/jasmine_async.js:63:37)
describe("test", () => {
beforeEach(1);
it("should work", () => {
expect(2).toEqual(2);
});
}); Would love your input on how we should proceed! |
@natealcedo sorry I never got back to you. However, I think this has been solved by #6917, and this I'm closing this. Do you agree? |
Hey @SimenB, thanks for reaching out. I actually don't think this is solved satisfactorily enough. I had similar looking code as #6917 however jest will only throw the helpful error message if you pass in a string in the hook functions. Numbers, symbols, arrays. I didn't proceed further as I didn't have enough context. Fixing the code just in jest-jasmine didn't fix it and I didn't know where to proceed next. I can replicate the behaviour where this will not work.
Having said that, its not really a big bug. More like a quality of life improvement. I defer to your judgement here as to whether or not you believe this warrants further investigation. :) |
Good point! It's fixed for circus, but not jasmine. Moving the checks into |
@SimenB I'd love to send in a PR. Will do when I get the chance to do so! |
Let's close this issue now since I believe this is solved satisfactorily enough with #6931 :) |
Right! |
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
The error trace generated when a method reference is not passed as expected to afterEach is cryptic and hard to debug.
To Reproduce
Expected behavior
An appropriate error message that suggests that the problem is in the
afterEach
line so that as a developer I can know what went wrong and fix it, without manually guessing which of my 100 lines of test code is problematic.Link to repl or repo (highly encouraged)
https://repl.it/@anuragkapur/ProudDependablePhysics
Run
npx envinfo --preset jest
Paste the results here:
The text was updated successfully, but these errors were encountered: