Skip to content

Commit

Permalink
change examples with 'function ()' to ES6 '() =>' (cypress-io#2693)
Browse files Browse the repository at this point in the history
  • Loading branch information
jennifer-shehane authored Apr 7, 2020
1 parent c25eed0 commit 1a4406b
Show file tree
Hide file tree
Showing 30 changed files with 175 additions and 233 deletions.
6 changes: 3 additions & 3 deletions source/api/commands/as.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ The name of the alias to be referenced later within a {% url `cy.get()` get %} o
Aliasing a DOM element and then using {% url `cy.get()` get %} to access the aliased element.

```javascript
it('disables on click', function () {
it('disables on click', () => {
cy.get('button[type=submit]').as('submitBtn')
cy.get('@submitBtn').click().should('be.disabled')
})
Expand All @@ -70,7 +70,7 @@ cy.wait('@userPut')
Aliasing {% url `cy.fixture()` fixture %} data and then using `this` to access it via the alias.

```javascript
beforeEach(function () {
beforeEach(() => {
cy.fixture('users-admins.json').as('admins')
})

Expand Down Expand Up @@ -120,7 +120,7 @@ describe('A fixture', () => {
})

describe('aliased in beforeEach()', () => {
beforeEach(function () {
beforeEach(() => {
cy.fixture('admin-users.json').as('admins')
})

Expand Down
8 changes: 4 additions & 4 deletions source/api/commands/each.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ Iterate through an array like structure (arrays or objects with a `length` prope
**{% fa fa-check-circle green %} Correct Usage**

```javascript
cy.get('ul>li').each(function () {...}) // Iterate through each 'li'
cy.getCookies().each(function () {...}) // Iterate through each cookie
cy.get('ul>li').each(() => {...}) // Iterate through each 'li'
cy.getCookies().each(() => {...}) // Iterate through each cookie
```

**{% fa fa-exclamation-triangle red %} Incorrect Usage**

```javascript

cy.each(function () {...}) // Errors, cannot be chained off 'cy'
cy.location().each(function () {...}) // Errors, 'location' doesn't yield an array
cy.each(() => {...}) // Errors, cannot be chained off 'cy'
cy.location().each(() => {...}) // Errors, 'location' doesn't yield an array
```

## Arguments
Expand Down
8 changes: 4 additions & 4 deletions source/api/commands/get.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,23 +122,23 @@ cy.get('@todos')
### Get the aliased 'submitBtn' element

```javascript
beforeEach(function() {
beforeEach(() => {
cy.get('button[type=submit]').as('submitBtn')
})

it('disables on click', function() {
it('disables on click', () => {
cy.get('@submitBtn').should('be.disabled')
})
```

### Get the aliased 'users' fixture

```javascript
beforeEach(function() {
beforeEach(() => {
cy.fixture('users.json').as('users')
})

it('disables on click', function() {
it('disables on click', () => {
// access the array of users
cy.get('@users').then((users) => {
// get the first user
Expand Down
2 changes: 1 addition & 1 deletion source/api/commands/request.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ You can also set options for `cy.request()`'s `baseUrl` and `responseTimeout` gl
`cy.request()` is great for talking to an external endpoint before your tests to seed a database.

```javascript
beforeEach(function () {
beforeEach(() => {
cy.request('http://localhost:8080/db/seed')
})
```
Expand Down
8 changes: 4 additions & 4 deletions source/api/commands/screenshot.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ The screenshot will be stored in the `cypress/screenshots` folder by default. Yo
```javascript
// cypress/integration/users.spec.js

describe('my tests', function () {
it('takes a screenshot', function () {
describe('my tests', () => {
it('takes a screenshot', () => {
// screenshot will be saved as
// cypress/screenshots/users.spec.js/my tests -- takes a screenshot.png
cy.screenshot()
Expand Down Expand Up @@ -164,8 +164,8 @@ Screenshot naming follows these rules:
For example, given a spec file located at `cypress/integration/users/login_spec.js`:

```javascript
describe('my tests', function () {
it('takes a screenshot', function () {
describe('my tests', () => {
it('takes a screenshot', () => {
cy.screenshot() // cypress/screenshots/users/login_spec.js/my tests -- takes a screenshot.png
cy.screenshot() // cypress/screenshots/users/login_spec.js/my tests -- takes a screenshot (1).png
cy.screenshot() // cypress/screenshots/users/login_spec.js/my tests -- takes a screenshot (2).png
Expand Down
2 changes: 1 addition & 1 deletion source/api/commands/server.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ cy.route('/activities/**', 'fixture:activities.json')

```javascript
// Application Code
$(function () {
$(() => {
$.get('/activities')

// this will be sent back 404 since it
Expand Down
4 changes: 2 additions & 2 deletions source/api/commands/type.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,12 @@ cy.get('button').click()
Modifiers are automatically released between tests, even with `{release: false}`.

```javascript
it('has modifiers activated', function () {
it('has modifiers activated', () => {
// 'altKey' will be true while typing 'foo'
cy.get('input').type('{alt}foo', { release: false })
})

it('does not have modifiers activated', function () {
it('does not have modifiers activated', () => {
// 'altKey' will be false while typing 'bar'
cy.get('input').type('bar')
})
Expand Down
14 changes: 7 additions & 7 deletions source/api/commands/viewport.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,28 +86,28 @@ cy.viewport(1024, 768)
### Organize desktop vs mobile tests separately

```javascript
describe('Nav Menus', function () {
context('720p resolution', function () {
beforeEach(function () {
describe('Nav Menus', () => {
context('720p resolution', () => {
beforeEach(() => {
// run these tests as if in a desktop
// browser with a 720p monitor
cy.viewport(1280, 720)
})

it('displays full header', function () {
it('displays full header', () => {
cy.get('nav .desktop-menu').should('be.visible')
cy.get('nav .mobile-menu').should('not.be.visible')
})
})

context('iphone-5 resolution', function () {
beforeEach(function () {
context('iphone-5 resolution', () => {
beforeEach(() => {
// run these tests as if in a mobile browser
// and ensure our responsive UI is correct
cy.viewport('iphone-5')
})

it('displays mobile menu on click', function () {
it('displays mobile menu on click', () => {
cy.get('nav .desktop-menu').should('not.be.visible')
cy.get('nav .mobile-menu')
.should('be.visible')
Expand Down
2 changes: 1 addition & 1 deletion source/api/commands/visit.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ Cypress will automatically apply the server and routes to the very next `cy.visi
***Visit example application in a `beforeEach`***

```javascript
beforeEach(function () {
beforeEach(() => {
cy.visit('https://example.cypress.io/commands/viewport')
})
```
Expand Down
4 changes: 2 additions & 2 deletions source/api/cypress-api/arch.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ Cypress.arch // 'x64'
## CPU Architecture

```javascript
it('has expected CPU architecture', function () {
it('has expected CPU architecture', () => {
expect(Cypress.arch).to.be.oneOf(['x64', 'ia32'])
})
```

## Conditionals

```javascript
it('does something differently', function () {
it('does something differently', () => {
if (Cypress.arch === 'x64') {
cy.exec('something')
} else {
Expand Down
4 changes: 2 additions & 2 deletions source/api/cypress-api/browser.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Property | Type | Description
### `Cypress.browser` returns browser object

```js
it('log browser info', function() {
it('log browser info', () => {
console.log(Cypress.browser)
// {
// channel: 'stable',
Expand Down Expand Up @@ -62,7 +62,7 @@ it('log browser info', function() {
```

```javascript
it('has correct Chrome specific css property', function () {
it('has correct Chrome specific css property', () => {
// if in Chrome, check css property was properly applied
if (Cypress.browser.name === 'chrome') {
cy
Expand Down
14 changes: 7 additions & 7 deletions source/api/cypress-api/cookies.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,16 @@ You can use `Cypress.Cookies.preserveOnce()` to preserve cookies through multipl
There are *likely* better ways to do this, but this isn't well documented at the moment. Every application is different and there is no one-size-fits-all solution. For the moment, if you're using session-based cookies, this method will work.

```javascript
describe('Dashboard', function () {
before(function () {
describe('Dashboard', () => {
before () => {
// log in only once before any of the tests run.
// your app will likely set some sort of session cookie.
// you'll need to know the name of the cookie(s), which you can find
// in your Resources -> Cookies panel in the Chrome Dev Tools.
cy.login()
})

beforeEach(function () {
beforeEach () => {
// before each test, we can automatically preserve the
// 'session_id' and 'remember_token' cookies. this means they
// will not be cleared before the NEXT test starts.
Expand All @@ -103,15 +103,15 @@ describe('Dashboard', function () {
Cypress.Cookies.preserveOnce('session_id', 'remember_token')
})

it('displays stats', function () {
it('displays stats', () => {
// ...
})

it('can do something', function () {
it('can do something', () => {
// ...
})

it('opens a modal', function () {
it('opens a modal', () => {
// ...
})
})
Expand Down Expand Up @@ -170,7 +170,7 @@ Cypress.Cookies.defaults({

```javascript
Cypress.Cookies.defaults({
whitelist: function(cookie) {
whitelist: (cookie) => {
// implement your own logic here
// if the function returns truthy
// then the cookie will not be cleared
Expand Down
6 changes: 3 additions & 3 deletions source/api/cypress-api/custom-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ const search = (term, options = {}) => {
.wait('@getSearchResults')
}

it('displays a list of search results', function () {
it('displays a list of search results', () => {
cy
.visit('/page')
.then(() => {
Expand All @@ -626,7 +626,7 @@ it('displays a list of search results', function () {
.get('#pagination').should('not.exist')
})

it('displays no search results', function () {
it('displays no search results', () => {
cy
.visit('/page')
.then(() => {
Expand All @@ -637,7 +637,7 @@ it('displays no search results', function () {
.get('#results').should('contain', 'No results found')
})

it('paginates many search results', function () {
it('paginates many search results', () => {
cy
.visit('/page')
.then(() => {
Expand Down
6 changes: 3 additions & 3 deletions source/api/cypress-api/isbrowser.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Property | Type | Description
```javascript
// true when running in Chrome
if (Cypress.isBrowser('chrome')) {
it('only runs in chrome', function () {
it('only runs in chrome', () => {
// test some (hypothetical) issue with chrome
})
}
Expand All @@ -64,7 +64,7 @@ it('a test', function() {
### Only run commands in Chromium-based browser

```javascript
it('has correct Chromium-based specific css property', function () {
it('has correct Chromium-based specific css property', () => {
// if in Chromium-based browser (Chrome, Electron, etc...)
// check css property was properly applied
if (Cypress.isBrowser({ family: 'chromium' })) {
Expand All @@ -81,7 +81,7 @@ it('has correct Chromium-based specific css property', function () {
```javascript
// true when running in any stable release of a Chromium-based browser
if (Cypress.isBrowser({ family: 'chromium', channel: 'stable' })) {
it('will not run in Canary or Dev browsers', function () {
it('will not run in Canary or Dev browsers', () => {
// test some (hypothetical) issue with chrome
})
}
Expand Down
2 changes: 1 addition & 1 deletion source/api/cypress-api/platform.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Cypress.platform // 'darwin'
## Conditionals

```javascript
it('has JSON files', function () {
it('has JSON files', () => {
// if windows do one thing, else do another
const cmd = Cypress.platform === 'win32' ? 'dir *.json' : 'ls *.json'

Expand Down
2 changes: 1 addition & 1 deletion source/api/cypress-api/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Cypress.spec // returns spec object
### `Cypress.spec` returns an object

```js
it('log spec info', function() {
it('log spec info', () => {
console.log(Cypress.spec)
// {
// name: 'filter.spec.js',
Expand Down
14 changes: 7 additions & 7 deletions source/api/events/catalog-of-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ Cypress.on('uncaught:exception', (err, runnable) => {
### To catch a single uncaught exception

```javascript
it('is doing something very important', function (done) {
it('is doing something very important', (done) => {
// this event will automatically be unbound when this
// test ends because it's attached to 'cy'
cy.on('uncaught:exception', (err, runnable) => {
Expand Down Expand Up @@ -222,7 +222,7 @@ Cypress.on('fail', (error, runnable) => {
throw error // throw error to have test still fail
})

it('calls the "fail" callback when this test fails', function () {
it('calls the "fail" callback when this test fails', () => {
// when this cy.get() fails the callback
// is invoked with the error
cy.get('element-that-does-not-exist')
Expand All @@ -241,7 +241,7 @@ $('button').on('click', (e) => {
})

// test code
it('redirects to another page on click', function (done) {
it('redirects to another page on click', (done) => {
// this event will automatically be unbound when this
// test ends because it's attached to 'cy'
cy.on('window:before:unload', (e) => {
Expand All @@ -266,7 +266,7 @@ it('redirects to another page on click', function (done) {
### Modify your Application before it loads after page transitions

```javascript
it('can modify the window prior to page load on all pages', function () {
it('can modify the window prior to page load on all pages', () => {
// create the stub here
const ga = cy.stub().as('ga')

Expand Down Expand Up @@ -321,7 +321,7 @@ $('button').on('click', (e) => {
})

// test code
it('can control application confirms', function (done) {
it('can control application confirms', (done) => {
let count = 0

// make sure you bind to this **before** the
Expand Down Expand Up @@ -364,7 +364,7 @@ it('can control application confirms', function (done) {
cy.get('button').click()
})

it('could also use a stub instead of imperative code', function () {
it('could also use a stub instead of imperative code', () => {
const stub = cy.stub()

// not necessary but showing for clarity
Expand Down Expand Up @@ -400,7 +400,7 @@ $('button').on('click', (e) => {
alert('friend')
})

it('can assert on the alert text content', function () {
it('can assert on the alert text content', () => {
const stub = cy.stub()

cy.on('window:alert', stub)
Expand Down
Loading

0 comments on commit 1a4406b

Please sign in to comment.