no-async-tests.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. 'use strict'
  2. module.exports = {
  3. meta: {
  4. docs: {
  5. description: 'Prevent using async/await in Cypress test cases',
  6. category: 'Possible Errors',
  7. recommended: true,
  8. },
  9. messages: {
  10. unexpected: 'Avoid using async functions with Cypress tests',
  11. },
  12. },
  13. create (context) {
  14. function isTestBlock (callExpressionNode) {
  15. const { type, name } = callExpressionNode.callee
  16. return type === 'Identifier'
  17. && name === 'it' || name === 'test'
  18. }
  19. function isTestAsync (node) {
  20. return node.arguments
  21. && node.arguments.length >= 2
  22. && node.arguments[1].async === true
  23. }
  24. return {
  25. Identifier (node) {
  26. if (node.name === 'cy' || node.name === 'Cypress') {
  27. const ancestors = context.getAncestors()
  28. const asyncTestBlocks = ancestors
  29. .filter((n) => n.type === 'CallExpression')
  30. .filter(isTestBlock)
  31. .filter(isTestAsync)
  32. if (asyncTestBlocks.length >= 1) {
  33. asyncTestBlocks.forEach((node) => {
  34. context.report({ node, messageId: 'unexpected' })
  35. })
  36. }
  37. }
  38. },
  39. }
  40. },
  41. }