-
-
Notifications
You must be signed in to change notification settings - Fork 698
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
Throw when non-existent property is read #721
Conversation
Great idea! Awesome work @meeber. I'd love to use ES6 on our codebase. It would be great to include Babel as a build step and make it possible to use ES6 without breaking browsers compatible with ES5 only, however even if we used Babel there would be no way of including this without breaking older browsers.
I also found this module on NPM but it doesn't seem to be very popular and there's also this warning on the README:
Unfortunately I don't think there's any other possibility of intercepting calls to every single property on an object and throw an error when it does not exist. I've done some research on this about two months ago due to a new project I was working into and I couldn't find any equivalent workaround for this, at that time I even had to run Node with an especial flag to enable |
@lucasfcosta It's worth noting though that it only does the proxy stuff if Proxy is implemented, otherwise it falls back to the current ES5 behavior. |
@meeber Thanks for pointing this out. Well, then I think we wouldn't have problems bringing those changes to the core. We'd just need to contact plugin developers and ask for their input on this. Also, regarding the stack trace problem, I'm not a big fan of manipulating them as Strings. It feels too error-prone to me. I'll make sure to clone this and see if there's anything I can do about this. |
Great work @meeber, like the implementation and the tests. Thanks for also doing the due diligence to see how this impacts our plugins. I think we could spend some time to reach out to plugin authors, perhaps raise some PRs to fix these issues, before merging this. Perhaps we'll have to earmark this PR for My 2¢ |
After work I plan to test out some other plugins with this to get a sense of how much this breaks. It's possible chai-as-promised was a special case with the |
@lucasfcosta @keithamus I cloned a bunch of popular chai plugins and then ran their test suites twice, first using the current version of Chai via the master repo, and then using this PR's branch. The main takeaways are:
Findingschai-as-promised
sinon-chai
chai-jquery
dirty-chai
chai-webdriver
chai-http
chai-immutable
chai-things
chai-spies
|
Great work @meeber ❤️. I wonder if, considering this has such a large impact on the ecosystem, we should instead just park the whole proxy thing and focus our efforts in other areas? If we are going to have such an impact by trying to solve the whole property assertion thing, we could just rip the bandaid off and remove property assertions altogether. If either solution will take as much effort then the latter is probably preferred by the community at large. To expand - we get lots of vocal complaints about property assertions (e.g. misspelling, linting rules disliking it, people unhappy with the design decision), and not many voice opinions of being in favour of it. Rather than keep trying to solve the symptoms of the issue, we could probably make the majority happy by removing property assertions altogether. |
0c1d280
to
0033b15
Compare
@keithamus I think it's worth having that conversation even if the impact of this proxy thing can be lowered to acceptable levels. Speaking of which, I just updated the PR to exclude |
0033b15
to
9625fe8
Compare
Success! Excluding Edit: Doesn't break chai-as-promised either. :D |
9625fe8
to
1725019
Compare
Feeling pretty good about this. Rebased and removed the WIP tag. I think it's ready to go. |
How do we feel about merging this one, given all of the info we have now? If we merge this in to get it into |
@keithamus Yes it will! I think this post pretty much explains what was going on with the chaining issue. It was a simple problem which went unnoticed until @meeber did the great job of finding it out 😄 This post also explains the whole fix. I'm feeling pretty confident with it since all we needed was to get into a simple |
Just to be clear, chaijs/chai-as-promised#157 fixes a problem that's unrelated to this PR. I just wanted to fix that issue first so I could verify that this PR doesn't break chai-as-promised. Which it doesn't! :D This should be good to go once I rebase... |
1725019
to
d0972d2
Compare
@keithamus @lucasfcosta Rebased and fixed the conflict (was minor). This should be good to go. |
Well, LGTM 😄 |
@meeber makes sense, I have mixed up things since we found out that bug due to this issue. Disclaimer for anyone reading this in the future: This LGTM too, nice work here, detailing why |
Hey guys, I was just playing around with ES6 Proxies to see what it'd look like if assertion chains threw an error if any of the properties were invalid.
For example, this assertion would throw an error:
It seems pretty cool and doesn't break any of Chai's tests, but it'd be a breaking change for some plugins.
For example, you'd no longer be able to test for the existence of properties on an assertion like this code from
chai-as-promised
:Instead, plugin authors would have to guard such checks like so:
Therefore, before rolling out a change like this, we'd want to work with plugin authors to get their code updated.
Also, there's an extra frame in the stack traces for assertion errors involving property assertions (but not for method assertions). Haven't found a way to get rid of it without also getting rid of the meaningful frame. A heavy-handed approach would be to filter out the Proxy frame manually from the stack string.
Anyway, this is a problem we should look to tackle soon. I'd argue that Chai's greatest weakness is the fact that invalid assertions silently pass. Hope this proof-of-concept sparks some ideas!
Edit: Whoops just saw #407, but it looks similar to the approach taken in this PR! :D