Skip to content

Commit eff7d81

Browse files
committed
tests: create tests-legacy
1 parent c40989d commit eff7d81

File tree

10 files changed

+393
-0
lines changed

10 files changed

+393
-0
lines changed

tests-legacy/config.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* global describe, it, expect */
2+
'use strict'
3+
4+
const globals = require('globals')
5+
const config = require('../index.js')
6+
7+
describe('environments globals', () => {
8+
const env = config.environments.globals
9+
10+
it('should not mutate globals', () => {
11+
expect(globals.browser).not.toHaveProperty('cy')
12+
expect(globals.mocha).not.toHaveProperty('cy')
13+
})
14+
15+
it('should include other globals', () => {
16+
expect(env.globals).toEqual(expect.objectContaining(globals.browser))
17+
expect(env.globals).toEqual(expect.objectContaining(globals.mocha))
18+
})
19+
20+
it('should include cypress globals', () => {
21+
expect(env.globals).toEqual(expect.objectContaining({
22+
cy: false,
23+
Cypress: false,
24+
}))
25+
})
26+
})
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict'
2+
3+
const rule = require('../../../lib/rules/assertion-before-screenshot')
4+
const RuleTester = require('eslint').RuleTester
5+
6+
const ruleTester = new RuleTester()
7+
8+
const errors = [{ messageId: 'unexpected' }]
9+
const parserOptions = { ecmaVersion: 6 }
10+
11+
ruleTester.run('assertion-before-screenshot', rule, {
12+
valid: [
13+
{ code: 'cy.get(".some-element"); cy.screenshot();', parserOptions },
14+
{ code: 'cy.get(".some-element").should("exist").screenshot();', parserOptions },
15+
{ code: 'cy.get(".some-element").should("exist").screenshot().click()', parserOptions, errors },
16+
{ code: 'cy.get(".some-element").should("exist"); if(true) cy.screenshot();', parserOptions },
17+
{ code: 'if(true) { cy.get(".some-element").should("exist"); cy.screenshot(); }', parserOptions },
18+
{ code: 'cy.get(".some-element").should("exist"); if(true) { cy.screenshot(); }', parserOptions },
19+
{ code: 'const a = () => { cy.get(".some-element").should("exist"); cy.screenshot(); }', parserOptions, errors },
20+
{ code: 'cy.get(".some-element").should("exist").and("be.visible"); cy.screenshot();', parserOptions },
21+
{ code: 'cy.get(".some-element").contains("Text"); cy.screenshot();', parserOptions },
22+
],
23+
24+
invalid: [
25+
{ code: 'cy.screenshot()', parserOptions, errors },
26+
{ code: 'cy.visit("somepage"); cy.screenshot();', parserOptions, errors },
27+
{ code: 'cy.custom(); cy.screenshot()', parserOptions, errors },
28+
{ code: 'cy.get(".some-element").click(); cy.screenshot()', parserOptions, errors },
29+
{ code: 'cy.get(".some-element").click().screenshot()', parserOptions, errors },
30+
{ code: 'if(true) { cy.get(".some-element").click(); cy.screenshot(); }', parserOptions, errors },
31+
{ code: 'cy.get(".some-element").click(); if(true) { cy.screenshot(); }', parserOptions, errors },
32+
{ code: 'cy.get(".some-element"); function a() { cy.screenshot(); }', parserOptions, errors },
33+
{ code: 'cy.get(".some-element"); const a = () => { cy.screenshot(); }', parserOptions, errors },
34+
],
35+
})
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict'
2+
3+
const rule = require('../../../lib/rules/no-assigning-return-values')
4+
const RuleTester = require('eslint').RuleTester
5+
6+
const ruleTester = new RuleTester()
7+
8+
const errors = [{ messageId: 'unexpected' }]
9+
const parserOptions = { ecmaVersion: 6 }
10+
11+
ruleTester.run('no-assigning-return-values', rule, {
12+
valid: [
13+
{ code: 'var foo = true;', parserOptions },
14+
{ code: 'let foo = true;', parserOptions },
15+
{ code: 'const foo = true;', parserOptions },
16+
{ code: 'const foo = bar();', parserOptions },
17+
{ code: 'const foo = bar().baz();', parserOptions },
18+
{ code: 'const spy = cy.spy();', parserOptions },
19+
{ code: 'const spy = cy.spy().as();', parserOptions },
20+
{ code: 'const stub = cy.stub();', parserOptions },
21+
{ code: 'const result = cy.now();', parserOptions },
22+
{ code: 'const state = cy.state();', parserOptions },
23+
{ code: 'cy.get("foo");', parserOptions },
24+
{ code: 'cy.contains("foo").click();', parserOptions },
25+
],
26+
27+
invalid: [
28+
{ code: 'let a = cy.get("foo")', parserOptions, errors },
29+
{ code: 'const a = cy.get("foo")', parserOptions, errors },
30+
{ code: 'var a = cy.get("foo")', parserOptions, errors },
31+
32+
{ code: 'let a = cy.contains("foo")', parserOptions, errors },
33+
{ code: 'let a = cy.window()', parserOptions, errors },
34+
{ code: 'let a = cy.wait("@something")', parserOptions, errors },
35+
36+
{ code: 'let a = cy.contains("foo").click()', parserOptions, errors },
37+
],
38+
})
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict'
2+
3+
const rule = require('../../../lib/rules/no-async-before')
4+
const RuleTester = require('eslint').RuleTester
5+
6+
const ruleTester = new RuleTester()
7+
8+
const errors = [{ messageId: 'unexpected' }]
9+
// async functions are an ES2017 feature
10+
const parserOptions = { ecmaVersion: 8 }
11+
12+
ruleTester.run('no-async-before', rule, {
13+
valid: [
14+
{ code: 'before(\'a before case\', () => { cy.get(\'.someClass\'); })', parserOptions },
15+
{ code: 'before(\'a before case\', async () => { await somethingAsync(); })', parserOptions },
16+
{ code: 'async function nonTestFn () { return await somethingAsync(); }', parserOptions },
17+
{ code: 'const nonTestArrowFn = async () => { await somethingAsync(); }', parserOptions },
18+
],
19+
invalid: [
20+
{ code: 'before(\'a test case\', async () => { cy.get(\'.someClass\'); })', parserOptions, errors },
21+
{ code: 'beforeEach(\'a test case\', async () => { cy.get(\'.someClass\'); })', parserOptions, errors },
22+
{ code: 'before(\'a test case\', async function () { cy.get(\'.someClass\'); })', parserOptions, errors },
23+
{ code: 'beforeEach(\'a test case\', async function () { cy.get(\'.someClass\'); })', parserOptions, errors },
24+
],
25+
})
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict'
2+
3+
const rule = require('../../../lib/rules/no-async-tests')
4+
const RuleTester = require('eslint').RuleTester
5+
6+
const ruleTester = new RuleTester()
7+
8+
const errors = [{ messageId: 'unexpected' }]
9+
// async functions are an ES2017 feature
10+
const parserOptions = { ecmaVersion: 8 }
11+
12+
ruleTester.run('no-async-tests', rule, {
13+
valid: [
14+
{ code: 'it(\'a test case\', () => { cy.get(\'.someClass\'); })', parserOptions },
15+
{ code: 'it(\'a test case\', async () => { await somethingAsync(); })', parserOptions },
16+
{ code: 'async function nonTestFn () { return await somethingAsync(); }', parserOptions },
17+
{ code: 'const nonTestArrowFn = async () => { await somethingAsync(); }', parserOptions },
18+
],
19+
invalid: [
20+
{ code: 'it(\'a test case\', async () => { cy.get(\'.someClass\'); })', parserOptions, errors },
21+
{ code: 'test(\'a test case\', async () => { cy.get(\'.someClass\'); })', parserOptions, errors },
22+
{ code: 'it(\'a test case\', async function () { cy.get(\'.someClass\'); })', parserOptions, errors },
23+
{ code: 'test(\'a test case\', async function () { cy.get(\'.someClass\'); })', parserOptions, errors },
24+
],
25+
})

