Skip to content

Commit 11367ec

Browse files
authored
Add mocha like grep (#250)
* added Mocha-like grep example * update readme * add new recipe to circle * add new recipe to the root readme * add env variables for fgrep and grep * rename tests to have admin * update example readme * tweak readme
1 parent 054f82b commit 11367ec

File tree

12 files changed

+2542
-143
lines changed

12 files changed

+2542
-143
lines changed

Diff for: README.md

+6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Recipe | Category | Description
1616
[Hover and Hidden Elements](#hover-and-hidden-elements) | Testing the DOM | Test hidden elements requiring hover
1717
[Form Interactions](#form-interactions) | Testing the DOM | Test form elements like input type `range`
1818
[Drag and Drop](#drag-and-drop) | Testing the DOM | Use `.trigger()` to test drag and drop
19+
[grep](#grep) | Preprocessors | Filter tests by name using Mocha-like `grep` syntax
1920
[Typescript with Browserify](#typescript-with-browserify) | Preprocessors | Add typescript support with browserify
2021
[Typescript with Webpack](#typescript-with-webpack) | Preprocessors | Add typescript support with webpack
2122
[Application Actions](#application-actions) | Blogs | Application actions are a replacement for Page Objects
@@ -149,6 +150,11 @@ Get around the lack of a `.hover()` command.
149150
- Use [`.trigger()`](https://on.cypress.io/trigger) to test drag-n-drop that uses mouse events.
150151
- Use [`.trigger()`](https://on.cypress.io/trigger) to test drag-n-drop that uses drag events.
151152

153+
### [grep](./examples/preprocessors__grep)
154+
155+
- Use Mocha-like syntax to select tests to run
156+
- Implemented as a preprocessor [cypress-select-tests](https://github.com/bahmutov/cypress-select-tests)
157+
152158
### [Typescript with Browserify](./examples/preprocessors__typescript-browserify)
153159

154160
- Use [`@cypress/browserify-preprocessor`](https://github.com/cypress-io/cypress-browserify-preprocessor) to write Cypress tests in Typescript

Diff for: circle.yml

+10
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ jobs:
111111
<<: *defaults
112112
logging-in__xhr-web-forms:
113113
<<: *defaults
114+
preprocessors__grep:
115+
<<: *defaults
116+
# in this particular example we want to grep tests
117+
# and run only tests with name having "@admin"
118+
environment:
119+
CYPRESS_grep: '@admin'
114120
preprocessors__typescript-browserify:
115121
<<: *defaults
116122
preprocessors__typescript-webpack:
@@ -208,6 +214,9 @@ workflows:
208214
- logging-in__xhr-web-forms:
209215
requires:
210216
- build
217+
- preprocessors__grep:
218+
requires:
219+
- build
211220
- preprocessors__typescript-browserify:
212221
requires:
213222
- build
@@ -277,6 +286,7 @@ workflows:
277286
- logging-in__html-web-forms
278287
- logging-in__single-sign-on
279288
- logging-in__xhr-web-forms
289+
- preprocessors__grep
280290
- preprocessors__typescript-browserify
281291
- preprocessors__typescript-webpack
282292
- server-communication__bootstrapping-your-app

Diff for: examples/preprocessors__grep/README.md

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Selecting tests using Mocha-like "grep"
2+
3+
This example uses [cypress-select-tests](https://github.com/bahmutov/cypress-select-tests) preprocessor plugin to filter tests, similar to how [Mocha](https://mochajs.org/) has the `--grep` CLI argument. This project provides an imitation using a string (no regular expressions).
4+
5+
Because Cypress ignores unknown CLI parameters, you need to pass `grep` argument as an environment variables, for example by using `--env` CLI argument.
6+
7+
## Examples
8+
9+
### interactive
10+
11+
Open Cypress GUI but only allow running tests with `@admin` in the name
12+
13+
```bash
14+
$ npm run cypress:open -- --env grep=@admin
15+
```
16+
17+
shows only "@admin" tests were executed, while the rest were skipped
18+
19+
![Running all tests with admin](images/grep-admin.png)
20+
21+
### headless
22+
23+
Run tests with `edge case` in the name in headless mode
24+
25+
```bash
26+
$ npm run cypress:run -- --env grep='edge case'
27+
```
28+
29+
produces
30+
31+
```text
32+
Running: feature-a.js... (1 of 2)
33+
picking tests to run in file cypress/integration/feature-a.js
34+
35+
36+
feature A
37+
- works
38+
- reports for @admin
39+
40+
41+
0 passing (30ms)
42+
2 pending
43+
44+
Running: feature-b.js... (2 of 2)
45+
picking tests to run in file cypress/integration/feature-b.js
46+
47+
48+
feature B
49+
- works
50+
✓ an edge case (54ms)
51+
- works for @admin
52+
53+
54+
1 passing (87ms)
55+
2 pending
56+
```
57+
58+
## Notes
59+
60+
### test title
61+
62+
The full test title used for string matching is a concatenation of suite names plus the test's own name.
63+
64+
```js
65+
describe('app', () => {
66+
context('feature A', () => {
67+
it('works', () => {
68+
// full test name is 'app feature A works'
69+
})
70+
})
71+
})
72+
```
73+
74+
Thus you can run all tests form the given suite
75+
76+
```bash
77+
$ npm run cypress:run -- --env grep='feature A'
78+
```
79+
80+
## NPM arguments
81+
82+
Note how you need to use `--` to separate NPM open or run script command from arguments to Cypress itself.
83+
84+
```bash
85+
# runs only tests with "works" in their name
86+
$ npm run cypress:run -- --env grep=works
87+
```
88+
89+
## Quote strings with spaces
90+
91+
If you want to pass string with spaces, please quote it like
92+
93+
```bash
94+
$ npm run cypress:run -- --env grep='feature A'
95+
```
96+
97+
## Details
98+
99+
See [cypress/plugins/index.js](cypress/plugins/index.js) file how to configure test selection preprocessor, built on top of [Cypress Browserify preprocessor](https://github.com/cypress-io/cypress-browserify-preprocessor).

Diff for: examples/preprocessors__grep/cypress.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
describe('feature A', () => {
2+
it('works', () => {})
3+
4+
it('reports for @admin', () => {})
5+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
describe('feature B', () => {
2+
it('works', () => {})
3+
4+
it('an edge case', () => {})
5+
6+
it('works for @admin', () => {})
7+
})
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const selectTestsWithGrep = require('cypress-select-tests/grep')
2+
module.exports = (on, config) => {
3+
on('file:preprocessor', selectTestsWithGrep(config))
4+
}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// no additional commands

Diff for: examples/preprocessors__grep/images/grep-admin.png

45.8 KB
Loading

Diff for: examples/preprocessors__grep/package.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "cypress-example-grep",
3+
"version": "1.0.0",
4+
"description": "Selecting specs and tests to run using grep",
5+
"scripts": {
6+
"start": "echo",
7+
"cypress:run": "../../node_modules/.bin/cypress run",
8+
"cypress:open": "../../node_modules/.bin/cypress open"
9+
}
10+
}

0 commit comments

Comments
 (0)