Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
cy.visit('index.html')
// before the request goes out we need to set up spying
// see https://on.cypress.io/network-requests
cy.server()
cy.route('POST', '/posts').as('post')
cy.get('#load').click()
cy.contains('#output', '"title": "example post"').should('be.visible')
// Spok https://github.com/thlorenz/spok is a mix between schema and value assertions
// Since it supports nested objects, in a single "should()" we can verify desired
// properties of the XHR object, its request and response nested objects.
cy.get('@post').should(spok({
status: 201,
url: spok.endsWith('posts'),
// network request takes at least 10ms
// but should finish in less than 1 second
duration: spok.range(10, 1000),
statusMessage: spok.string,
// check the request inside XHR object
request: {
// using special keyword "$topic" to get
// nicer indentation in the command log
$topic: 'request',
body: {
title: 'example post',
userId: 1,
},
},
response: {
$topic: 'response',