tests-legacy/lib/rules/no-force.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'use strict'
2+
3+
//------------------------------------------------------------------------------
4+
// Requirements
5+
//------------------------------------------------------------------------------
6+
7+
const rule = require('../../../lib/rules/no-force')
8+
9+
const RuleTester = require('eslint').RuleTester
10+
11+
const errors = [{ messageId: 'unexpected' }]
12+
const parserOptions = { ecmaVersion: 2018 }
13+
14+
//------------------------------------------------------------------------------
15+
// Tests
16+
//------------------------------------------------------------------------------
17+
18+
let ruleTester = new RuleTester()
19+
20+
ruleTester.run('no-force', rule, {
21+
22+
valid: [
23+
{ code: `cy.get('button').click()`, parserOptions },
24+
{ code: `cy.get('button').click({multiple: true})`, parserOptions },
25+
{ code: `cy.get('button').dblclick()`, parserOptions },
26+
{ code: `cy.get('input').type('somth')`, parserOptions },
27+
{ code: `cy.get('input').type('somth', {anyoption: true})`, parserOptions },
28+
{ code: `cy.get('input').trigger('click', {anyoption: true})`, parserOptions },
29+
{ code: `cy.get('input').rightclick({anyoption: true})`, parserOptions },
30+
{ code: `cy.get('input').check()`, parserOptions },
31+
{ code: `cy.get('input').select()`, parserOptions },
32+
{ code: `cy.get('input').focus()`, parserOptions },
33+
{ code: `cy.document().trigger("keydown", { ...event })`, parserOptions },
34+
],
35+
36+
invalid: [
37+
{ code: `cy.get('button').click({force: true})`, parserOptions, errors },
38+
{ code: `cy.get('button').dblclick({force: true})`, parserOptions, errors },
39+
{ code: `cy.get('input').type('somth', {force: true})`, parserOptions, errors },
40+
{ code: `cy.get('div').find('.foo').type('somth', {force: true})`, parserOptions, errors },
41+
{ code: `cy.get('div').find('.foo').find('.bar').click({force: true})`, parserOptions, errors },
42+
{ code: `cy.get('div').find('.foo').find('.bar').trigger('change', {force: true})`, parserOptions, errors },
43+
{ code: `cy.get('input').trigger('click', {force: true})`, parserOptions, errors },
44+
{ code: `cy.get('input').rightclick({force: true})`, parserOptions, errors },
45+
{ code: `cy.get('input').check({force: true})`, parserOptions, errors },
46+
{ code: `cy.get('input').select({force: true})`, parserOptions, errors },
47+
{ code: `cy.get('input').focus({force: true})`, parserOptions, errors },
48+
],
49+
})

