- Upgrades eslint from v8 to v9 - Upgrades all other eslint packages. We will have to do a new full-project lint, as new rules have been added - Upgrades husky from v8 to v9 - Upgrades lint-staged from v14 to v15 - Moves the old .eslintrc.cjs file format to the new eslint.config.js flat file format. Previously, we were very specific regarding which rules are applied to which files. Now that `extends` is no longer a thing, I have to use deepMerge & imports instead. This is rather uncommon and is not a documented pattern - e.g. typescript-eslint docs want us to add the default typescript-eslint rules to the top-level & then disable it in files using the disable-typechecked config. However, I hate this opt-out approach. The way I did it here adds a lot of clarity as to which rules are applied to which files, and is pretty easy to read. Much less black magic ## .eslintignore These files are no longer supported (see https://eslint.org/docs/latest/use/configure/migration-guide#ignoring-files). I moved the entries to the ignores property in the eslint config. => one less file in each package folder!
125 lines
3.0 KiB
JavaScript
125 lines
3.0 KiB
JavaScript
/** @type {import('eslint').Rule.RuleModule} */
|
|
export const rule = {
|
|
meta: {
|
|
type: 'problem',
|
|
docs: {
|
|
description:
|
|
'Disallow non-retryable assertions in Playwright E2E tests unless they are wrapped in an expect.poll() or expect().toPass()',
|
|
category: 'Best Practices',
|
|
recommended: true,
|
|
},
|
|
schema: [],
|
|
},
|
|
create: function (context) {
|
|
const nonRetryableAssertions = [
|
|
'toBe',
|
|
'toBeCloseTo',
|
|
'toBeDefined',
|
|
'toBeFalsy',
|
|
'toBeGreaterThan',
|
|
'toBeGreaterThanOrEqual',
|
|
'toBeInstanceOf',
|
|
'toBeLessThan',
|
|
'toBeLessThanOrEqual',
|
|
'toBeNaN',
|
|
'toBeNull',
|
|
'toBeTruthy',
|
|
'toBeUndefined',
|
|
'toContain',
|
|
'toContainEqual',
|
|
'toEqual',
|
|
'toHaveLength',
|
|
'toHaveProperty',
|
|
'toMatch',
|
|
'toMatchObject',
|
|
'toStrictEqual',
|
|
'toThrow',
|
|
'any',
|
|
'anything',
|
|
'arrayContaining',
|
|
'closeTo',
|
|
'objectContaining',
|
|
'stringContaining',
|
|
'stringMatching',
|
|
]
|
|
|
|
function isNonRetryableAssertion(node) {
|
|
return (
|
|
node.type === 'MemberExpression' &&
|
|
node.property.type === 'Identifier' &&
|
|
nonRetryableAssertions.includes(node.property.name)
|
|
)
|
|
}
|
|
|
|
function isExpectPollOrToPass(node) {
|
|
if (
|
|
node.type === 'MemberExpression' &&
|
|
(node?.property?.name === 'poll' || node?.property?.name === 'toPass')
|
|
) {
|
|
return true
|
|
}
|
|
|
|
return (
|
|
node.type === 'CallExpression' &&
|
|
node.callee.type === 'MemberExpression' &&
|
|
((node.callee.object.type === 'CallExpression' &&
|
|
node.callee.object.callee.type === 'MemberExpression' &&
|
|
node.callee.object.callee.property.name === 'poll') ||
|
|
node.callee.property.name === 'toPass')
|
|
)
|
|
}
|
|
|
|
function hasExpectPollOrToPassInChain(node) {
|
|
let ancestor = node
|
|
|
|
while (ancestor) {
|
|
if (isExpectPollOrToPass(ancestor)) {
|
|
return true
|
|
}
|
|
ancestor = 'object' in ancestor ? ancestor.object : ancestor.callee
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
function hasExpectPollOrToPassInParentChain(node) {
|
|
let ancestor = node
|
|
|
|
while (ancestor) {
|
|
if (isExpectPollOrToPass(ancestor)) {
|
|
return true
|
|
}
|
|
ancestor = ancestor.parent
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
return {
|
|
CallExpression(node) {
|
|
// node.callee is MemberExpressiom
|
|
if (isNonRetryableAssertion(node.callee)) {
|
|
if (hasExpectPollOrToPassInChain(node.callee)) {
|
|
return
|
|
}
|
|
|
|
if (hasExpectPollOrToPassInParentChain(node)) {
|
|
return
|
|
}
|
|
|
|
context.report({
|
|
node: node.callee.property,
|
|
message:
|
|
'Non-retryable, flaky assertion used in Playwright test: "{{ assertion }}". Those need to be wrapped in expect.poll() or expect().toPass().',
|
|
data: {
|
|
assertion: node.callee.property.name,
|
|
},
|
|
})
|
|
}
|
|
},
|
|
}
|
|
},
|
|
}
|
|
|
|
export default rule
|