Skip to content

Commit 9b2e907

Browse files
committed
feat: cleanup and improvement for the significantly faster testrun for all tests
closes #189
1 parent 3bd07a5 commit 9b2e907

File tree

10 files changed

+88
-66
lines changed

10 files changed

+88
-66
lines changed

circle.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ jobs:
1717
- run:
1818
name: run tests
1919
command: npm test
20+
- run:
21+
name: slow test run, for sanity
22+
command: npm run test:each
2023
- run:
2124
name: release
2225
command: npm run semantic-release || true

cypress/integration/All.features

Whitespace-only changes.

cypress/integration/AndAndButSteps.feature

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
Feature: Using And and but
2-
Scenario: With an And everything is find
1+
Feature: Using And and But
2+
Scenario: With an And everything is fine
33
Given I do something
44
And Something else
55
Then I happily work
66

7-
Scenario: With a But also the step definition
7+
Scenario: With a But
88
Given I dont do something
99
And it is sunday
1010
Then I stream on twitch

cypress/integration/UsingAnd.feature

Lines changed: 0 additions & 5 deletions
This file was deleted.

cypress/support/step_definitions/usingAnd.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

lib/cucumberTemplate.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
exports.cucumberTemplate = `
2+
const {
3+
resolveAndRunStepDefinition,
4+
defineParameterType,
5+
given,
6+
when,
7+
then,
8+
and,
9+
but,
10+
Before,
11+
After,
12+
defineStep
13+
} = require("cypress-cucumber-preprocessor/lib/resolveStepDefinition");
14+
const Given = (window.Given = window.given = given);
15+
const When = (window.When = window.when = when);
16+
const Then = (window.Then = window.then = then);
17+
const And = (window.And = window.and = and);
18+
const But = (window.But = window.but = but);
19+
window.defineParameterType = defineParameterType;
20+
window.defineStep = defineStep;
21+
const {
22+
createTestsFromFeature
23+
} = require("cypress-cucumber-preprocessor/lib/createTestsFromFeature");
24+
`;

lib/featuresLoader.js

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
1-
/* eslint-disable no-eval */
21
const glob = require("glob");
32
const path = require("path");
43
const fs = require("fs");
4+
const { Parser } = require("gherkin");
55
const log = require("debug")("cypress:cucumber");
6+
67
const { getStepDefinitionsPaths } = require("./getStepDefinitionsPaths");
8+
const { cucumberTemplate } = require("./cucumberTemplate");
9+
const { getCucumberJsonConfig } = require("./getCucumberJsonConfig");
710

