Gathering detailed insights and metrics for @bahmutov/cy-grep
Gathering detailed insights and metrics for @bahmutov/cy-grep
Gathering detailed insights and metrics for @bahmutov/cy-grep
Gathering detailed insights and metrics for @bahmutov/cy-grep
npm install @bahmutov/cy-grep
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
17 Stars
133 Commits
4 Forks
2 Watching
27 Branches
2 Contributors
Updated on 19 Oct 2024
JavaScript (99.09%)
TypeScript (0.91%)
Cumulative downloads
Total Downloads
Last day
-32.7%
5,657
Compared to previous day
Last week
-0.4%
37,971
Compared to previous week
Last month
14.9%
155,662
Compared to previous month
Last year
290.1%
1,261,263
Compared to previous year
1
Filter tests using substring or tag
1# run only tests with "hello" in their names 2npx cypress run --env grep=hello 3 4 ✓ hello world 5 - works 6 - works 2 @tag1 7 - works 2 @tag1 @tag2 8 9 1 passing (38ms) 10 3 pending
All other tests will be marked pending, see why in the Cypress test statuses blog post.
If you have multiple spec files, all specs will be loaded, and every test will be filtered the same way, since the grep is run-time operation and cannot eliminate the spec files without loading them. If you want to run only specific tests, use the built-in --spec CLI argument.
Watch the video intro to cypress-grep plugin and study my 🎓 Cypress course Cypress Plugins:
Assuming you have Cypress installed, add this module as a dev dependency.
1# using NPM 2npm i -D @bahmutov/cy-grep 3# using Yarn 4yarn add -D @bahmutov/cy-grep
Note: @bahmutov/cy-grep should work with all Cypress versions, but I mostly test it on the newest versions.
required: load this module from the support file or at the top of the spec file if not using the support file. You import the registration function and then call it:
1// cypress/support/e2e.js 2 3// load and register the grep feature using "require" function 4// https://github.com/bahmutov/cy-grep 5const registerCypressGrep = require('@bahmutov/cy-grep') 6registerCypressGrep() 7 8// if you want to use the "import" keyword 9// note: `./index.d.ts` currently extends the global Cypress types and 10// does not define `registerCypressGrep` so the import path is directly 11// pointed to the support file 12import registerCypressGrep from '@bahmutov/cy-grep/src/support' 13registerCypressGrep() 14 15// "import" with `@ts-ignore` 16// @see error 2306 https://github.com/microsoft/TypeScript/blob/3fcd1b51a1e6b16d007b368229af03455c7d5794/src/compiler/diagnosticMessages.json#L1635 17// @ts-ignore 18import registerCypressGrep from '@bahmutov/cy-grep' 19registerCypressGrep()
optional: load and register this module from the config file:
1// cypress.config.js 2{ 3 e2e: { 4 setupNodeEvents(on, config) { 5 require('@bahmutov/cy-grep/src/plugin')(config); 6 // IMPORTANT: return the config object 7 return config; 8 }, 9 } 10}
Installing the plugin via setupNodeEvents()
is required to enable the grepFilterSpecs feature.
Tip: you probably want to set these env
settings in your config file
1module.exports = defineConfig({ 2 env: { grepFilterSpecs: true, grepOmitFiltered: true }, 3 ... 4})
Trying to call the plugin function without any arguments or with more than a single argument throws an error
1// ERROR: forgot the config file 2setupNodeEvents(on, config) { 3 require('@bahmutov/cy-grep/src/plugin')(); 4} 5// ERROR: too many arguments 6setupNodeEvents(on, config) { 7 require('@bahmutov/cy-grep/src/plugin')(on, config); 8}
See test-cy-v9 for example
1// cypress/plugins/index.js 2module.exports = (on, config) => { 3 // `on` is used to hook into various events Cypress emits 4 // `config` is the resolved Cypress config 5 require('@bahmutov/cy-grep/src/plugin')(config) 6 // IMPORTANT: return the config object 7 return config 8}
1// cypress/support/index.js 2require('@bahmutov/cy-grep')()
Put the common settings into cypress.json
1{ 2 "env": { 3 "grepOmitFiltered": true, 4 "grepFilterSpecs": true 5 } 6}
You can filter tests to run using part of their title via grep
, and via explicit tags via grepTags
Cypress environment variables.
Most likely you will pass these environment variables from the command line. For example, to only run tests with "login" in their title and tagged "smoke", you would run:
Here are a few examples:
1# run only the tests with "auth user" in the title 2$ npx cypress run --env grep="auth user" 3# run tests with "hello" or "auth user" in their titles 4# by separating them with ";" character 5$ npx cypress run --env grep="hello; auth user" 6# run tests tagged @fast 7$ npx cypress run --env grepTags=@fast 8# run only the tests tagged "smoke" 9# that have "login" in their titles 10$ npx cypress run --env grep=login,grepTags=smoke 11# only run the specs that have any tests with "user" in their titles 12$ npx cypress run --env grep=user,grepFilterSpecs=true 13# only run the specs that have any tests tagged "@smoke" 14$ npx cypress run --env grepTags=@smoke,grepFilterSpecs=true 15# run only tests that do not have any tags 16# and are not inside suites that have any tags 17$ npx cypress run --env grepUntagged=true
You can use any way to modify the environment values grep
and grepTags
, 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 viewport
in their names
1{ 2 "env": { 3 "grep": "viewport" 4 } 5}
You can also set the env.grep
object in the plugin file, but remember to return the changed config object:
1// cypress/plugin/index.js 2module.exports = (on, config) => { 3 config.env.grep = 'viewport' 4 return config 5}
You can also set the grep and grepTags from the DevTools console while running Cypress in the interactive mode cypress open
, see DevTools Console section.
1# run all tests with "hello" in their title 2$ npx cypress run --env grep=hello 3# run all tests with "hello world" in their title 4$ npx cypress run --env grep="hello world"
You can pass multiple title substrings to match separating them with ;
character. Each substring is trimmed.
1# run all tests with "hello world" or "auth user" in their title 2$ npx cypress run --env grep="hello world; auth user"
The filter is also applied to the "describe" blocks. In that case, the tests look up if any of their outer suites are enabled.
1describe('block for config', () => { 2 it('should run', () => {}) 3 4 it('should also work', () => {}) 5})
# run any tests in the blocks including "config"
--env grep=config
Note: global function describe
and context
are aliases and both supported by this plugin.
1# run all tests WITHOUT "hello world" in their title 2$ npx cypress run --env grep="-hello world" 3# run tests with "hello", but without "world" in the titles 4$ npx cypress run --env grep="hello; -world"
You can select tests to run or skip using tags by passing --env grepTags=...
value.
# enable the tests with tag "one" or "two"
--env grepTags="one two"
# enable the tests with both tags "one" and "two"
--env grepTags="one+two"
# enable the tests with "hello" in the title and tag "smoke"
--env grep=hello,grepTags=smoke
If you can pass commas in the environment variable grepTags
, you can use ,
to separate the tags
# enable the tests with tag "one" or "two"
CYPRESS_grepTags=one,two npx cypress run
If a specific tag is not found in the specs, you will get a warning in the terminal:
$ npx cypress run --env grepTags=@wrong-tag
cy-grep: could not find the tag "@wrong-tag" in any of the specs
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.
1it('works as an array', { tags: ['config', 'some-other-tag'] }, () => { 2 expect(true).to.be.true 3}) 4 5it('works as a string', { tags: 'config' }, () => { 6 expect(true).to.be.true 7})
You can run both of these tests using --env grepTags=config
string.
Use +
to require both tags to be present
--env grepTags=@smoke+@fast
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 grepTags='@slow @critical'
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 grepTags=-@slow
If you want to run all tests with tag @slow
but without tag @smoke
:
--env grepTags=@slow+-@smoke
Note: Inverted tag filter is not compatible with the grepFilterSpecs
option
You can skip running the tests with specific tag, even if they have a tag that should run, using the not option: prefix the tag with --
.
Note this is the same as appending +-<tag to never run>
to each tag. May be useful with large number of tags.
If you want to run tests with tags @slow
or @regression
but without tag @smoke
--env grepTags='@slow @regression --@smoke'
which is equivalent to
--env grepTags='@slow+-@smoke @regression+-@smoke'
The tags are also applied to the "describe" blocks. In that case, the tests look up if any of their outer suites are enabled.
1describe('block with config tag', { tags: '@smoke' }, () => {})
# run any tests in the blocks having "@smoke" tag
--env grepTags=@smoke
# skip any blocks with "@smoke" tag
--env grepTags=-@smoke
See the cypress/integration/describe-tags-spec.js file.
Note: global function describe
and context
are aliases and both supported by this plugin.
Sometimes you want to run only the tests without any tags, and these tests are inside the describe blocks without any tags.
$ npx cypress run --env grepUntagged=true
You can check the current test's tags (including its parent suites) by checking the Cypress.env('testTags')
list
1describe('parent', { tags: ['@p1', '@p2'] }, () => { 2 describe('child', { tags: '@c1' }, () => { 3 it('has all effective test tags', { tags: '@t1' }, () => { 4 const tags = Cypress.env('testTags') 5 // includes tags from the parent suites and the test itself 6 expect(tags, 'tags').to.deep.equal(['@p1', '@p2', '@c1', '@t1']) 7 }) 8 }) 9})
By default, when using grep
and grepTags
all specs are executed, and inside each the filters are applied. This can be very wasteful, if only a few specs contain the grep
in the test titles. Thus when doing the positive grep
, you can pre-filter specs using the grepFilterSpecs=true
parameter.
# filter all specs first, and only run the ones with
# suite or test titles containing the string "it loads"
$ npx cypress run --env grep="it loads",grepFilterSpecs=true
# filter all specs files, only run the specs with a tag "@smoke"
$ npx cypress run --env grepTags=@smoke,grepFilterSpecs=true
Note 1: this requires installing this plugin in your project's plugin file, see the Install.
Note 2: the grepFilterSpecs
option is only compatible with the positive grep
and grepTags
options, not with the negative (inverted) "-..." filter.
Note 3: if there are no files remaining after filtering, the plugin prints a warning and leaves all files unchanged to avoid the test runner erroring with "No specs found".
Tip: you can set this environment variable in the config file file to enable it by default and skip using the environment variable:
1// config file 2{ 3 "e2e": { 4 "env": { 5 "grepFilterSpecs": true 6 } 7 } 8}
By default, all filtered tests are made pending using it.skip
method. If you want to completely omit them, pass the environment variable grepOmitFiltered=true
.
Pending filtered tests
cypress run --env grep="works 2"
Omit filtered tests
cypress run --env grep="works 2",grepOmitFiltered=true
Tip: you can set this environment variable in the config file (usually cypress.config.js
) file to enable it by default and skip using the environment variable:
1{ 2 "env": { 3 "grepOmitFiltered": true 4 } 5}
If you specify the grep
parameters the config file, you can disable it from the command line
$ npx cypress run --env grep=,grepTags=,burn=
You can burn the filtered tests to make sure they are flake-free
npx cypress run --env grep="hello world",burn=5
You can pass the number of times to run the tests via environment name burn
or grepBurn
or grep-burn
. Note, if a lot of tests match the grep and grep tags, a lot of tests will be burnt!
If you do not specify the "grep" or "grep tags" option, the "burn" will repeat every test.
Sometimes you want to pre-filter specs using tags AND then run extra specs without any filtering. You can set the list of specs / patterns using the grepExtraSpecs
env string. For example, to filter specs using tag @a
plus run the spec "b.cy.js":
npx cypress run --env grepTags=@a,grepExtraSpecs=cypress/e2e/b.cy.js
Sometimes you might want to run a test or a suite of tests only if a specific tag or tags are present. For example, you might have a test that cleans the data. This test is meant to run nightly, not on every test run. Thus you can set a required
tag:
1it('cleans up the data', { requiredTags: '@nightly' }, () => {...})
When you run the tests now, this test will be skipped, as if it were it.skip
. It will only run if you use the tag @nightly
, for example: npx cypress run --env grepTags=@nightly
.
If grepFilterSpecs=true
and a spec has only required tags, and you are running without any tags, the the spec will be skipped completely.
Read the blog posts 📝 Required Tags and Use Required Test Tags Instead Of Skipping Tests.
When grepping tests by title, the parent suite title is included. For example if this is the spec
1describe('users', () => { 2 it('works 1', () => {}) 3 it('works 2', () => {}) 4 it('works 3', () => {}) 5}) 6 7describe('projects', () => { 8 it('load 1', () => {}) 9 it('load 2', () => {}) 10 it('load 3', () => {}) 11})
You can run the tests inside the suite "projects" by using --env grep=projects
and you can skip the tests in the suite projects
by using --env grep=-projects
.
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:
1// @ts-ignore 2it('runs on deploy', { tags: 'smoke' }, () => { 3 ... 4})
This package comes with src/index.d.ts definition file that adds the property tags
to the Cypress test overrides interface. Include this file in your specs or TS config settings. For example, you can load it using a reference comment
1// cypress/integration/my-spec.js 2/// <reference types="@bahmutov/cy-grep" />
If you have tsconfig.json
file, add this library to the types list
1{ 2 "compilerOptions": { 3 "target": "es5", 4 "lib": ["es5", "dom"], 5 "types": ["cypress", "@bahmutov/cy-grep"] 6 }, 7 "include": ["**/*.ts"] 8}
Using test tags that start with @
is so common, you can enforce it using the env option grepPrefixAt: true
. This lets you use @tag1,@tag2, ...
or tag1,tag2, ...
when calling.
# use grepPrefixAt in your env settings object
# use { tags: '@tag1' } in your tests
# then these two are equivalent
--env grepTags=@tag1
--env grepTags=tag1
If the user selected spec(s) to run, then it might conflict with grepFilterSpecs=true
that filters the specs. There is no way to "know" if the user used --spec <...>
option when the plugin runs, see issues 33 and 26032. Thus if you use --spec pattern
, you need to pass it to the plugin via CYPRESS_grepSpec=pattern
or --env grepSpec=pattern
option.
cypress run --spec a.cy.js --env grepTags=...,grepSpec=a.cy.js
@
as tag prefix to make the tags searchable1// ✅ good practice 2describe('auth', { tags: '@critical' }, () => ...) 3it('works', { tags: '@smoke' }, () => ...) 4it('works quickly', { tags: ['@smoke', '@fast'] }, () => ...) 5 6// 🚨 NOT GOING TO WORK 7// ERROR: treated as a single tag, 8// probably want an array instead 9it('works', { tags: '@smoke @fast' }, () => ...)
Grepping the tests
1# run the tests by title 2$ npx cypress run --env grep="works quickly" 3# run all tests tagged @smoke 4$ npx cypress run --env grepTags=@smoke 5# run all tests except tagged @smoke 6$ npx cypress run --env grepTags=-@smoke 7# run all tests that have tag @fast but do not have tag @smoke 8$ npx cypress run --env grepTags=@fast+-@smoke
I would run all tests by default, and grep tests from the command line. For example, I could run the smoke tests first using grep plugin, and if the smoke tests pass, then run all the tests. See the video How I organize pull request workflows by running smoke tests first and its pull request workflow file.
You can set the grep string from the DevTools Console. This plugin adds method Cypress.grep
and Cypress.grepTags
to set the grep strings and restart the tests
1// filter tests by title substring 2Cypress.grep('hello world') 3// run filtered tests 100 times 4Cypress.grep('hello world', null, 100) 5// filter tests by tag string 6// in this case will run tests with tag @smoke OR @fast 7Cypress.grep(null, '@smoke @fast') 8// run tests tagged @smoke AND @fast 9Cypress.grep(null, '@smoke+@fast') 10// run tests with title containing "hello" and tag @smoke 11Cypress.grep('hello', '@smoke') 12// run tests with title containing "hello" and tag @smoke 10 times 13Cypress.grep('hello', '@smoke', 10)
Cypress.grep()
Once the tests finish, you can run just the failed tests from DevTools console
1> Cypress.grepFailed()
Tip: use Cypress.grep()
to reset and run all the tests
📝 Read the blog post Run Just The Failed Tests In Cypress.
When debugging a problem, first make sure you are using the expected version of this plugin, as some features might be only available in the later releases.
# get the plugin's version using NPM
$ npm ls @bahmutov/cy-grep
...
└── @bahmutov/cy-grep@1.1.0
# get the plugin's version using Yarn
$ yarn why @bahmutov/cy-grep
...
=> Found "@bahmutov/cy-grep@1.1.0"
info Has been hoisted to "@bahmutov/cy-grep"
info This module exists because it's specified in "devDependencies".
...
Second, make sure you are passing the values to the plugin correctly by inspecting the "Settings" tab in the Cypress Desktop GUI screen. You should see the values you have passed in the "Config" object under the env
property. For example, if I start the Test Runner with
1$ npx cypress open --env grep=works,grepFilterTests=true
Then I expect to see the grep string and the "filter tests" flag in the env
object.
This module uses debug to log verbose messages. You can enable the debug messages in the plugin file (runs when discovering specs to filter), and inside the browser to see how it determines which tests to run and to skip. When opening a new issue, please provide the debug logs from the plugin (if any) and from the browser.
Start Cypress with the environment variable DEBUG=cy-grep
. You will see a few messages from this plugin in the terminal output:
$ DEBUG=cy-grep npx cypress run --env grep=works,grepFilterSpecs=true
cy-grep: tests with "works" in their names
cy-grep: filtering specs using "works" in the title
cy-grep Cypress config env object: { grep: 'works', grepFilterSpecs: true }
...
cy-grep found 1 spec files +5ms
cy-grep [ 'spec.js' ] +0ms
cy-grep spec file spec.js +5ms
cy-grep suite and test names: [ 'hello world', 'works', 'works 2 @tag1',
'works 2 @tag1 @tag2', 'works @tag2' ] +0ms
cy-grep found "works" in 1 specs +0ms
cy-grep [ 'spec.js' ] +0ms
To enable debug console messages in the browser, from the DevTools console set localStorage.debug='cy-grep'
and run the tests again.
To see how to debug this plugin, watch the video Debug cypress-grep Plugin but use the string cy-grep
Many years ago I wrote a plugin cypress-grep
. When I left the company Cypress, I transferred that MIT-licensed plugin to the Cypress GitHub organization. They moved it to the Cypress monorepo and renamed the NPM module @cypress/grep
. I still use this grep plugin in some projects. When Cypress v10 was released, it broke some of the things in the plugin. Since I needed to fix it quickly and the monorepo setup is suboptimal, I forked the plugin back to my own repo bahmutov/cy-grep
(this repo) and released under NPM name @bahmutov/cy-grep
.
I plan to maintain the plugin @bahmutov/cy-grep
in the future, since I rely on it myself a lot.
Author: Gleb Bahmutov <gleb.bahmutov@gmail.com> © 2022
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
No vulnerabilities found.
No security vulnerabilities found.