Skip to content

Commit

Permalink
feat: update example tests for compatibility with latest recommended …
Browse files Browse the repository at this point in the history
…mocha lint (#633)

* fix: ignore mocha/no-mocha-arrows linting rule

* fix: merge sibling beforeeach in files example

* fix: add top level context to cypress_api example

* chore(deps): update eslint-plugin-mocha to 10.1.0
  • Loading branch information
MikeMcC399 committed Apr 20, 2023
1 parent 666a5e7 commit 1cb6bed
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 147 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Expand Up @@ -11,6 +11,7 @@
},
"rules": {
"indent": ["error", 2, { "SwitchCase": 1, "MemberExpression": "off" }],
"mocha/no-exclusive-tests": "error"
"mocha/no-exclusive-tests": "error",
"mocha/no-mocha-arrows": "off"
}
}
251 changes: 127 additions & 124 deletions cypress/e2e/2-advanced-examples/cypress_api.cy.js
@@ -1,184 +1,187 @@
/// <reference types="cypress" />

context('Cypress.Commands', () => {
beforeEach(() => {
cy.visit('http://localhost:8080/cypress-api')
})
context('Cypress APIs', () => {

context('Cypress.Commands', () => {
beforeEach(() => {
cy.visit('http://localhost:8080/cypress-api')
})

// https://on.cypress.io/custom-commands
// https://on.cypress.io/custom-commands

it('.add() - create a custom command', () => {
Cypress.Commands.add('console', {
prevSubject: true,
}, (subject, method) => {
it('.add() - create a custom command', () => {
Cypress.Commands.add('console', {
prevSubject: true,
}, (subject, method) => {
// the previous subject is automatically received
// and the commands arguments are shifted

// allow us to change the console method used
method = method || 'log'
// allow us to change the console method used
method = method || 'log'

// log the subject to the console
// eslint-disable-next-line no-console
console[method]('The subject is', subject)
// log the subject to the console
// eslint-disable-next-line no-console
console[method]('The subject is', subject)

// whatever we return becomes the new subject
// we don't want to change the subject so
// we return whatever was passed in
return subject
})
// whatever we return becomes the new subject
// we don't want to change the subject so
// we return whatever was passed in
return subject
})

// eslint-disable-next-line no-unused-vars
cy.get('button').console('info').then(($button) => {
// eslint-disable-next-line no-unused-vars
cy.get('button').console('info').then(($button) => {
// subject is still $button
})
})
})
})

context('Cypress.Cookies', () => {
beforeEach(() => {
cy.visit('http://localhost:8080/cypress-api')
})
context('Cypress.Cookies', () => {
beforeEach(() => {
cy.visit('http://localhost:8080/cypress-api')
})

// https://on.cypress.io/cookies
it('.debug() - enable or disable debugging', () => {
Cypress.Cookies.debug(true)

// Cypress will now log in the console when
// cookies are set or cleared
cy.setCookie('fakeCookie', '123ABC')
cy.clearCookie('fakeCookie')
cy.setCookie('fakeCookie', '123ABC')
cy.clearCookie('fakeCookie')
cy.setCookie('fakeCookie', '123ABC')
// https://on.cypress.io/cookies
it('.debug() - enable or disable debugging', () => {
Cypress.Cookies.debug(true)

// Cypress will now log in the console when
// cookies are set or cleared
cy.setCookie('fakeCookie', '123ABC')
cy.clearCookie('fakeCookie')
cy.setCookie('fakeCookie', '123ABC')
cy.clearCookie('fakeCookie')
cy.setCookie('fakeCookie', '123ABC')
})
})
})

context('Cypress.arch', () => {
beforeEach(() => {
cy.visit('http://localhost:8080/cypress-api')
})
context('Cypress.arch', () => {
beforeEach(() => {
cy.visit('http://localhost:8080/cypress-api')
})

it('Get CPU architecture name of underlying OS', () => {
it('Get CPU architecture name of underlying OS', () => {
// https://on.cypress.io/arch
expect(Cypress.arch).to.exist
expect(Cypress.arch).to.exist
})
})
})

context('Cypress.config()', () => {
beforeEach(() => {
cy.visit('http://localhost:8080/cypress-api')
})
context('Cypress.config()', () => {
beforeEach(() => {
cy.visit('http://localhost:8080/cypress-api')
})

it('Get and set configuration options', () => {
it('Get and set configuration options', () => {
// https://on.cypress.io/config
let myConfig = Cypress.config()
let myConfig = Cypress.config()

expect(myConfig).to.have.property('animationDistanceThreshold', 5)
expect(myConfig).to.have.property('baseUrl', null)
expect(myConfig).to.have.property('defaultCommandTimeout', 4000)
expect(myConfig).to.have.property('requestTimeout', 5000)
expect(myConfig).to.have.property('responseTimeout', 30000)
expect(myConfig).to.have.property('viewportHeight', 660)
expect(myConfig).to.have.property('viewportWidth', 1000)
expect(myConfig).to.have.property('pageLoadTimeout', 60000)
expect(myConfig).to.have.property('waitForAnimations', true)
expect(myConfig).to.have.property('animationDistanceThreshold', 5)
expect(myConfig).to.have.property('baseUrl', null)
expect(myConfig).to.have.property('defaultCommandTimeout', 4000)
expect(myConfig).to.have.property('requestTimeout', 5000)
expect(myConfig).to.have.property('responseTimeout', 30000)
expect(myConfig).to.have.property('viewportHeight', 660)
expect(myConfig).to.have.property('viewportWidth', 1000)
expect(myConfig).to.have.property('pageLoadTimeout', 60000)
expect(myConfig).to.have.property('waitForAnimations', true)

expect(Cypress.config('pageLoadTimeout')).to.eq(60000)
expect(Cypress.config('pageLoadTimeout')).to.eq(60000)

// this will change the config for the rest of your tests!
Cypress.config('pageLoadTimeout', 20000)
// this will change the config for the rest of your tests!
Cypress.config('pageLoadTimeout', 20000)

expect(Cypress.config('pageLoadTimeout')).to.eq(20000)
expect(Cypress.config('pageLoadTimeout')).to.eq(20000)

Cypress.config('pageLoadTimeout', 60000)
Cypress.config('pageLoadTimeout', 60000)
})
})
})

context('Cypress.dom', () => {
beforeEach(() => {
cy.visit('http://localhost:8080/cypress-api')
})
context('Cypress.dom', () => {
beforeEach(() => {
cy.visit('http://localhost:8080/cypress-api')
})

// https://on.cypress.io/dom
it('.isHidden() - determine if a DOM element is hidden', () => {
let hiddenP = Cypress.$('.dom-p p.hidden').get(0)
let visibleP = Cypress.$('.dom-p p.visible').get(0)
// https://on.cypress.io/dom
it('.isHidden() - determine if a DOM element is hidden', () => {
let hiddenP = Cypress.$('.dom-p p.hidden').get(0)
let visibleP = Cypress.$('.dom-p p.visible').get(0)

// our first paragraph has css class 'hidden'
expect(Cypress.dom.isHidden(hiddenP)).to.be.true
expect(Cypress.dom.isHidden(visibleP)).to.be.false
// our first paragraph has css class 'hidden'
expect(Cypress.dom.isHidden(hiddenP)).to.be.true
expect(Cypress.dom.isHidden(visibleP)).to.be.false
})
})
})

context('Cypress.env()', () => {
beforeEach(() => {
cy.visit('http://localhost:8080/cypress-api')
})
context('Cypress.env()', () => {
beforeEach(() => {
cy.visit('http://localhost:8080/cypress-api')
})

// We can set environment variables for highly dynamic values
// We can set environment variables for highly dynamic values

// https://on.cypress.io/environment-variables
it('Get environment variables', () => {
// https://on.cypress.io/environment-variables
it('Get environment variables', () => {
// https://on.cypress.io/env
// set multiple environment variables
Cypress.env({
host: 'veronica.dev.local',
api_server: 'http://localhost:8888/v1/',
})
Cypress.env({
host: 'veronica.dev.local',
api_server: 'http://localhost:8888/v1/',
})

// get environment variable
expect(Cypress.env('host')).to.eq('veronica.dev.local')
// get environment variable
expect(Cypress.env('host')).to.eq('veronica.dev.local')

// set environment variable
Cypress.env('api_server', 'http://localhost:8888/v2/')
expect(Cypress.env('api_server')).to.eq('http://localhost:8888/v2/')
// set environment variable
Cypress.env('api_server', 'http://localhost:8888/v2/')
expect(Cypress.env('api_server')).to.eq('http://localhost:8888/v2/')

// get all environment variable
expect(Cypress.env()).to.have.property('host', 'veronica.dev.local')
expect(Cypress.env()).to.have.property('api_server', 'http://localhost:8888/v2/')
// get all environment variable
expect(Cypress.env()).to.have.property('host', 'veronica.dev.local')
expect(Cypress.env()).to.have.property('api_server', 'http://localhost:8888/v2/')
})
})
})

context('Cypress.log', () => {
beforeEach(() => {
cy.visit('http://localhost:8080/cypress-api')
})
context('Cypress.log', () => {
beforeEach(() => {
cy.visit('http://localhost:8080/cypress-api')
})

it('Control what is printed to the Command Log', () => {
it('Control what is printed to the Command Log', () => {
// https://on.cypress.io/cypress-log
})
})
})

context('Cypress.platform', () => {
beforeEach(() => {
cy.visit('http://localhost:8080/cypress-api')
})
context('Cypress.platform', () => {
beforeEach(() => {
cy.visit('http://localhost:8080/cypress-api')
})

it('Get underlying OS name', () => {
it('Get underlying OS name', () => {
// https://on.cypress.io/platform
expect(Cypress.platform).to.be.exist
expect(Cypress.platform).to.be.exist
})
})
})

context('Cypress.version', () => {
beforeEach(() => {
cy.visit('http://localhost:8080/cypress-api')
})
context('Cypress.version', () => {
beforeEach(() => {
cy.visit('http://localhost:8080/cypress-api')
})

it('Get current version of Cypress being run', () => {
it('Get current version of Cypress being run', () => {
// https://on.cypress.io/version
expect(Cypress.version).to.be.exist
expect(Cypress.version).to.be.exist
})
})
})

context('Cypress.spec', () => {
beforeEach(() => {
cy.visit('http://localhost:8080/cypress-api')
})
context('Cypress.spec', () => {
beforeEach(() => {
cy.visit('http://localhost:8080/cypress-api')
})

it('Get current spec information', () => {
it('Get current spec information', () => {
// https://on.cypress.io/spec
// wrap the object so we can inspect it easily by clicking in the command log
cy.wrap(Cypress.spec).should('include.keys', ['name', 'relative', 'absolute'])
cy.wrap(Cypress.spec).should('include.keys', ['name', 'relative', 'absolute'])
})
})
})
2 changes: 0 additions & 2 deletions cypress/e2e/2-advanced-examples/files.cy.js
Expand Up @@ -7,9 +7,7 @@ const requiredExample = require('../../fixtures/example')
context('Files', () => {
beforeEach(() => {
cy.visit('http://localhost:8080/commands/files')
})

beforeEach(() => {
// load example.json fixture file and store
// in the test context object
cy.fixture('example.json').as('example')
Expand Down

0 comments on commit 1cb6bed

Please sign in to comment.