Skip to content

Commit

Permalink
fix: grep specs looking for the required tag (#16)
Browse files Browse the repository at this point in the history
* add example with required tags only

* fix: required tag spec grep
  • Loading branch information
bahmutov authored Jan 24, 2023
1 parent 77a2d28 commit d0e34e4
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 1 deletion.
20 changes: 20 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,25 @@ jobs:
--env grepTags=@special \
--expect-exactly ./tests/required-tags/expect-with-required-tag.json
test-required-tags-only:
runs-on: ubuntu-20.04
steps:
- name: Checkout 🛎
uses: actions/checkout@v3

- name: Install Cypress 🧪
uses: cypress-io/github-action@v5
with:
runTests: false

- name: running the tests WITH the scrape required tag
# TODO: add expected test statuses
run: |
npx cypress-expect run \
--project tests/required-tags-only \
--env grepTags=@scrape \
--expect-exactly ./tests/required-tags-only/expect-scrape.json
release:
needs:
[
Expand All @@ -86,6 +105,7 @@ jobs:
test-one-spec-filter,
test-effective-tags-AND-filter,
test-required-tags,
test-required-tags-only,
]
runs-on: ubuntu-20.04
if: github.ref == 'refs/heads/main'
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ registerCypressGrep()

Installing the plugin via `setupNodeEvents()` is required to enable the [grepFilterSpecs](#pre-filter-specs-grepfilterspecs) feature.

**Tip:** you probably want to set these `env` settings in your config file

```js
module.exports = defineConfig({
env: { grepFilterSpecs: true, grepOmitFiltered: true },
...
})
```

## Usage Overview

You can filter tests to run using part of their title via `grep`, and via explicit tags via `grepTags` Cypress environment variables.
Expand Down
9 changes: 8 additions & 1 deletion src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,14 @@ function cypressGrepPlugin(config) {
debug('effective test tags %o', testTags)
return Object.keys(testTags).some((testTitle) => {
const effectiveTags = testTags[testTitle].effectiveTags
return shouldTestRun(parsedGrep, null, effectiveTags)
const requiredTags = testTags[testTitle].requiredTags
return shouldTestRun(
parsedGrep,
null,
effectiveTags,
false,
requiredTags,
)
})
} catch (err) {
console.error('Could not determine test names in file: %s', specFile)
Expand Down
16 changes: 16 additions & 0 deletions tests/required-tags-only/cypress.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const { defineConfig } = require('cypress')

module.exports = defineConfig({
env: { grepFilterSpecs: true, grepOmitFiltered: true },
e2e: {
defaultCommandTimeout: 1000,
setupNodeEvents(on, config) {
require('../../src/plugin')(config)
return config
},
supportFile: false,
specPattern: 'e2e/*.cy.js',
},
fixturesFolder: false,
video: false,
})
8 changes: 8 additions & 0 deletions tests/required-tags-only/e2e/clean.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// <reference types="@bahmutov/cy-grep" />

describe('clean', () => {
it('old data', { requiredTags: '@nightly' }, () => {
// pretend we are deleting old test data
cy.wait(5000)
})
})
8 changes: 8 additions & 0 deletions tests/required-tags-only/e2e/scrape.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// <reference path="../../../src/index.d.ts" />

describe('scrape', () => {
it('scrapes the blog', { requiredTags: '@scrape' }, () => {
// pretend we are scraping the blog posts
cy.wait(5000)
})
})
7 changes: 7 additions & 0 deletions tests/required-tags-only/e2e/spec-a.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/// <reference types="cypress" />

describe('spec A', () => {
it('works', () => {})

it('works some more', () => {})
})
7 changes: 7 additions & 0 deletions tests/required-tags-only/e2e/spec-b.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/// <reference types="cypress" />

describe('spec B', () => {
it('works', () => {})

it('works some more', () => {})
})
5 changes: 5 additions & 0 deletions tests/required-tags-only/expect-scrape.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"scrape": {
"scrapes the blog": "pass"
}
}

0 comments on commit d0e34e4

Please sign in to comment.