tests-legacy/lib/rules/no-pause.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict'
2+
3+
//------------------------------------------------------------------------------
4+
// Requirements
5+
//------------------------------------------------------------------------------
6+
7+
const rule = require('../../../lib/rules/no-pause')
8+
9+
const RuleTester = require('eslint').RuleTester
10+
11+
const errors = [{ messageId: 'unexpected' }]
12+
const parserOptions = { ecmaVersion: 2018 }
13+
14+
//------------------------------------------------------------------------------
15+
// Tests
16+
//------------------------------------------------------------------------------
17+
18+
const ruleTester = new RuleTester()
19+
20+
ruleTester.run('no-pause', rule, {
21+
22+
valid: [
23+
{ code: `pause()`, parserOptions },
24+
{ code: `cy.get('button').dblclick()`, parserOptions },
25+
],
26+
27+
invalid: [
28+
{ code: `cy.pause()`, parserOptions, errors },
29+
{ code: `cy.pause({ log: false })`, parserOptions, errors },
30+
{ code: `cy.get('button').pause()`, parserOptions, errors },
31+
{
32+
code: `cy.get('a').should('have.attr', 'href').and('match', /dashboard/).pause()`,
33+
parserOptions,
34+
errors
35+
}
36+
],
37+
})
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
'use strict'
2+
3+
const rule = require('../../../lib/rules/no-unnecessary-waiting')
4+
const RuleTester = require('eslint').RuleTester
5+
6+
const ruleTester = new RuleTester()
7+
8+
const errors = [{ messageId: 'unexpected' }]
9+
const parserOptions = { ecmaVersion: 6, sourceType: 'module' }
10+
11+
ruleTester.run('no-unnecessary-waiting', rule, {
12+
valid: [
13+
{ code: 'foo.wait(10)', parserOptions },
14+
15+
{ code: 'cy.wait("@someRequest")', parserOptions },
16+
{ code: 'cy.wait("@someRequest", { log: false })', parserOptions },
17+
{ code: 'cy.wait("@someRequest").then((xhr) => xhr)', parserOptions },
18+
{ code: 'cy.wait(["@someRequest", "@anotherRequest"])', parserOptions },
19+
20+
{ code: 'cy.clock(5000)', parserOptions },
21+
{ code: 'cy.scrollTo(0, 10)', parserOptions },
22+
{ code: 'cy.tick(500)', parserOptions },
23+
24+
{ code: 'const someRequest="@someRequest"; cy.wait(someRequest)', parserOptions, errors },
25+
{ code: 'function customWait (alias = "@someRequest") { cy.wait(alias) }', parserOptions, errors },
26+
{ code: 'const customWait = (alias = "@someRequest") => { cy.wait(alias) }', parserOptions, errors },
27+
{ code: 'function customWait (ms) { cy.wait(ms) }', parserOptions, errors },
28+
{ code: 'const customWait = (ms) => { cy.wait(ms) }', parserOptions, errors },
29+
30+
{ code: 'import BAR_BAZ from "bar-baz"; cy.wait(BAR_BAZ)', parserOptions },
31+
{ code: 'import { FOO_BAR } from "foo-bar"; cy.wait(FOO_BAR)', parserOptions },
32+
{ code: 'import * as wildcard from "wildcard"; cy.wait(wildcard.value)', parserOptions },
33+
{ code: 'import { NAME as OTHER_NAME } from "rename"; cy.wait(OTHER_NAME)', parserOptions },
34+
35+
// disable the eslint rule
36+
{
37+
code: `
38+
cy.wait(100); // eslint-disable-line no-unnecessary-waiting
39+
`,
40+
parserOptions,
41+
},
42+
{
43+
code: `
44+
/* eslint-disable-next-line no-unnecessary-waiting */
45+
cy.wait(100)
46+
`,
47+
parserOptions,
48+
},
49+
{
50+
code: `
51+
/* eslint-disable no-unnecessary-waiting */
52+
cy.wait(100)
53+
/* eslint-enable no-unnecessary-waiting */
54+
`,
55+
parserOptions,
56+
},
57+
],
58+
59+
invalid: [
60+
{ code: 'cy.wait(0)', parserOptions, errors },
61+
{ code: 'cy.wait(100)', parserOptions, errors },
62+
{ code: 'cy.wait(5000)', parserOptions, errors },
63+
{ code: 'const someNumber=500; cy.wait(someNumber)', parserOptions, errors },
64+
{ code: 'function customWait (ms = 1) { cy.wait(ms) }', parserOptions, errors },
65+
{ code: 'const customWait = (ms = 1) => { cy.wait(ms) }', parserOptions, errors },
66+
67+
{ code: 'cy.get(".some-element").wait(10)', parserOptions, errors },
68+
{ code: 'cy.get(".some-element").contains("foo").wait(10)', parserOptions, errors },
69+
{ code: 'const customWait = (ms = 1) => { cy.get(".some-element").wait(ms) }', parserOptions, errors },
70+
],
71+
})
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict'
2+
3+
const rule = require('../../../lib/rules/require-data-selectors')
4+
const RuleTester = require('eslint').RuleTester
5+
6+
const ruleTester = new RuleTester()
7+
8+
const errors = [{ messageId: 'unexpected' }]
9+
const parserOptions = { ecmaVersion: 6 }
10+
11+
ruleTester.run('require-data-selectors', rule, {
12+
valid: [
13+
{ code: 'cy.get(\'[data-cy=submit]\').click()', parserOptions },
14+
{ code: 'cy.get(\'[data-QA=submit]\')', parserOptions },
15+
{ code: 'cy.clock(5000)', parserOptions },
16+
{ code: 'cy.scrollTo(0, 10)', parserOptions },
17+
{ code: 'cy.tick(500)', parserOptions },
18+
{ code: 'cy.get(\`[data-cy=${1}]\`)', parserOptions }, // eslint-disable-line no-useless-escape
19+
{ code: 'cy.get("@my-alias")', parserOptions, errors },
20+
{ code: 'cy.get(`@my-alias`)', parserOptions, errors },
21+
],
22+
23+
invalid: [
24+
{ code: 'cy.get(\'[daedta-cy=submit]\').click()', parserOptions, errors },
25+
{ code: 'cy.get(\'[d-cy=submit]\')', parserOptions, errors },
26+
{ code: 'cy.get(".btn-large").click()', parserOptions, errors },
27+
{ code: 'cy.get(".btn-.large").click()', parserOptions, errors },
28+
{ code: 'cy.get(".a")', parserOptions, errors },
29+
{ code: 'cy.get(\`[daedta-cy=${1}]\`)', parserOptions, errors }, // eslint-disable-line no-useless-escape
30+
],
31+
})

0 commit comments

Comments
 (0)