Friday, 17 February 2023

Prevent screen.findByX without an await with eslint no-restricted-syntax?

This medium article shows me how to prevent an await preceding a property:

"no-restricted-syntax": [
  "error",
  {
    "message": "promise.then is unnecessary when using async/await",
    "selector": "AwaitExpression[argument.callee.property.name=\" then\"]"
  }
]

But I want the opposite, I want to restrict these:

expect(screen.findByRole(...))....;
screen.findByRole(...);

but allow these:

expect(await screen.findByRole(...))....;
await screen.findByRole(...);

I tried this under my overrides for test files:

"no-restricted-syntax": [
  "error",
  {
    "message": "Find By queries must be followed by an await",
    "selector": ":not(AwaitExpression[argument.callee.property.name=/findBy.*/])"
  }
]

But now every line is showing that error. I also tried putting * in front, hoping it would allow anything (the *) unless is was followed by my banned syntax expression, but no dice.

How can I make this work?



from Prevent screen.findByX without an await with eslint no-restricted-syntax?

No comments:

Post a Comment