cypress: add a custom ESLint rule against cy.get().contains() usage.

Since cypress retries only the last command, it's better to use
the compact cy.contains(selector, content) version, instead of
cy.get(selector).contains(content) call.

Change-Id: Ie1f4c17bbf736058ecf6bd996b46384fdff19446
Reviewed-on: https://gerrit.libreoffice.org/c/online/+/93081
Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoffice@gmail.com>
Reviewed-by: Tamás Zolnai <tamas.zolnai@collabora.com>
distro/collabora/co-4-2-3
Tamás Zolnai 2020-04-28 16:00:15 +02:00
parent d006204478
commit af76b48460
5 changed files with 42 additions and 1 deletions

View File

@ -1,5 +1,9 @@
{
"extends": "../loleaflet/.eslintrc",
"plugins": ["cypress-rules"],
"rules": {
"cypress-rules/no-get-contains-chain": 2
},
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module",

View File

@ -0,0 +1,31 @@
module.exports = {
rules: {
'no-get-contains-chain': {
/**
* Catches cy.get(selector).contains(content) calls in the code.
*
* Issue: In cypress test framework only the last command is retried.
* In this case it's the contains method and so the get() method is
* not retried. Sometimes, it behaves unexpectedly, because the DOM
* has the correct item matching with both the selector and the content,
* but the test still fails on this command chain.
*
* Fix 1: When we use the content as a selector. In this case, we can use
* cy.contains(selector, content) instead. This is compact command which
* will retry to match both the selector and the content.
*
* Fix 2: When we need an assertion about the content. In this case, we can
* use cy.contains(selector, content) or cy.get(selector).should('have.text', content)
* is also a good replacement.
*
**/
create: function(context) {
return {
'CallExpression[callee.property.name=\'contains\'][callee.object.callee.property.name=\'get\']': function(node) {
context.report(node, 'Do not chain get() and contains(). Use cy.contains(selector, content) instead for better retriability!');
}
};
}
}
}
};

View File

@ -0,0 +1,5 @@
{
"name": "eslint-plugin-cypress-rules",
"version": "1.0.0",
"main": "index.js"
}

View File

@ -64,7 +64,7 @@ describe('Calc insertion wizard.', function() {
calcHelper.selectAllMobile();
cy.get('#copy-paste-container table td a')
.contains('some text');
.should('have.text', 'some text');
cy.get('#copy-paste-container table td a')
.should('have.attr', 'href', 'http://www.something.com');

View File

@ -7,6 +7,7 @@
"cypress": "4.3.0",
"cypress-failed-log": "2.6.2",
"eslint": "6.8.0",
"eslint-plugin-cypress-rules": "file:eslint_plugin",
"get-port-cli": "2.0.0",
"wait-on": "4.0.0"
},