Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add cy-spok plugin #2320

Merged
merged 1 commit into from
Dec 19, 2019
Merged

add cy-spok plugin #2320

merged 1 commit into from
Dec 19, 2019

Conversation

bahmutov
Copy link
Contributor

https://github.com/bahmutov/cy-spok

validates complex objects in a single shot. For example, in our network assertions recipe

const spok = require('cy-spok')
it('asserts multiple XHR properties at once using cy-spok', () => {
  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',
      headers: {
        'content-type': 'application/json; charset=utf-8',
        'cache-control': 'no-cache',
      },
      body: {
        title: 'example post',
        body: spok.string,
        userId: 1,
        // we don't know the exact id the server assigns to the new post
        // but it should be > 100
        id: spok.gt(100),
      },
    },
  }))
})

Screen Shot 2019-12-18 at 4 32 44 PM

@bahmutov bahmutov requested a review from amirrustam December 18, 2019 21:33
@amirrustam
Copy link
Contributor

@bahmutov also submit it here: https://awesomejs.dev/for/cypress/

@bahmutov bahmutov merged commit be45406 into develop Dec 19, 2019
@matthamil matthamil deleted the add-cy-spok-plugin branch April 14, 2021 19:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants