Skip to content

Commit 271e211

Browse files
authored
add data access example
1 parent 4c64f8f commit 271e211

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,38 @@ The fixture file should be an array of items.
4040
4141
See [todos.cy.js](./cypress/e2e/todos.cy.js) and [todos.json](./cypress/fixtures/todos.json) for examples
4242
43+
The mocks and data are reset before each test.
44+
45+
### data access
46+
47+
You can get the "live" data for each resource by name, for example
48+
49+
```js
50+
it('adds a todo', { rest: { todos: 'todos.json' } }, () => {
51+
// use the REST resource name
52+
const todos = Cypress.env('todos')
53+
const n = todos.length
54+
cy.visit('/')
55+
cy.get('li.todo').should('have.length', n)
56+
cy.get('input.new-todo')
57+
.type('Write tests{enter}')
58+
// there should be one more item in the array
59+
.then(() => {
60+
expect(todos).to.have.length(n + 1)
61+
})
62+
})
63+
```
64+
65+
You need `cy.then` to access the changed data _after_ Cypress commands have finished. Alternatively, you can wrap the array reference:
66+
67+
```js
68+
cy.get('input.new-todo')
69+
.type('Write tests{enter}')
70+
// there should be one more item in the array
71+
cy.wrap(todos)
72+
.should('have.length', n + 1)
73+
```
74+
4375
### baseUrl
4476
4577
If all your REST endpoints use the same prefix, you can set the `baseUrl` option

0 commit comments

Comments
 (0)