Skip to content
This repository has been archived by the owner on Mar 29, 2023. It is now read-only.

Commit

Permalink
feat: grep untagged tests to run (#78)
Browse files Browse the repository at this point in the history
* start working on grep untagged

* feat: implement untagged

* update the readme
  • Loading branch information
bahmutov committed Oct 28, 2021
1 parent f16ffd5 commit 514dd11
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 14 deletions.
16 changes: 15 additions & 1 deletion .github/workflows/ci.yml
Expand Up @@ -225,7 +225,21 @@ jobs:
npx cypress-expect \
--config testFiles="**/*.js" \
--env grep="outside any suites",grepFilterSpecs=true \
--expect expects/grep-filter-specs.json
--expect-exactly expects/grep-filter-specs.json
- name: run untagged tests 🧪
run: |
npx cypress-expect \
--config testFiles="**/spec.js" \
--env grepUntagged=true \
--expect-exactly expects/grep-untagged.json
- name: run untagged tests in blocks 🧪
run: |
npx cypress-expect \
--config testFiles="**/describe-tags-spec.js" \
--env grepUntagged=true \
--expect-exactly expects/describe-tags-spec-untagged.json
- name: Semantic Release 🚀
uses: cycjimmy/semantic-release-action@v2
Expand Down
13 changes: 13 additions & 0 deletions README.md
Expand Up @@ -81,6 +81,11 @@ $ npx cypress run --env grepTags=@fast
# run only the tests tagged "smoke"
# that have "login" in their titles
$ npx cypress run --env grep=login,grepTags=smoke
# only run the specs that have any tests with "user" in their titles
$ npx cypress run --env grep=user,grepFilterSpecs=true
# run only tests that do not have any tags
# and are not inside suites that have any tags
$ npx cypress run --env grepUntagged=true
```

## Videos
Expand Down Expand Up @@ -192,6 +197,14 @@ Note: this requires installing this plugin in your project's plugin file, see th

Note 2: the `grepFilterSpecs` option is only compatible with the `grep` option, and not with `grepTags` option.

### grep untagged tests

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
```

### 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:
Expand Down
14 changes: 14 additions & 0 deletions expects/describe-tags-spec-untagged.json
@@ -0,0 +1,14 @@
{
"block with no tags": {
"inside describe 1": "passing",
"inside describe 2": "passing"
},
"block with tag smoke": {
"inside describe 3": "pending",
"inside describe 4": "pending"
},
"block without any tags": {
"test with tag smoke": "pending"
},
"is a test outside any suites": "passing"
}
7 changes: 7 additions & 0 deletions expects/grep-untagged.json
@@ -0,0 +1,7 @@
{
"hello world": "passing",
"works": "passing",
"works 2 @tag1": "pending",
"works 2 @tag1 @tag2": "pending",
"works @tag2": "pending"
}
33 changes: 25 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -18,7 +18,7 @@
"devDependencies": {
"cypress": "8.7.0",
"cypress-each": "^1.5.0",
"cypress-expect": "2.4.1",
"cypress-expect": "2.5.0",
"prettier": "2.3.1",
"semantic-release": "17.4.3"
},
Expand Down
2 changes: 2 additions & 0 deletions src/index.d.ts
@@ -1,3 +1,5 @@
/// <reference types="cypress" />

declare namespace Cypress {
// specify additional properties in the TestConfig object
// in our case we will add "tags" property
Expand Down
5 changes: 5 additions & 0 deletions src/plugin.js
Expand Up @@ -30,6 +30,11 @@ function cypressGrepPlugin(config) {
console.log('cypress-grep: running filtered tests %d times', grepBurn)
}

const grepUntagged = config.env.grepUntagged || config.env['grep-untagged']
if (grepUntagged) {
console.log('cypress-grep: running untagged tests')
}

const grepFilterSpecs = config.env.grepFilterSpecs === true
if (grepFilterSpecs && grep) {
console.log('cypress-grep: filtering specs using "%s" in the title', grep)
Expand Down
15 changes: 12 additions & 3 deletions src/support.js
Expand Up @@ -26,7 +26,10 @@ function cypressGrep() {
const burnSpecified =
Cypress.env('grepBurn') || Cypress.env('grep-burn') || Cypress.env('burn')

if (!grep && !grepTags && !burnSpecified) {
const grepUntagged =
Cypress.env('grepUntagged') || Cypress.env('grep-untagged')

if (!grep && !grepTags && !burnSpecified && !grepUntagged) {
// nothing to do, the user has no specified the "grep" string
debug('Nothing to grep')
return
Expand Down Expand Up @@ -78,8 +81,14 @@ function cypressGrep() {
const tagsToGrep = suiteStack
.flatMap((item) => item.tags)
.concat(configTags)

const shouldRun = shouldTestRun(parsedGrep, nameToGrep, tagsToGrep)
.filter(Boolean)

const shouldRun = shouldTestRun(
parsedGrep,
nameToGrep,
tagsToGrep,
grepUntagged,
)

if (tagsToGrep && tagsToGrep.length) {
debug(
Expand Down
6 changes: 5 additions & 1 deletion src/utils.js
Expand Up @@ -125,7 +125,11 @@ function shouldTestRunTitle(parsedGrep, testName) {
}

// note: tags take precedence over the test name
function shouldTestRun(parsedGrep, testName, tags = []) {
function shouldTestRun(parsedGrep, testName, tags = [], grepUntagged = false) {
if (grepUntagged) {
return !tags.length
}

if (Array.isArray(testName)) {
// the caller passed tags only, no test name
tags = testName
Expand Down

0 comments on commit 514dd11

Please sign in to comment.