first commit
This commit is contained in:
commit
eb2f504652
32490 changed files with 5731109 additions and 0 deletions
110
node_modules/eslint-plugin-cypress/lib/rules/assertion-before-screenshot.js
generated
vendored
Normal file
110
node_modules/eslint-plugin-cypress/lib/rules/assertion-before-screenshot.js
generated
vendored
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
'use strict'
|
||||
|
||||
const assertionCommands = [
|
||||
// assertions
|
||||
'should',
|
||||
'and',
|
||||
'contains',
|
||||
|
||||
// retries until it gets something
|
||||
'get',
|
||||
|
||||
// not an assertion, but unlikely to require waiting for render
|
||||
'scrollIntoView',
|
||||
'scrollTo',
|
||||
]
|
||||
|
||||
module.exports = {
|
||||
meta: {
|
||||
docs: {
|
||||
description: 'Assert on the page state before taking a screenshot, so the screenshot is consistent',
|
||||
category: 'Possible Errors',
|
||||
recommended: false,
|
||||
},
|
||||
schema: [],
|
||||
messages: {
|
||||
unexpected: 'Make an assertion on the page state before taking a screenshot',
|
||||
},
|
||||
},
|
||||
create (context) {
|
||||
return {
|
||||
CallExpression (node) {
|
||||
if (isCallingCyScreenshot(node) && !isPreviousAnAssertion(node)) {
|
||||
context.report({ node, messageId: 'unexpected' })
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
function isRootCypress (node) {
|
||||
while (node.type === 'CallExpression') {
|
||||
if (node.callee.type !== 'MemberExpression') return false
|
||||
|
||||
if (node.callee.object.type === 'Identifier' &&
|
||||
node.callee.object.name === 'cy') {
|
||||
return true
|
||||
}
|
||||
|
||||
node = node.callee.object
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
function getPreviousInChain (node) {
|
||||
return node.type === 'CallExpression' &&
|
||||
node.callee.type === 'MemberExpression' &&
|
||||
node.callee.object.type === 'CallExpression' &&
|
||||
node.callee.object.callee.type === 'MemberExpression' &&
|
||||
node.callee.object.callee.property.type === 'Identifier' &&
|
||||
node.callee.object.callee.property.name
|
||||
}
|
||||
|
||||
function getCallExpressionCypressCommand (node) {
|
||||
return isRootCypress(node) &&
|
||||
node.callee.property.type === 'Identifier' &&
|
||||
node.callee.property.name
|
||||
}
|
||||
|
||||
function isCallingCyScreenshot (node) {
|
||||
return getCallExpressionCypressCommand(node) === 'screenshot'
|
||||
}
|
||||
|
||||
function getPreviousCypressCommand (node) {
|
||||
const previousInChain = getPreviousInChain(node)
|
||||
|
||||
if (previousInChain) {
|
||||
return previousInChain
|
||||
}
|
||||
|
||||
while (node.parent && !node.parent.body) {
|
||||
node = node.parent
|
||||
}
|
||||
|
||||
if (!node.parent || !node.parent.body) return null
|
||||
|
||||
const body = node.parent.body.type === 'BlockStatement' ? node.parent.body.body : node.parent.body
|
||||
|
||||
const index = body.indexOf(node)
|
||||
|
||||
// in the case of a function declaration it won't be found
|
||||
if (index < 0) return null
|
||||
|
||||
if (index === 0) return getPreviousCypressCommand(node.parent)
|
||||
|
||||
const previousStatement = body[index - 1]
|
||||
|
||||
if (previousStatement.type !== 'ExpressionStatement' ||
|
||||
previousStatement.expression.type !== 'CallExpression') {
|
||||
return null
|
||||
}
|
||||
|
||||
return getCallExpressionCypressCommand(previousStatement.expression)
|
||||
}
|
||||
|
||||
function isPreviousAnAssertion (node) {
|
||||
const previousCypressCommand = getPreviousCypressCommand(node)
|
||||
|
||||
return assertionCommands.indexOf(previousCypressCommand) >= 0
|
||||
}
|
||||
67
node_modules/eslint-plugin-cypress/lib/rules/no-assigning-return-values.js
generated
vendored
Normal file
67
node_modules/eslint-plugin-cypress/lib/rules/no-assigning-return-values.js
generated
vendored
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
'use strict'
|
||||
|
||||
// safely get nested object property
|
||||
function get (obj, propertyString = '') {
|
||||
const properties = propertyString.split('.')
|
||||
|
||||
for (let i = 0; i < properties.length; i++) {
|
||||
const value = (obj || {})[properties[i]]
|
||||
|
||||
if (value == null) return value
|
||||
|
||||
obj = value
|
||||
}
|
||||
|
||||
return obj
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
meta: {
|
||||
docs: {
|
||||
description: 'Prevent assigning return values of cy calls',
|
||||
category: 'Possible Errors',
|
||||
recommended: true,
|
||||
url: 'https://on.cypress.io/best-practices#Assigning-Return-Values',
|
||||
},
|
||||
schema: [],
|
||||
messages: {
|
||||
unexpected: 'Do not assign the return value of a Cypress command',
|
||||
},
|
||||
},
|
||||
create (context) {
|
||||
return {
|
||||
VariableDeclaration (node) {
|
||||
if (node.declarations.some(isCypressCommandDeclaration)) {
|
||||
context.report({ node, messageId: 'unexpected' })
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
const allowedCommands = {
|
||||
now: true,
|
||||
spy: true,
|
||||
state: true,
|
||||
stub: true,
|
||||
}
|
||||
|
||||
function isCypressCommandDeclaration (declarator) {
|
||||
let object = get(declarator, 'init.callee.object')
|
||||
|
||||
if (!object) return
|
||||
|
||||
while (object.callee) {
|
||||
object = object.callee.object
|
||||
|
||||
if (!object) return
|
||||
}
|
||||
|
||||
const commandName = get(declarator, 'init.callee.property.name')
|
||||
|
||||
const parent = get(object, 'parent.property.name') || get(declarator, 'id.name')
|
||||
|
||||
if (commandName && (allowedCommands[commandName] || allowedCommands[parent])) return
|
||||
|
||||
return object.name === 'cy'
|
||||
}
|
||||
47
node_modules/eslint-plugin-cypress/lib/rules/no-async-tests.js
generated
vendored
Normal file
47
node_modules/eslint-plugin-cypress/lib/rules/no-async-tests.js
generated
vendored
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
'use strict'
|
||||
|
||||
module.exports = {
|
||||
meta: {
|
||||
docs: {
|
||||
description: 'Prevent using async/await in Cypress test cases',
|
||||
category: 'Possible Errors',
|
||||
recommended: true,
|
||||
},
|
||||
messages: {
|
||||
unexpected: 'Avoid using async functions with Cypress tests',
|
||||
},
|
||||
},
|
||||
|
||||
create (context) {
|
||||
function isTestBlock (callExpressionNode) {
|
||||
const { type, name } = callExpressionNode.callee
|
||||
|
||||
return type === 'Identifier'
|
||||
&& name === 'it' || name === 'test'
|
||||
}
|
||||
|
||||
function isTestAsync (node) {
|
||||
return node.arguments
|
||||
&& node.arguments.length >= 2
|
||||
&& node.arguments[1].async === true
|
||||
}
|
||||
|
||||
return {
|
||||
Identifier (node) {
|
||||
if (node.name === 'cy' || node.name === 'Cypress') {
|
||||
const ancestors = context.getAncestors()
|
||||
const asyncTestBlocks = ancestors
|
||||
.filter((n) => n.type === 'CallExpression')
|
||||
.filter(isTestBlock)
|
||||
.filter(isTestAsync)
|
||||
|
||||
if (asyncTestBlocks.length >= 1) {
|
||||
asyncTestBlocks.forEach((node) => {
|
||||
context.report({ node, messageId: 'unexpected' })
|
||||
})
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
78
node_modules/eslint-plugin-cypress/lib/rules/no-force.js
generated
vendored
Normal file
78
node_modules/eslint-plugin-cypress/lib/rules/no-force.js
generated
vendored
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
'use strict'
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Rule Definition
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
module.exports = {
|
||||
meta: {
|
||||
docs: {
|
||||
description: 'Disallow using of \'force: true\' option for click and type calls',
|
||||
category: 'Possible Errors',
|
||||
recommended: false,
|
||||
},
|
||||
fixable: null, // or "code" or "whitespace"
|
||||
schema: [],
|
||||
messages: {
|
||||
unexpected: 'Do not use force on click and type calls',
|
||||
},
|
||||
},
|
||||
|
||||
create (context) {
|
||||
|
||||
// variables should be defined here
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
// Helpers
|
||||
//----------------------------------------------------------------------
|
||||
function isCallingClickOrType (node) {
|
||||
const allowedMethods = ['click', 'dblclick', 'type', 'trigger', 'check', 'rightclick', 'focus', 'select']
|
||||
|
||||
return node.property && node.property.type === 'Identifier' &&
|
||||
allowedMethods.includes(node.property.name)
|
||||
}
|
||||
|
||||
function isCypressCall (node) {
|
||||
return node.callee.type === 'MemberExpression' &&
|
||||
node.callee.object.type === 'Identifier' &&
|
||||
node.callee.object.name === 'cy'
|
||||
}
|
||||
|
||||
function hasOptionForce (node) {
|
||||
|
||||
return node.arguments && node.arguments.length &&
|
||||
node.arguments.some((arg) => {
|
||||
return arg.type === 'ObjectExpression' && arg.properties.some((propNode) => propNode.key && propNode.key.name === 'force')
|
||||
})
|
||||
}
|
||||
|
||||
function deepCheck (node, checkFunc) {
|
||||
let currentNode = node
|
||||
|
||||
while (currentNode.parent) {
|
||||
|
||||
if (checkFunc(currentNode.parent)) {
|
||||
return true
|
||||
}
|
||||
|
||||
currentNode = currentNode.parent
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
// Public
|
||||
//----------------------------------------------------------------------
|
||||
|
||||
return {
|
||||
|
||||
CallExpression (node) {
|
||||
if (isCypressCall(node) && deepCheck(node, isCallingClickOrType) && deepCheck(node, hasOptionForce)) {
|
||||
context.report({ node, messageId: 'unexpected' })
|
||||
}
|
||||
},
|
||||
|
||||
}
|
||||
},
|
||||
}
|
||||
56
node_modules/eslint-plugin-cypress/lib/rules/no-pause.js
generated
vendored
Normal file
56
node_modules/eslint-plugin-cypress/lib/rules/no-pause.js
generated
vendored
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
'use strict'
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Rule Definition
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
module.exports = {
|
||||
meta: {
|
||||
docs: {
|
||||
description: 'Disallow using of \'cy.pause\' calls',
|
||||
category: 'Possible Errors',
|
||||
recommended: false,
|
||||
},
|
||||
fixable: null, // or "code" or "whitespace"
|
||||
schema: [],
|
||||
messages: {
|
||||
unexpected: 'Do not use cy.pause command',
|
||||
},
|
||||
},
|
||||
|
||||
create (context) {
|
||||
|
||||
// variables should be defined here
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
// Helpers
|
||||
//----------------------------------------------------------------------
|
||||
function isCallingPause (node) {
|
||||
return node.callee &&
|
||||
node.callee.property &&
|
||||
node.callee.property.type === 'Identifier' &&
|
||||
node.callee.property.name === 'pause'
|
||||
}
|
||||
|
||||
function isCypressCall (node) {
|
||||
return node.callee &&
|
||||
node.callee.type === 'MemberExpression' &&
|
||||
node.callee.object.type === 'Identifier' &&
|
||||
node.callee.object.name === 'cy'
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
// Public
|
||||
//----------------------------------------------------------------------
|
||||
|
||||
return {
|
||||
|
||||
CallExpression (node) {
|
||||
if (isCypressCall(node) && isCallingPause(node)) {
|
||||
context.report({ node, messageId: 'unexpected' })
|
||||
}
|
||||
},
|
||||
|
||||
}
|
||||
},
|
||||
}
|
||||
86
node_modules/eslint-plugin-cypress/lib/rules/no-unnecessary-waiting.js
generated
vendored
Normal file
86
node_modules/eslint-plugin-cypress/lib/rules/no-unnecessary-waiting.js
generated
vendored
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
'use strict'
|
||||
|
||||
module.exports = {
|
||||
meta: {
|
||||
docs: {
|
||||
description: 'Prevent waiting for arbitrary time periods',
|
||||
category: 'Possible Errors',
|
||||
recommended: true,
|
||||
url: 'https://on.cypress.io/best-practices#Unnecessary-Waiting',
|
||||
},
|
||||
schema: [],
|
||||
messages: {
|
||||
unexpected: 'Do not wait for arbitrary time periods',
|
||||
},
|
||||
},
|
||||
create (context) {
|
||||
return {
|
||||
CallExpression (node) {
|
||||
if (isCallingCyWait(node)) {
|
||||
const scope = context.getScope()
|
||||
|
||||
if (isIdentifierNumberConstArgument(node, scope) || isNumberArgument(node)) {
|
||||
context.report({ node, messageId: 'unexpected' })
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
function nodeIsCalledByCy (node) {
|
||||
if (node.type === 'Identifier' && node.name === 'cy') return true
|
||||
|
||||
if (typeof node.callee === 'undefined' || typeof node.callee.object === 'undefined') {
|
||||
return false
|
||||
}
|
||||
|
||||
return nodeIsCalledByCy(node.callee.object)
|
||||
}
|
||||
|
||||
function isCallingCyWait (node) {
|
||||
return node.callee.type === 'MemberExpression' &&
|
||||
nodeIsCalledByCy(node) &&
|
||||
node.callee.property.type === 'Identifier' &&
|
||||
node.callee.property.name === 'wait'
|
||||
}
|
||||
|
||||
function isNumberArgument (node) {
|
||||
return node.arguments.length > 0 &&
|
||||
node.arguments[0].type === 'Literal' &&
|
||||
typeof (node.arguments[0].value) === 'number'
|
||||
}
|
||||
|
||||
function isIdentifierNumberConstArgument (node, scope) {
|
||||
if (node.arguments.length === 0) return false
|
||||
|
||||
if (node.arguments[0].type !== 'Identifier') return false
|
||||
|
||||
const identifier = node.arguments[0]
|
||||
const resolvedIdentifier = scope.references.find((ref) => ref.identifier === identifier).resolved
|
||||
const definition = resolvedIdentifier.defs[0]
|
||||
const isVariable = definition.type === 'Variable'
|
||||
|
||||
// const amount = 1000 or const amount = '@alias'
|
||||
// cy.wait(amount)
|
||||
if (isVariable) {
|
||||
if (!definition.node.init) return false
|
||||
|
||||
return typeof definition.node.init.value === 'number'
|
||||
}
|
||||
|
||||
// import { WAIT } from './constants'
|
||||
// cy.wait(WAIT)
|
||||
// we don't know if WAIT is a number or alias '@someRequest', so don't fail
|
||||
if (definition.type === 'ImportBinding') return false
|
||||
|
||||
const param = definition.node.params[definition.index]
|
||||
|
||||
// function wait (amount) { cy.wait(amount) }
|
||||
// we can't know the type of value, so don't fail
|
||||
if (!param || param.type !== 'AssignmentPattern') return false
|
||||
|
||||
// function wait (amount = 1) { cy.wait(amount) } or
|
||||
// function wait (amount = '@alias') { cy.wait(amount) }
|
||||
return typeof param.right.value === 'number'
|
||||
}
|
||||
48
node_modules/eslint-plugin-cypress/lib/rules/require-data-selectors.js
generated
vendored
Normal file
48
node_modules/eslint-plugin-cypress/lib/rules/require-data-selectors.js
generated
vendored
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
'use strict'
|
||||
|
||||
module.exports = {
|
||||
meta: {
|
||||
docs: {
|
||||
description: 'Use data-* attributes to provide context to your selectors and insulate them from CSS or JS changes https://docs.cypress.io/guides/references/best-practices.html#Selecting-Elements',
|
||||
category: 'Possible Errors',
|
||||
recommended: false,
|
||||
url: 'https://docs.cypress.io/guides/references/best-practices.html#Selecting-Elements',
|
||||
},
|
||||
schema: [],
|
||||
messages: {
|
||||
unexpected: 'use data-* attribute selectors instead of classes or tag names',
|
||||
},
|
||||
},
|
||||
|
||||
create (context) {
|
||||
return {
|
||||
CallExpression (node) {
|
||||
if (isCallingCyGet(node) && !isDataArgument(node)) {
|
||||
context.report({ node, messageId: 'unexpected' })
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
function isCallingCyGet (node) {
|
||||
return node.callee.type === 'MemberExpression' &&
|
||||
node.callee.object.type === 'Identifier' &&
|
||||
node.callee.object.name === 'cy' &&
|
||||
node.callee.property.type === 'Identifier' &&
|
||||
node.callee.property.name === 'get'
|
||||
}
|
||||
|
||||
function isDataArgument (node) {
|
||||
return node.arguments.length > 0 &&
|
||||
(
|
||||
(node.arguments[0].type === 'Literal' && isAliasOrDataSelector(String(node.arguments[0].value))) ||
|
||||
(node.arguments[0].type === 'TemplateLiteral' && isAliasOrDataSelector(String(node.arguments[0].quasis[0].value.cooked)))
|
||||
)
|
||||
}
|
||||
|
||||
function isAliasOrDataSelector (selector) {
|
||||
return ['[data-', '@'].some(function (validValue) {
|
||||
return selector.startsWith(validValue)
|
||||
})
|
||||
}
|
||||
142
node_modules/eslint-plugin-cypress/lib/rules/unsafe-to-chain-command.js
generated
vendored
Normal file
142
node_modules/eslint-plugin-cypress/lib/rules/unsafe-to-chain-command.js
generated
vendored
Normal file
|
|
@ -0,0 +1,142 @@
|
|||
'use strict'
|
||||
|
||||
const { basename } = require('path')
|
||||
|
||||
const NAME = basename(__dirname)
|
||||
const DESCRIPTION = 'Actions should be in the end of chains, not in the middle'
|
||||
|
||||
/**
|
||||
* Commands listed in the documentation with text: 'It is unsafe to chain further commands that rely on the subject after xxx.'
|
||||
* See {@link https://docs.cypress.io/guides/core-concepts/retry-ability#Actions-should-be-at-the-end-of-chains-not-the-middle Actions should be at the end of chains, not the middle}
|
||||
* for more information.
|
||||
*
|
||||
* @type {string[]}
|
||||
*/
|
||||
const unsafeToChainActions = [
|
||||
'blur',
|
||||
'clear',
|
||||
'click',
|
||||
'check',
|
||||
'dblclick',
|
||||
'each',
|
||||
'focus',
|
||||
'rightclick',
|
||||
'screenshot',
|
||||
'scrollIntoView',
|
||||
'scrollTo',
|
||||
'select',
|
||||
'selectFile',
|
||||
'spread',
|
||||
'submit',
|
||||
'type',
|
||||
'trigger',
|
||||
'uncheck',
|
||||
'within',
|
||||
]
|
||||
|
||||
/**
|
||||
* @type {import('eslint').Rule.RuleMetaData['schema']}
|
||||
*/
|
||||
const schema = {
|
||||
title: NAME,
|
||||
description: DESCRIPTION,
|
||||
type: 'object',
|
||||
properties: {
|
||||
methods: {
|
||||
type: 'array',
|
||||
description:
|
||||
'An additional list of methods to check for unsafe chaining.',
|
||||
default: [],
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {import('eslint').Rule.RuleContext} context
|
||||
* @returns {Record<string, any>}
|
||||
*/
|
||||
const getDefaultOptions = (context) => {
|
||||
return Object.entries(schema.properties).reduce((acc, [key, value]) => {
|
||||
if (!(value.default in value)) return acc
|
||||
|
||||
return {
|
||||
...acc,
|
||||
[key]: value.default,
|
||||
}
|
||||
}, context.options[0] || {})
|
||||
}
|
||||
|
||||
/** @type {import('eslint').Rule.RuleModule} */
|
||||
module.exports = {
|
||||
meta: {
|
||||
docs: {
|
||||
description: DESCRIPTION,
|
||||
category: 'Possible Errors',
|
||||
recommended: true,
|
||||
url: 'https://docs.cypress.io/guides/core-concepts/retry-ability#Actions-should-be-at-the-end-of-chains-not-the-middle',
|
||||
},
|
||||
schema: [schema],
|
||||
messages: {
|
||||
unexpected:
|
||||
'It is unsafe to chain further commands that rely on the subject after this command. It is best to split the chain, chaining again from `cy.` in a next command line.',
|
||||
},
|
||||
},
|
||||
create (context) {
|
||||
const { methods } = getDefaultOptions(context)
|
||||
|
||||
return {
|
||||
CallExpression (node) {
|
||||
if (
|
||||
isRootCypress(node) &&
|
||||
isActionUnsafeToChain(node, methods) &&
|
||||
node.parent.type === 'MemberExpression'
|
||||
) {
|
||||
context.report({
|
||||
node,
|
||||
messageId: 'unexpected',
|
||||
})
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {import('estree').Node} node
|
||||
* @returns {boolean}
|
||||
*/
|
||||
const isRootCypress = (node) => {
|
||||
if (
|
||||
node.type !== 'CallExpression' ||
|
||||
node.callee.type !== 'MemberExpression'
|
||||
) {
|
||||
return false
|
||||
}
|
||||
|
||||
if (
|
||||
node.callee.object.type === 'Identifier' &&
|
||||
node.callee.object.name === 'cy'
|
||||
) {
|
||||
return true
|
||||
}
|
||||
|
||||
return isRootCypress(node.callee.object)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {import('estree').Node} node
|
||||
* @param {(string | RegExp)[]} additionalMethods
|
||||
*/
|
||||
const isActionUnsafeToChain = (node, additionalMethods = []) => {
|
||||
const unsafeActionsRegex = new RegExp([
|
||||
...unsafeToChainActions,
|
||||
...additionalMethods.map((method) => method instanceof RegExp ? method.source : method),
|
||||
].join('|'))
|
||||
|
||||
return (
|
||||
node.callee &&
|
||||
node.callee.property &&
|
||||
node.callee.property.type === 'Identifier' &&
|
||||
unsafeActionsRegex.test(node.callee.property.name)
|
||||
)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue