Skip to content

Commit

Permalink
fix(cypress): Merge all steps of a scenario into a single test. (#44)
Browse files Browse the repository at this point in the history
* fix(cypress): Fix state clearing between each state

BREAKING CHANGE: each scenario is implemented as a single test now. We lose the ability to know exactly which step fails within a scenario

* test: Fix lint errors and tests

* fix(cypress): Transform background sections into beforeEach blocks

* fix(cypress): Add an index to tests with examples

* chore(package): Remove mocha-steps

It has become useless since we don't execute steps as individual tests anymore. It will become useful again when cypress allows to control state clearing and we can get steps as tests again.
  • Loading branch information
BenoitAverty authored and lgandecki committed Apr 19, 2018
1 parent ac63069 commit 92b5d77
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 37 deletions.
52 changes: 27 additions & 25 deletions createTestFromScenario.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
/* eslint-disable prefer-template */
/* global step */

require("mocha-steps");
const { resolveAndRunStepDefinition } = require("./resolveStepDefinition");

const stepTest = stepDetails =>
step(`${stepDetails.keyword} ${stepDetails.text}`, () => {
resolveAndRunStepDefinition(stepDetails);
});
const stepTest = stepDetails => {
cy.log(`${stepDetails.keyword} ${stepDetails.text}`);
resolveAndRunStepDefinition(stepDetails);
};

const createTestFromScenario = scenario => {
describe(scenario.name, () => {
if (scenario.examples) {
scenario.examples.forEach(example => {
const exampleValues = [];
const createTestFromScenario = (scenario, backgroundSection) => {
if (scenario.examples) {
scenario.examples.forEach(example => {
const exampleValues = [];

example.tableBody.forEach((row, rowIndex) => {
example.tableHeader.cells.forEach((header, headerIndex) => {
exampleValues[rowIndex] = Object.assign(
{},
exampleValues[rowIndex],
{
[header.value]: row.cells[headerIndex].value
}
);
example.tableBody.forEach((row, rowIndex) => {
example.tableHeader.cells.forEach((header, headerIndex) => {
exampleValues[rowIndex] = Object.assign({}, exampleValues[rowIndex], {
[header.value]: row.cells[headerIndex].value
});
});
});

exampleValues.forEach((_, index) => {
it(`${scenario.name} (example #${index + 1})`, () => {
if (backgroundSection) {
backgroundSection.steps.forEach(stepTest);
}

exampleValues.forEach((_, index) => {
scenario.steps.forEach(step => {
const newStep = Object.assign({}, step);
Object.entries(exampleValues[index]).forEach(column => {
Expand All @@ -43,10 +40,15 @@ const createTestFromScenario = scenario => {
});
});
});
} else {
});
} else {
it(scenario.name, () => {
if (backgroundSection) {
backgroundSection.steps.forEach(stepTest);
}
scenario.steps.forEach(step => stepTest(step));
}
});
});
}
};

module.exports = {
Expand Down
8 changes: 3 additions & 5 deletions createTestsFromFeature.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { createTestFromScenario } = require("./createTestFromScenario");

const createTestsFromFeature = parsedFeature =>
const createTestsFromFeature = parsedFeature => {
describe(parsedFeature.feature.name, () => {
const backgroundSection = parsedFeature.feature.children.find(
section => section.type === "Background"
Expand All @@ -9,12 +9,10 @@ const createTestsFromFeature = parsedFeature =>
section => section.type !== "Background"
);
otherSections.forEach(section => {
if (backgroundSection) {
createTestFromScenario(backgroundSection);
}
createTestFromScenario(section);
createTestFromScenario(section, backgroundSection);
});
});
};

module.exports = {
createTestsFromFeature
Expand Down
7 changes: 1 addition & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"debug": "^3.0.1",
"gherkin": "^5.0.0",
"glob": "^7.1.2",
"mocha-steps": "^1.1.0",
"through": "^2.3.8"
},
"devDependencies": {
Expand Down
4 changes: 4 additions & 0 deletions resolveStepDefinition.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable global-require */
/* global jest */
const fs = require("fs");
const { Parser } = require("gherkin");
const { createTestsFromFeature } = require("./createTestsFromFeature");
Expand All @@ -7,6 +8,9 @@ const { when, then, given } = require("./resolveStepDefinition");
window.when = when;
window.then = then;
window.given = given;
window.cy = {
log: jest.fn()
};

const readAndParseFeatureFile = featureFilePath => {
const spec = fs.readFileSync(featureFilePath);
Expand Down

0 comments on commit 92b5d77

Please sign in to comment.