-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Browser: Don't panic on nil page.on handlers
#5461
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
Conversation
ankur22
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGMT 🚀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 🎉 with some suggestions.
| tests := map[string]struct { | ||
| fun string | ||
| wantErr string | ||
| }{ | ||
| "nil on('console') handler": { | ||
| fun: `page.on('console')`, | ||
| wantErr: `TypeError: The "listener" argument must be a function`, | ||
| }, | ||
| "valid on('console') handler": { | ||
| fun: `page.on('console', () => {})`, | ||
| }, | ||
| "nil on('metric') handler": { | ||
| fun: `page.on('metric')`, | ||
| wantErr: `TypeError: The "listener" argument must be a function`, | ||
| }, | ||
| "valid on('metric') handler": { | ||
| fun: `page.on('metric', () => {})`, | ||
| }, | ||
| "nil on('request') handler": { | ||
| fun: `page.on('request')`, | ||
| wantErr: `TypeError: The "listener" argument must be a function`, | ||
| }, | ||
| "valid on('request') handler": { | ||
| fun: `page.on('request', () => {})`, | ||
| }, | ||
| "nil on('response') handler": { | ||
| fun: `page.on('response')`, | ||
| wantErr: `TypeError: The "listener" argument must be a function`, | ||
| }, | ||
| "valid on('response') handler": { | ||
| fun: `page.on('response', () => {})`, | ||
| }, | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since page.on runs for every page.on event, I believe checking only one event (or not at all) is enough to test it, rather than going through every event name. Otherwise, we slow down the already slow tests and have to keep maintaining this test for every event type we add. It's not the end of the world, but I don't think it's worth it.
No strong opinion! 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason why I did so is because although for now it's a bit redundant (I know the same line is executed despite the event type), in case of refactoring code, that corner case would still be covered, at least for the types that exist now.
If we follow the same idea you suggested, arguably the "valid" test cases are also a bit redundant. So... do you want me to write a single test named something like TestPageOnWithNilHandler, with a single call to page.on? If that's enough, I'm more than happy. Indeed, that's what I wrote first, trying to quickly fix this panic, but then I thought that maybe it was better to have broader coverage 🤷🏻 If it feels useless, and has downsides as making the test suite slightly slower/take longer, then I'm happy to simplify this.
ankur22
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did a test to see the behaviour difference between a panic with a typed error as @joanlopez has implemented vs what we had for these cases when we wanted to abort the whole test run with k6ext.Abortf.
With panic with a typed error (and a simple browser test running 10 iterations with 5 VUs), the panic does not stop the test run but fails the iteration. The errors are pretty clear:
ERRO[0001] Uncaught (in promise) TypeError: The "listener" argument must be a function
running at go.k6.io/k6/internal/js/modules/k6/browser/browser.mapPage.mapPageOn.func67 (native)With k6ext.Abortf with the same test we get a lot of noise which doesn't explain the issue at all, but the whole test does end early:
ERRO[0001] communicating with browser: websocket: close 1006 (abnormal closure): unexpected EOF category=cdp elapsed="0 ms" source=browser
ERRO[0001] closing the browser: context canceled category="Browser:Close" elapsed="0 ms" source=browser
ERRO[0001] process with PID 55218 unexpectedly ended: signal: killed category=browser elapsed="9 ms" source=browser
ERRO[0001] communicating with browser: websocket: close 1006 (abnormal closure): unexpected EOF category=cdp elapsed="0 ms" source=browser
ERRO[0001] communicating with browser: websocket: close 1006 (abnormal closure): unexpected EOF category=cdp elapsed="0 ms" source=browserI think having a clear message is more important. We probably need to revisit the panicIfFatalError (which is a wrapper around k6ext.Abortf), test and possibly fix it.
|
@ankur22 Wouldn't returning a non-fatal error (instead of a |
It's almost identical. THe only difference is that it is presented as a go error instead of a typed error. |
That's because @joanlopez returns it as |
I'm not sure if I understand what you mean @inancgumus, but note that Besides what we have discussed so far (UX-related), another part of the reason why I did it like this is also because the pattern: That said, note that I'd also prefer to have a more idiomatic way to return errors. Not to say that I think for other types of JS errors or scenarios, isn't as easy as calling a So, I merged the branch to fix the |
Ah, OK. I thought it was returning an error! TIL.
That's fine! :) My questions are not about this PR. I'm asking in general. |
What?
It handles
nilpage.onhandlers at both layers:Why?
Because now it makes k6 to panic.
Checklist
make check) and all pass.Checklist: Documentation (only for k6 maintainers and if relevant)
Please do not merge this PR until the following items are filled out.
Related PR(s)/Issue(s)