GithubHelp home page GithubHelp logo

cy-spok's Introduction

cy-spok renovate-app badge cypress version

Playing with spok inside Cypress test

CI status
lint Lint status
badges Badges status
cy-spok GH Action status
cy-spok-example cy-spok-example status

Learn

Install

$ npm i -D cy-spok

Use

See spok docs

// in your Cypress spec file
import spok from 'cy-spok'

const object = {
  one: 1,
  two: 2,
  three: 3,
  four: 4,
  helloWorld: 'hello world',
  anyNum: 999,
  anotherNum: 888,
  anArray: [1, 2],
  anotherArray: [1, 2, 3],
  anObject: {},
  id: 'abc123',
  list: ['one', 'two', 'three'],
}

// using Spok
// https://github.com/thlorenz/spok#readme
cy.wrap(object, { timeout: 2000 }).should(
  spok({
    $topic: 'spok-example', // optional
    one: spok.ge(1),
    two: 2,
    three: spok.range(2, 6),
    four: spok.lt(5),
    helloWorld: spok.startsWith('hello'),
    anyNum: spok.type('number'),
    anotherNum: spok.number,
    anArray: spok.array,
    anObject: spok.ne(undefined),
    // test a string using regular expression
    id: spok.test(/^abc\d{3}$/),
    // array with 3 elements
    list: spok.arrayElements(3),
  }),
)

See cypress/integration/spec.js here and in the cy-spok-example repo.

Spok in action

vs deep.equal

Spok prints a lot more information when using it compared to deep.equal. Note that Spok is a subset, not strict value equality.

deep.equal vs spok for complex objects

See deep-equal-spec.js

Treat arrays as an object

// verify each item in an array as an object
cy.wrap(['one', 42, 'three']).should(
  spok({
    0: spok.string,
    1: spok.number,
    2: spok.string,
  }),
)

Use Lodash predicates

Lodash has many predicate functions is..., see Lodash docs

cy.wrap({
  name: 'Joe',
  age: 20,
}).should(
  spok({
    name: Cypress._.isString,
    age: Cypress._.isNumber,
  }),
)

Own predicate

Any synchronous function that returns a boolean could be a predicate

// it is a list of strings
const areGreetings = (list) =>
  Array.isArray(list) && list.every(Cypress._.isString)

cy.wrap({
  greetings: ['hello', 'hi'],
}).should(
  spok({
    greetings: areGreetings,
  }),
)

First failure only

If there are multiple failing predicates, only the first one is shown. All passing predicates are shown

cy.wrap({
  name: 'Joe',
  age: 42,
  job: 'chimney sweeper',
  location: 'Boston',
  present: true,
}).should(
  spok({
    name: 'Mary', // fails
    age: 42, // passes
    job: 'secret agent', // fails
    location: 'Boston', // passes
    present: spok.type('boolean'), // passes
  }),
)

Only the first failed predicate is shown

Small print

Author: Gleb Bahmutov <[email protected]> ยฉ 2021

License: MIT - do anything with the code, but don't blame me if it does not work.

Support: if you find any problems with this module, email / tweet / open issue on Github

MIT License

Copyright (c) 2021 Gleb Bahmutov <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

cy-spok's People

Contributors

bahmutov avatar renovate-bot avatar renovate[bot] avatar stumblinbear avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

cy-spok's Issues

Create separate assertions per spok property

If we use should(cb) with multiple expect calls then we get separate assertions in the GUI

it('spoks', () => {
  const o = {
    name: 'Hello world',
    guess: 50
  }

  // using "regular" should callback function
  cy.wrap(o).should(obj => {
    expect(obj.name).to.match(/^Hello/)
    expect(obj.guess).to.be.gte(1).and.lte(10)
  })

  setTimeout(() => {
    o.guess = 7
  }, 1000)
})

Screen Shot 2019-12-13 at 9 05 27 AM

I want to show separate assertions using single spok too

it('spoks', () => {
  const o = {
    name: 'Hello world',
    guess: 50
  }

  // using Spok
  cy.wrap(o).should('spok', {
    name: spok.startsWith('Hello'),
    guess: spok.range(1, 10)
  })

  setTimeout(() => {
    o.guess = 7
  }, 1000)
})

Right now only the last assertion is shown, so I am forced to concatenate everything together

Screen Shot 2019-12-13 at 9 07 23 AM

TypeScript support

Trying to make cy-spok work with typescript.

Making a plugin work with Typescript is usually not easy, but I tried all ways and still am failing.

Does cy-spok work with Typescript, is there an example we can look at?

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/badges.yml
  • actions/checkout v4
  • stefanzweifel/git-auto-commit-action v5
  • ubuntu 22.04
.github/workflows/lint.yml
  • actions/checkout v4
  • bahmutov/npm-install v1
.github/workflows/main.yml
  • actions/checkout v4
  • cypress-io/github-action v5
  • actions/checkout v4
  • cypress-io/github-action v5
  • actions/checkout v4
  • cycjimmy/semantic-release-action v3
  • ubuntu 22.04
npm
package.json
  • @bahmutov/spok 1.2.6
  • strip-ansi 6.0.1
  • @bahmutov/cypress-esbuild-preprocessor ^2.1.5
  • cypress 13.13.2
  • esbuild ^0.23.0
  • prettier 3.3.3
  • semantic-release 24.0.0

  • Check this box to trigger a request for Renovate to run again on this repository

Problem installing cy-spok

Hello! I'm having trouble trying to install cy-spok on my project. Deletes the package-log.json file from the project and gave npm install again to the project and then used the command [npm install -D bahmutov / cy-spok] but unsuccessfully presented the following error message below:
npm ERR! code E404
npm ERR! 404 Object Not Found - GET https://skimdb.npmjs.com/registry/@bahmutov%2fspok - not_found
npm ERR! 404
npm ERR! 404 '@bahmutov/[email protected]' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.

Can anybody help me?
Operating system: windows 10 32 bit

Trying to use cy-spok with webpack as preprocessor.

Hey I'm trying to run cy-spok using https://github.com/cypress-io/cypress-webpack-preprocessor .. but got random errors which I cannot resolve so far.

Error: Webpack CompLooked for and couldn't find the file at the following paths:resolve 'child_process' in 'fullpath/node_modules/@bahmutov/spok/dist'

then tried to do this (because of webpack 5, no provide polifill)

module.exports = { resolve: { fallback: { child_process: false, },

I'm getting next error

Error: Webpack CompLooked for and couldn't find the file at the following paths:resolve 'util' in 'fullpath/node_modules/@bahmutov/spok/dist'

Thanks in advance if you can help with that one and provide some configuration related to webpack processor.

Cannot chain spoks

This works (single spok with 2 items)

it('spoks several times', () => {
  const o = {
    one: 1,
    two: 2,
    three: 3,
  }
  cy.wrap(o).should(spok({ one: 1, two: 2 }))
})

Screen Shot 2019-12-16 at 4 32 12 PM

but adding another assertion there breaks things

it('spoks several times', () => {
  const o = {
    one: 1,
    two: 2,
    three: 3,
  }
  cy.wrap(o)
    .should(spok({ one: 1, two: 2 }))
    .and(spok({ two: 2 }))
})

Screen Shot 2019-12-16 at 4 48 03 PM

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.