[jest/expect-expect] doesn't work with assertion function names with $ character
See original GitHub issueProblem
I have configured jest/expect-expect
the next way:
"jest/expect-expect": ["warn", { "assertFunctionNames": ["expect", "expect$"] }]
But as I explored the rule doesn’t work with the function names which contain special characters from regular expressions, including $
.
As I see from the implementation of matching function provided in assertFunctionNames
patterns are transformed into regular expressions for further matching. Thus, characters as $
are handled as regex special characters, not like part of a provided function name.
Expectation
I expect that assertion function names with special characters are whitelisted and do not trigger eslint errors.
Proposed solution
As a solution, I propose to add an extra step and escape special characters while converting provided function name into regular expression.
function matchesAssertFunctionName(
nodeName: string,
patterns: readonly string[],
): boolean {
return patterns.some(p =>
new RegExp(
`^${p
.split('.')
.replace((/[+?^${}()|[\]\\]/g, '\\$&'))
.map(x => {
if (x === '**') return '[a-z\\.]*';
return x.replace(/\*/gu, '[a-z]*');
})
.join('\\.')}(\\.|$)`,
'ui',
).test(nodeName),
);
}
NOTE: the link to syntax characters list. I’ve excluded *
, .
as they could be used in wildcards syntax of names.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5
Top GitHub Comments
Thank you!
No problem 😃
You’re totally right that it’ll be good to have an example for this - I’ve done so via #627, and also added a test to ensure we support this situation.