Skip to content
This repository has been archived by the owner on May 28, 2021. It is now read-only.

Commit

Permalink
feat: Add invert option (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
untidy-hair authored and bahmutov committed Jan 28, 2020
1 parent 4edcd49 commit 458e9fa
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 16 deletions.
6 changes: 5 additions & 1 deletion README.md
Expand Up @@ -24,7 +24,7 @@ npm install --save-dev cypress-select-tests

## Mocha-like selection

[Mocha](https://mochajs.org/) has `--fgrep` and `--grep` CLI arguments to select spec files and tests to run. This package provides imitation using strings. In your `cypress/plugins/index.js` use:
[Mocha](https://mochajs.org/) has `--fgrep`, `--grep` and `--invert` CLI arguments to select spec files and tests to run. This package provides imitation using strings. In your `cypress/plugins/index.js` use:

```js
const selectTestsWithGrep = require('cypress-select-tests/grep')
Expand All @@ -44,6 +44,10 @@ Then open or run Cypress and use environment variables to pass strings to find.
$ npx cypress run --env fgrep=foo,grep=works
## runs tests with "feature A" in the title
$ npx cypress run --env grep='feature A'
## runs only specs NOT with "foo" in their filename
$ npx cypress run --env fgrep=foo,invert=true
## runs tests NOT with "feature A" in the title
$ npx cypress run --env grep='feature A',invert=true
```

The test picking function is available by itself in [src/grep-pick-tests.js](src/grep-pick-tests.js) file.
Expand Down
120 changes: 120 additions & 0 deletions __snapshots__/spec.js
Expand Up @@ -211,3 +211,123 @@ exports['combines custom browserify with grep picker 2'] = {
}
]
}

exports['runs tests except selected files with fgrep invert 1'] = [
{
"stats": {
"suites": 1,
"tests": 1,
"passes": 0,
"pending": 1,
"skipped": 0,
"failures": 0
},
"spec": {
"name": "spec-2.js",
"relative": "cypress/integration/spec-2.js"
},
"tests": [
{
"title": [
"Second spec",
"works"
],
"state": "pending"
}
]
},
{
"stats": {
"suites": 2,
"tests": 4,
"passes": 4,
"pending": 0,
"skipped": 0,
"failures": 0
},
"spec": {
"name": "spec.js",
"relative": "cypress/integration/spec.js"
},
"tests": [
{
"title": [
"Example tests",
"works"
],
"state": "passed"
},
{
"title": [
"Example tests",
"nested",
"does A"
],
"state": "passed"
},
{
"title": [
"Example tests",
"nested",
"does B"
],
"state": "passed"
},
{
"title": [
"Example tests",
"nested",
"does C"
],
"state": "passed"
}
]
}
]

exports['runs tests without "does" in their name from spec.js with grep invert 1'] = {
"main stats": {
"suites": 2,
"tests": 4,
"passes": 1,
"pending": 3,
"skipped": 0,
"failures": 0
}
}

exports['runs tests without "does" in their name from spec.js with grep invert 2'] = {
"test state": [
{
"title": [
"Example tests",
"works"
],
"state": "passed"
},
{
"title": [
"Example tests",
"nested",
"does A"
],
"state": "pending"
},
{
"title": [
"Example tests",
"nested",
"does B"
],
"state": "pending"
},
{
"title": [
"Example tests",
"nested",
"does C"
],
"state": "pending"
}
]
}
50 changes: 35 additions & 15 deletions src/grep-pick-tests.js
Expand Up @@ -9,31 +9,51 @@ const grepPickTests = (filename, foundTests, cypressConfig) => {

// we could use Cypress env variables to use same options as Mocha
// see https://mochajs.org/
// --fgrep, -f Only run tests containing this string
// --grep, -g Only run tests matching this string or regexp

// --fgrep, -f Only run tests containing this string [string]
// --grep, -g Only run tests matching this string or regexp [string]
// --invert, -i Inverts --grep and --fgrep matches [boolean]
// for example, only leave tests where the test name is "works"
// return foundTests.filter(testName => R.last(testName) === 'works')

const fgrep = cypressConfig.env.fgrep
const grep = cypressConfig.env.grep // assume string for now, not regexp
const invert = cypressConfig.env.invert

if (fgrep) {
console.log('\tJust tests with a name that contains: %s', fgrep)
if (!filename.includes(fgrep)) {
console.warn(
'\tTest filename %s did not match fgrep "%s"',
filename,
fgrep
)
return
if (invert) {
console.log('\tJust tests with a name that does not contain: %s', fgrep)
if (filename.includes(fgrep)) {
console.warn(
'\tTest filename %s matched fgrep "%s"',
filename,
fgrep
)
return
}
} else {
console.log('\tJust tests with a name that contains: %s', fgrep)
if (!filename.includes(fgrep)) {
console.warn(
'\tTest filename %s did not match fgrep "%s"',
filename,
fgrep
)
return
}
}
}
if (grep) {
console.log('\tJust tests tagged with: %s', grep)
return foundTests.filter(testName =>
testName.some(part => part && part.includes(grep))
)
if (invert) {
console.log('\tJust tests not tagged with: %s', grep)
return foundTests.filter(testName =>
!testName.some(part => part && part.includes(grep))
)
} else {
console.log('\tJust tests tagged with: %s', grep)
return foundTests.filter(testName =>
testName.some(part => part && part.includes(grep))
)
}
}

return foundTests
Expand Down
55 changes: 55 additions & 0 deletions test/spec.js
Expand Up @@ -52,6 +52,37 @@ it('runs only tests with "does" in their name from spec.js', () => {
})
})

it('runs tests without "does" in their name from spec.js with grep invert', () => {
return cypress
.run({
env: {
grep: 'does',
invert: 'true'
},
config: {
video: false,
videoUploadOnPasses: false,
pluginsFile: path.join(__dirname, 'plugin-does-grep.js')
},
spec: 'cypress/integration/spec.js'
})
.then(R.prop('runs'))
.then(runs => {
la(runs.length === 1, 'expected single run', runs)
return runs[0]
})
.then(run => {
// 1 pass without "does", 3 pending with "does"
snapshot({
'main stats': pickMainStatsFromRun(run)
})

snapshot({
'test state': pickTestInfo(run)
})
})
})

it('runs no tests', () => {
return cypress
.run({
Expand Down Expand Up @@ -100,6 +131,30 @@ it('only runs tests in spec-2', () => {
})
})

it('runs tests except selected files with fgrep invert', () => {
return cypress
.run({
env: {
fgrep: '-2',
invert: 'true'
},
config: {
video: false,
videoUploadOnPasses: false,
pluginsFile: path.join(__dirname, 'plugin-does-grep.js')
},
spec: 'cypress/integration/*'
})
.then(R.prop('runs'))
.then(runs => {
la(runs.length === 2, 'expected two specs', runs)

const info = R.map(pickRunInfo, runs)
// only tests from cypress/integration/spec.js should run
snapshot(info)
})
})

it('combines custom browserify with grep picker', () => {
return cypress
.run({
Expand Down

0 comments on commit 458e9fa

Please sign in to comment.