811
// This is the template for the file that we will send back to cypress instead of the text of a
912
// feature file
10-
const createCucumber = (specs, toRequire) =>
13+
const createCucumber = (specs, toRequire, cucumberJson) =>
1114
`
12-
const {resolveAndRunStepDefinition, defineParameterType, given, when, then, and, but, defineStep} = require('cypress-cucumber-preprocessor/lib/resolveStepDefinition');
13-
const Given = window.Given = window.given = given;
14-
const When = window.When = window.when = when;
15-
const Then = window.Then = window.then = then;
16-
const And = window.And = window.and = and;
17-
const But = window.But = window.but = but;
18-
window.defineParameterType = defineParameterType;
19-
window.defineStep = defineStep;
20-
21-
const { createTestsFromFeature } = require('cypress-cucumber-preprocessor/lib/createTestsFromFeature');
22-
${eval(toRequire).join("\n")}
23-
const {Parser, Compiler} = require('gherkin');
15+
${cucumberTemplate}
16+
17+
window.cucumberJson = ${JSON.stringify(cucumberJson)};
18+
19+
${toRequire.join("\n")}
2420
2521
${specs
26-
.map(spec => `createTestsFromFeature(new Parser().parse(\`${spec}\`))`)
22+
.map(
23+
({ spec, filePath, name }) =>
24+
`
25+
describe(\`${name}\`, function() {
26+
createTestsFromFeature('${filePath}', \`${spec}\`);
27+
})
28+
`
29+
)
2730
.join("\n")}
2831
`;
2932

@@ -46,9 +49,20 @@ module.exports = function(_, filePath) {
4649
)
4750
];
4851

49-
const specs = features.map(featurePath =>
50-
fs.readFileSync(featurePath).toString()
51-
);
52+
const specs = features
53+
.map(featurePath => ({
54+
spec: fs.readFileSync(featurePath).toString(),
55+
filePath: path.basename(featurePath)
56+
}))
57+
.map(feature =>
58+
Object.assign({}, feature, {
59+
name: new Parser().parse(feature.spec.toString()).feature.name
60+
})
61+
);
5262

53-
return createCucumber(specs, stepDefinitionsToRequire);
63+
return createCucumber(
64+
specs,
65+
stepDefinitionsToRequire,
66+
getCucumberJsonConfig()
67+
);
5468
};

lib/getCucumberJsonConfig.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const cosmiconfig = require("cosmiconfig");
2+
const log = require("debug")("cypress:cucumber");
3+
4+
exports.getCucumberJsonConfig = () => {
5+
const explorer = cosmiconfig("cypress-cucumber-preprocessor", { sync: true });
6+
const loaded = explorer.load();
7+
8+
const cucumberJson =
9+
loaded && loaded.config && loaded.config.cucumberJson
10+
? loaded.config.cucumberJson
11+
: { generate: false };
12+
log("cucumber.json", JSON.stringify(cucumberJson));
13+
14+
return cucumberJson;
15+
};

lib/loader.js

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,33 @@
1-
/* eslint-disable no-eval */
21
const log = require("debug")("cypress:cucumber");
32
const path = require("path");
4-
const cosmiconfig = require("cosmiconfig");
53
const { Parser } = require("gherkin");
64
const { getStepDefinitionsPaths } = require("./getStepDefinitionsPaths");
7-
5+
const { cucumberTemplate } = require("./cucumberTemplate");
6+
const { getCucumberJsonConfig } = require("./getCucumberJsonConfig");
87
// This is the template for the file that we will send back to cypress instead of the text of a
98
// feature file
109
const createCucumber = (filePath, cucumberJson, spec, toRequire, name) =>
1110
`
12-
const {resolveAndRunStepDefinition, defineParameterType, given, when, then, and, but, Before, After, defineStep} = require('cypress-cucumber-preprocessor/lib/resolveStepDefinition');
13-
const Given = window.Given = window.given = given;
14-
const When = window.When = window.when = when;
15-
const Then = window.Then = window.then = then;
16-
const And = window.And = window.and = and;
17-
const But = window.But = window.but = but;
18-
window.defineParameterType = defineParameterType;
19-
window.defineStep = defineStep;
20-
window.cucumberJson = ${JSON.stringify(cucumberJson)};
21-
const { createTestsFromFeature } = require('cypress-cucumber-preprocessor/lib/createTestsFromFeature');
11+
${cucumberTemplate}
2212
13+
window.cucumberJson = ${JSON.stringify(cucumberJson)};
2314
describe(\`${name}\`, function() {
24-
${eval(toRequire).join("\n")}
15+
${toRequire.join("\n")}
2516
createTestsFromFeature('${filePath}', \`${spec}\`);
2617
});
2718
`;
2819

2920
// eslint-disable-next-line func-names
3021
module.exports = function(spec, filePath = this.resourcePath) {
31-
const explorer = cosmiconfig("cypress-cucumber-preprocessor", { sync: true });
32-
const loaded = explorer.load();
33-
const cucumberJson =
34-
loaded && loaded.config && loaded.config.cucumberJson
35-
? loaded.config.cucumberJson
36-
: { generate: false };
37-
3822
log("compiling", spec);
39-
log("cucumber.json", JSON.stringify(cucumberJson));
4023
const stepDefinitionsToRequire = getStepDefinitionsPaths(filePath).map(
4124
sdPath => `require('${sdPath}')`
4225
);
26+
4327
const { name } = new Parser().parse(spec.toString()).feature;
4428
return createCucumber(
4529
path.basename(filePath),
46-
cucumberJson,
30+
getCucumberJsonConfig(),
4731
spec,
4832
stepDefinitionsToRequire,
4933
name

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"description": "Run gherkin-syntaxed specs with cypress.io",
55
"main": "lib/index.js",
66
"scripts": {
7-
"test": "eslint . && jest && cypress run",
7+
"test": "eslint . && jest && npm run test:all",
8+
"test:all": "cypress run --spec \"**/*.features\"",
9+
"test:each": "cypress run --spec \"**/*.feature\"",
810
"test:debug": "jest && DEBUG=cypress:* cypress open\n",
911
"fixStyle": "eslint . --fix",
1012
"semantic-release": "semantic-release"

0 commit comments

Comments
 (0)