cypress-grep

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.
require('cypress-grep')()
optional: load and register this module from the plugin file
module.exports = (on, config) => {
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:
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'
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.