Skip to content

Commit

Permalink
Some fixes for intercept tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjhenkes committed Nov 7, 2022
1 parent cfd9c52 commit 65ad6c7
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 21 deletions.
2 changes: 1 addition & 1 deletion examples/blogs__vue-vuex-rest/cypress/e2e/api-spec.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe('Misc tests', () => {
// from a fixture file
it('loads todos from fixture file', () => {
// loads response from "cypress/fixtures/todos.json"
cy.intercept('/todos', 'fixture:todos')
cy.intercept('/todos', { fixture: 'todos' })
cy.visit('/')
getTodoItems()
.should('have.length', 2)
Expand Down
2 changes: 1 addition & 1 deletion examples/blogs__vue-vuex-rest/data.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"todos": []
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe('Logging In - XHR Web Form', function () {
'POST',
'/login',
{
status: 503,
statusCode: 503,
response: {},
}
)
Expand All @@ -86,7 +86,7 @@ describe('Logging In - XHR Web Form', function () {
// we can even test that the correct request
// body was sent in this XHR
cy.wait('@postLogin')
.its('requestBody')
.its('request.body')
.should('deep.eq', {
username: 'jane.lae',
password: 'password123',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ describe('clock', () => {
// force the application to pass 1 second really quickly
// https://on.cypress.io/tick
cy.tick(1001)
cy.wait('@post').should('have.property', 'status', 201)
cy.wait('@post').its('response').should('have.property', 'statusCode', 201)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ describe('waits', () => {
cy.get('@post.all').should('have.length', 2)
.then((xhrs) => {
// xhrs is an array of network call objects
expect(xhrs[0], 'first request status').to.have.property('status', 201)
expect(xhrs[1], 'second request status').to.have.property('status', 201)
expect(xhrs[0].response, 'first request status').to.have.property('statusCode', 201)
expect(xhrs[1].response, 'second request status').to.have.property('statusCode', 201)
})

// and we can make assertions about each separate call
// by retrieving it like this (index starts with 1)
// cy.get('<alias>.<index>')
cy.get('@post.1').should((xhr1) => {
expect(xhr1, 'first request').to.have.property('status', 201)
expect(xhr1.response, 'first request').to.have.property('statusCode', 201)
})

// we cannot guarantee the order of XHR requests - they really depend on the
// server response speed. Sometimes a later request finishes first.
// all we can say that each request should receive a response with
// id equal to 101 or 102
cy.get('@post.1').its('response.body.id').should('be.oneOf', [101, 102])
cy.get('@post.2').its('response.body.id').should('be.oneOf', [101, 102])
cy.get('@post.1').its('response').its('body.id').should('be.oneOf', [101, 102])
cy.get('@post.2').its('response').its('body.id').should('be.oneOf', [101, 102])
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,21 @@ describe('XHR', { retries: 3 }, () => {
// see https://on.cypress.io/get

// tip: log the request object to see everything it has in the console
cy.get('@post').then(console.log)
cy.wait('@post').then(console.log)

// you can retrieve the XHR multiple times - returns the same object
// confirm the request status
cy.get('@post').should('have.property', 'status', 201)
cy.get('@post').its('response').should('have.property', 'statusCode', 201)

// we cannot chain any more assertions to the above request object
// because the "have.property" assertion yields the property's value
// so let's just grab the request object again and run multiple assertions
cy.get('@post').should((req) => {
expect(req.method).to.equal('POST')
expect(req.url).to.match(/\/posts$/)
console.log('post req', req)
expect(req.request.method).to.equal('POST')
expect(req.request.url).to.match(/\/posts$/)
// it is good practice to add message to the assertion
expect(req, 'has duration in ms').to.have.property('duration').and.be.a('number')
expect(req, 'has a request id').to.have.property('browserRequestId').and.be.a('string')
})

// let's confirm the request sent to the server
Expand All @@ -66,7 +67,7 @@ describe('XHR', { retries: 3 }, () => {
})

// alternative: use "requestBody" alias to "request.body" property access
cy.get('@post').its('requestBody')
cy.get('@post').its('request.body')
.should('have.property', 'title', 'example post')

// get the same request object again and confirm the response
Expand Down Expand Up @@ -110,8 +111,8 @@ describe('XHR', { retries: 3 }, () => {
//
// https://on.cypress.io/wait
cy.wait('@post').should((xhr) => {
expect(xhr.status, 'successful POST').to.equal(201)
expect(xhr.url, 'post url').to.match(/\/posts$/)
expect(xhr.response.statusCode, 'successful POST').to.equal(201)
expect(xhr.request.url, 'post url').to.match(/\/posts$/)
// assert any other XHR properties
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ describe('stubbing', function () {
cy.intercept(
'/favorite-fruits',
{
status: 500,
statusCode: 500,
body: '',
delay: 2000,
headers: {
Expand Down
4 changes: 2 additions & 2 deletions examples/testing-dom__select2/cypress/e2e/spec.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,15 @@ describe('select2', () => {
// cy.intercept(
// 'https://jsonplaceholder.cypress.io/users?_type=query',
// {
// body: 'fixture:query.json',
// fixture: 'query.json',
// delay: 1000,
// }
// ).as('query')

// cy.intercept(
// 'https://jsonplaceholder.cypress.io/users?term=clem&_type=query&q=clem',
// {
// response: 'fixture:clem.json',
// fixture: 'clem.json',
// delay: 1000,
// }
// ).as('user_search')
Expand Down

0 comments on commit 65ad6c7

Please sign in to comment.