- how one test can affect another test by leaving its data behind
- when and how to reset state during testing
+++
- keep
todomvc
app running - open
cypress/integration/04-reset-state/spec.js
- if you reload the test it starts failing 😕
+++
+++
+++
+++
beforeEach(() => {
cy.visit('/')
})
const addItem = text => {
cy.get('.new-todo').type(`${text}{enter}`)
}
it('adds two items', () => {
addItem('first item')
addItem('second item')
cy.get('li.todo').should('have.length', 2)
})
+++
- how to reset the database?
- tip we are using json-server-reset middleware
- try to reset it from command line
$ http POST :3000/reset todos:=[]
Note:
I am using httpie
to easily send the empty list to reset the database.
+++
- how to make an arbitrary cross-domain XHR request from Cypress?
- reset the database before each test
- modify
04-reset-state/spec.js
to make XHR call to reset the database - before or after
cy.visit
?
- modify
Note:
Students should modify cypress/integration/04-reset-state/spec.js
and make the request to reset the database before each test using cy.request
.
The answer to this and other TODO assignments are in cypress/integration/04-reset-state/answer.js file.
+++
"start": "json-server --static . --watch data.json"
If we overwrite todomvc/data.json
and reload the web app we should see new data
+++
describe('reset data using cy.writeFile', () => {
beforeEach(() => {
// TODO write file "todomvc/data.json" with stringified todos object
cy.visit('/')
})
...
})
See cy.writeFile
+++ Make sure you are writing the right file.
Note: Most common mistake is using file path relative to the spec file, should be relative to the project's root folder.
+++
You can execute Node code during browser tests by calling cy.task
// cypress/plugins/index.js
module.exports = (on, config) => {
on('task', {
hello(name) {
console.log('Hello', name)
return null // or Promise
}
})
}
// cypress/integration/spec.js
cy.task('hello', 'World')
+++
Find "resetData" task in cypress/plugins/index.js
describe('reset data using a task', () => {
beforeEach(() => {
// call the task "resetData"
cy.visit('/')
})
})
+++
Pass an object when calling cy.task('resetData')
it('sets data to complex object right away', () => {
cy.task('resetData', /* object*/)
cy.visit('/')
// check what is rendered
})
+++
Pass an object when calling cy.task('resetData')
it('sets data using fixture', () => {
// load todos from "cypress/fixtures/two-items.json"
// and the call the task to set todos
cy.visit('/')
// check what is rendered
})
+++
- reset state before each test
- in our Best practices guide
- use
cy.request
,cy.exec
,cy.task