Skip to content

Commit 336fa19

Browse files
authored
Cypress tags wrapper (#166)
* fix: make sure that when using 'not @tag', we do run features that do not have any tags * feat: a custom wrapper that respects tags * docs: info about the new wrapper
1 parent 325e98d commit 336fa19

File tree

4 files changed

+80
-53
lines changed

4 files changed

+80
-53
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,11 @@ Keep in mind we are using newer syntax, eg. `'not @foo and (@bar or @zap)'`.
237237
In order to initialize tests using tags you will have to run cypress and pass TAGS environment variable.
238238
239239
Example:
240-
```cypress run -e TAGS='not @foo and (@bar or @zap)'```
240+
```./node_modules/.bin/cypress-tags run -e TAGS='not @foo and (@bar or @zap)'```
241+
242+
Please note - we use our own cypress-tags wrapper to speed things up.
243+
For more details and examples please take a look to the example repo:
244+
[cypress-cucumber-example](https://github.com/TheBrainFamily/cypress-cucumber-example)
241245
242246
### Smart tagging
243247
Start your tests without setting any tags. And then put a @focus on the scenario (or scenarios) you want to focus on while development or bug fixing.

cypress-tags.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env node
2+
3+
const { Parser } = require("gherkin");
4+
const glob = require("glob");
5+
const fs = require("fs");
6+
const { execFileSync } = require("child_process");
7+
8+
const { shouldProceedCurrentStep } = require("./lib/tagsHelper");
9+
10+
// TODO currently we only work with feature files in cypress/integration folder.
11+
// It should be easy to base this on the cypress.json configuration - we are happy to take a PR
12+
// here if you need this functionality!
13+
14+
const paths = glob.sync("cypress/integration/**/*.feature");
15+
16+
const featuresToRun = [];
17+
18+
const debug = (message, ...rest) =>
19+
process.env.DEBUG
20+
? console.log(`DEBUG: ${message}`, rest.length ? rest : "")
21+
: null;
22+
23+
const found = process.argv.slice(2).find(arg => arg.indexOf("TAGS=") === 0);
24+
25+
const envTags = found.replace(/.*=/, "");
26+
debug("Found tag expression", envTags);
27+
28+
paths.forEach(featurePath => {
29+
const spec = `${fs.readFileSync(featurePath)}`;
30+
const parsedFeature = new Parser().parse(spec);
31+
32+
const featureTags = parsedFeature.feature.tags;
33+
const featureShouldRun = shouldProceedCurrentStep(featureTags, envTags);
34+
const taggedScenarioShouldRun = parsedFeature.feature.children.some(
35+
section =>
36+
section.tags &&
37+
section.tags.length &&
38+
shouldProceedCurrentStep(section.tags.concat(featureTags), envTags)
39+
);
40+
debug(
41+
`Feature: ${featurePath}, featureShouldRun: ${featureShouldRun}, taggedScenarioShouldRun: ${taggedScenarioShouldRun}`
42+
);
43+
if (featureShouldRun || taggedScenarioShouldRun) {
44+
featuresToRun.push(featurePath);
45+
}
46+
});
47+
48+
try {
49+
execFileSync(
50+
`${__dirname}/node_modules/.bin/cypress`,
51+
[...process.argv.slice(2), "--spec", featuresToRun.join(",")],
52+
{
53+
stdio: [process.stdin, process.stdout, process.stderr]
54+
}
55+
);
56+
} catch (e) {
57+
debug("Error while running cypress (or just a test failure)", e);
58+
process.exit(1);
59+
}

package-lock.json

Lines changed: 13 additions & 52 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
"fixStyle": "eslint . --fix",
1010
"semantic-release": "semantic-release"
1111
},
12+
"bin": {
13+
"cypress-tags": "cypress-tags.js"
14+
},
1215
"husky": {
1316
"hooks": {
1417
"pre-commit": "lint-staged && jest"

0 commit comments

Comments
 (0)