New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

cypress-grep

Package Overview
Dependencies
Maintainers
1
Versions
55
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cypress-grep

Filter tests using substring

  • 1.7.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
55K
decreased by-17.97%
Maintainers
1
Weekly downloads
 
Created
Source

cypress-grep

ci status badges status renovate-app badge cypress version

Filter tests using substring

# run only tests with "hello" in their names
npx cypress run --env grep=hello

  ✓ hello world
  - works
  - works 2 @tag1
  - works 2 @tag1 @tag2

  1 passing (38ms)
  3 pending

All other tests will be marked pending, see Cypress test statuses

Install and use

Assuming you have Cypress installed, add this module as a dev dependency

# using NPM
npm i -D cypress-grep
# using Yarn
yarn add -D cypress-grep

required: load this module from the support file or at the top of the spec file if not using the support file.

// cypress/support/index.js
// load and register the grep feature
// https://github.com/bahmutov/cypress-grep
require('cypress-grep')()

optional: load and register this module from the plugin file

// cypress/plugins/index.js
module.exports = (on, config) => {
  // optional: register cypress-grep plugin code
  // https://github.com/bahmutov/cypress-grep
  require('cypress-grep/src/plugin')(config)
}

The plugin code will print a little message on load, for example

$ npx cypress run --env grep=hello
cypress-grep: only running tests with "hello" in their names

Filter by grep

You can use any way to modify the environment value grep except the run-time Cypress.env('grep') (because it is too late at run-time). You can set the grep value in the cypress.json file to run only tests with the substring @smoke in their names

{
  "env": {
    "grep": "@smoke"
  }
}

You can also set the env.grep object in the plugin file, but remember to return the changed config object:

// cypress/plugin/index.js
module.exports = (on, config) => {
  config.env.grep = '@smoke'
  return config
}

Most likely you will pass the grep string via CLI when launching Cypress

$ npx cypress run --env grep=@smoke

AND tags

Use + to require both tags to be present

--env grep=@smoke+@fast

Invert tag

You can skip running the tests with specific tag using the invert option: prefix the tag with the character -.

# do not run any tests with tag "@slow"
--env grep=-@slow

OR tags

You can run tests that match one tag or another using spaces. Make sure to quote the grep string!

# run tests with tags "@slow" or "@critical" in their names
--env grep='@slow @critical'

Tags in the test config object

Cypress tests can have their own test config object, and when using this plugin you can put the test tags there, either as a single tag string or as an array of tags.

it('works as an array', { tags: ['config', 'some-other-tag'] }, () => {
  expect(true).to.be.true
})

it('works as a string', { tags: 'config' }, () => {
  expect(true).to.be.true
})

You can run both of these tests using --env grep=config string.

TypeScript users

Because the Cypress test config object type definition does not have the tags property we are using above, the TypeScript linter will show an error. Just add an ignore comment above the test:

// @ts-ignore
it('runs on deploy', { tags: 'smoke' }, () => {
  ...
})

Test suites

The tags are also applied to the "describe" blocks with some limitations:

  • you can only use the config object tags
describe('block with config tag', { tags: '@smoke' }, () => {
})
  • currently only the invert tag to skip the blog has meaningful effect. For example you can skip the above suite of tests by using --env grep=-@smoke value. Keep an eye on issue #22 for the full support implementation.

See the cypress/integration/describe-tags-spec.js file.

General advice

  • do not use spaces in the tags themselves, as --env grep=... string separates the grep string into OR tags using the space separator.
  • I like using @ as tag prefix to make the tags searchable
  • I like using the test or suite configuration object to explicitly list the tags
// ✅ good practice
describe('auth', { tags: '@critical' }, () => ...)
it('works', { tags: '@smoke' }, () => ...)
it('works', { tags: ['@smoke', '@fast'] }, () => ...)

// 🚨 NOT GOING TO WORK
// ERROR: treated as a single tag,
// probably want an array instead
it('works', { tags: '@smoke @fast' }, () => ...)

DevTools console

Watch this video to see how to run only one or some tagged tests without editing the spec source files!

  • from the browser, open the DevTools console
  • if you want to run tests with specific tag or tag expression, enter the command to set the grep value like Cypress.env('grep', '@tag1') for example and then click the reload button or press "r" key. The tests should re-run and the grep value should be applied.

Run-time grep from DevTools console

  • if you want to run just a test with a title, copy the test title and use an array with just the test title Cypress.env('grep', ['test title here']) and re-run the test

Run a single test by title from the DevTools console

  • to remove the grep string, enter Cypress.env('grep', null)

Debugging

This module uses debug to log verbose messages. To enable debug console messages, from the DevTools console set localStorage.debug='cypress-grep' and run the tests again.

Debug messages

Examples

See also

Small print

Author: Gleb Bahmutov <gleb.bahmutov@gmail.com> © 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 <gleb.bahmutov@gmail.com>

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.

FAQs

Package last updated on 03 May 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc