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

match toBe(null) and possibly toEqual(null) in addition to toBeNull #152

Closed
julienw opened this issue Mar 22, 2021 · 5 comments · Fixed by #153
Closed

match toBe(null) and possibly toEqual(null) in addition to toBeNull #152

julienw opened this issue Mar 22, 2021 · 5 comments · Fixed by #153
Labels

Comments

@julienw
Copy link
Contributor

julienw commented Mar 22, 2021

In a few rules (eg: prefer-document) toBeNull() will be replaced by toBeInTheDocument() which is nice.

Is it possible to match toBe(null) and toEqual(null) as well?

Thanks!

@benmonro
Copy link
Member

Should be. Care to open a PR?

@julienw
Copy link
Contributor Author

julienw commented Mar 23, 2021

TBH I would love to, but I don't know how to write eslint rules. If you know similar examples I could have a look, but for now I looked at the source code for this rule and I have no idea how to change that...

@benmonro
Copy link
Member

benmonro commented Mar 23, 2021

for sure, it's a great skill to learn and once you do it's actually a ton of fun! The docs on eslint are a good placed to start. A thing to note, the function names are AST selectors (think CSS selectors, but for code). that may seem strange, but consider this code:

const foo = { "bar": function() {} };

then consider that this function can use the shorthand notation like this:

const foo = { "bar"() {} };

and if you have a variable you can do this:

const x = "baz";

const foo = { [`bar ${x}`]() {} }

which could be called like this:

foo["bar baz"]();

an eslint rule uses that pattern (as many of the rules in this plugin do) to declaratively handle various use cases. So for example, if you wanted a rule to match all CallExpressions you would do

{ "CallExpression"(node) { ... } }

if you wanted a rule to match all call expressions that were named 'foo' (as in foo()), you would do this:

{"CallExpression[callee.name=foo]"(node) { ... }}

(that might look familiar if you've ever written a CSS selector like div[role=button], that's because it is. :)
You might be wondering how you can know what all the various types and properties etc, don't worry astexplorer.net has you covered there. It's a great tool and this entire plugin was written using it. It's like a dom-inspector for code, and you can play around with rules in real time (among other things).

@julienw
Copy link
Contributor Author

julienw commented Mar 23, 2021

Thanks for the pointers, I think I managed to do something.

julienw added a commit to julienw/eslint-plugin-jest-dom that referenced this issue Mar 23, 2021
julienw added a commit to julienw/eslint-plugin-jest-dom that referenced this issue Mar 23, 2021
benmonro pushed a commit that referenced this issue Mar 23, 2021
@github-actions
Copy link

🎉 This issue has been resolved in version 3.7.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants