question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[jest/expect-expect] doesn't work with assertion function names with $ character

See original GitHub issue

Problem

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:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
nsasinovichcommented, Jul 16, 2020

Thank you!

1reaction
G-Rathcommented, Jul 15, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

eslint-plugin-jest/expect-expect.md at main - GitHub
This array option specifies the names of functions that should be considered to be asserting functions. Function names can use wildcards i.e request....
Read more >
Making 'expect()' mandatory for all test cases - Stack Overflow
There are two ways I'm aware of to automatically do this: The ESLint Jest plugin's jest/expect-expect rule, which will ensure that every ...
Read more >
Python's assert: Debug and Test Your Code Like a Pro
In this tutorial, you'll learn how to use Python's assert statement to document, debug, and test code in development.
Read more >
Assertions | Cypress examples (v9.7.0) - Gleb Bahmutov
Sometimes an attribute can have a special character like . or : in it. If you are just checking the attribute name, you...
Read more >
Custom rules and assertions - Redocly
Custom rules and assertions. Configure custom rules and assertions to enforce your API design standards. There are three types of custom ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found