Skip to content

Commit

Permalink
add list fixture specs (#710)
Browse files Browse the repository at this point in the history
* add list fixture specs

* add array assertion

* update expected filename
  • Loading branch information
bahmutov authored Jun 3, 2021
1 parent 275b9d2 commit ca8ad7e
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 31 deletions.
1 change: 1 addition & 0 deletions examples/fundamentals__fixtures/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ You can load fixture data using the [`cy.fixture()`](https://on.cypress.io/fixtu
- Load multiple files using closures in [multiple-fixtures-spec.js](cypress/integration/multiple-fixtures-spec.js)
- Load multiple files once or before each test in [load-fixtures-spec.js](cypress/integration/load-fixtures-spec.js)
- Require multiple JSON fixtures in [require-fixtures-spec.js](cypress/integration/require-fixtures-spec.js)
- Iterate over a list loaded from a JSON fixture in [list-spec.js](cypress/integration/list-spec.js)

For more examples see the "Fixtures" section in the [Cypress Testing Workshop](https://github.com/cypress-io/testing-workshop-cypress).
14 changes: 14 additions & 0 deletions examples/fundamentals__fixtures/cypress/fixtures/users.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"name": "Joe Bravo",
"age": 20
},
{
"name": "Mike Smith",
"age": 30
},
{
"name": "Alicia Blue",
"age": 25
}
]
25 changes: 25 additions & 0 deletions examples/fundamentals__fixtures/cypress/integration/list-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/// <reference types="cypress" />
const allUsers = require('../fixtures/users.json')

describe('array fixture', () => {
it('iterates over a list', () => {
cy.fixture('users').then((users) => {
expect(users).to.be.an('array').and.to.have.have.length(3)

users.forEach((user) => {
expect(user).to.have.keys(['name', 'age'])
expect(user.age).to.be.a('number').and.be.gt(10).and.be.lt(100)
})
})
})

// we can dynamically create tests from a static JSON list
// that we have loaded using "require" or "import" statement
// for more examples, see "Dynamic tests" recipes
allUsers.forEach((user) => {
it(`has user ${user.name}`, () => {
cy.wrap(user).should('have.property', 'name', user.name)
cy.wrap(user).should('have.property', 'age', user.age)
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,6 @@
const city = require('../fixtures/city.json')
const country = require('../fixtures/country.json')

// bundled "os" module does not have the right EOL
// thus we need to set it based on the platform ourself
const EOL = Cypress.platform === 'win32' ? '\\' : '/'

/**
* Joins parts of the file path using the EOL
* @example
* join('cypress', 'integration')
* // "cypress/integration" on non-Windows
* // "cypress\integration" on Windows
*/
const join = (...paths) => paths.join(EOL)

describe('requires fixtures', () => {
it('has city', () => {
expect(city).to.deep.equal({ name: 'Atlanta' })
Expand All @@ -26,22 +13,4 @@ describe('requires fixtures', () => {
it('has country', () => {
expect(country).to.deep.equal({ name: 'United States' })
})

context('sanity JavaScript tests', () => {
// confirm that the spec information is present

it('has __dirname', () => {
expect(__dirname).to.be.a('string')
// on Windows OS the directory is "cypress\integration"
// on other operating systems it is "cypress/integration"
expect(__dirname).to.equal(join('cypress', 'integration'))
})

it('has __filename', () => {
expect(__filename).to.be.a('string')
expect(__filename).to.equal(
join('cypress', 'integration', 'require-fixtures-spec.js')
)
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/// <reference types="cypress" />

// bundled "os" module does not have the right EOL
// thus we need to set it based on the platform ourself
const EOL = Cypress.platform === 'win32' ? '\\' : '/'

/**
* Joins parts of the file path using the EOL
* @example
* join('cypress', 'integration')
* // "cypress/integration" on non-Windows
* // "cypress\integration" on Windows
*/
const join = (...paths) => paths.join(EOL)

describe('sanity JavaScript tests', () => {
// confirm that the spec information is present

it('has __dirname', () => {
expect(__dirname).to.be.a('string')
// on Windows OS the directory is "cypress\integration"
// on other operating systems it is "cypress/integration"
expect(__dirname).to.equal(join('cypress', 'integration'))
})

it('has __filename', () => {
expect(__filename).to.be.a('string')
expect(__filename).to.equal(
join('cypress', 'integration', 'sanity-js-spec.js')
)
})
})

0 comments on commit ca8ad7e

Please sign in to comment.