Skip to content

Commit 25e31ac

Browse files
authored
fix(loader): work with escaped characters (#249)
We will now parse and escape the stringified .feature file when putting it in the template literal - otherwise the escaped characters got parsed and we were getting syntax errors fix #106
1 parent ad2b9f3 commit 25e31ac

File tree

6 files changed

+72
-108
lines changed

6 files changed

+72
-108
lines changed

cypress/integration/DataTable.feature

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,10 @@ Feature: Being a plugin handling DataTable scenario
88
| number | another number |
99
| 1 | 2 |
1010
| 3 | 4 |
11-
Then I verify the datatable result is equal to 10
11+
Then I verify the datatable result is equal to 10
12+
13+
14+
Scenario: New line character
15+
Given I have a table with some escaped characters in it
16+
| foo | bar |
17+
| foo\nfoo | bar\nbar |
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
/* global then, when */
1+
// eslint-disable-next-line import/no-extraneous-dependencies
2+
const { Given, When, Then } = require("cypress-cucumber-preprocessor/steps");
23

34
// you can have external state, and also require things!
45
let sum = 0;
56

6-
when("I add all following numbers:", dataTable => {
7+
When("I add all following numbers:", dataTable => {
78
// console.log("a, ", dataTable.rawTable.slice(1))
89
sum = dataTable.rawTable
910
.slice(1)
@@ -14,6 +15,11 @@ when("I add all following numbers:", dataTable => {
1415
);
1516
});
1617

17-
then("I verify the datatable result is equal to {int}", result => {
18+
Then("I verify the datatable result is equal to {int}", result => {
1819
expect(sum).to.equal(result);
1920
});
21+
22+
Given("I have a table with some escaped characters in it", dataTable => {
23+
console.log(dataTable);
24+
// we don't need to do anything, just make sure it doesn't break
25+
});

lib/featuresLoader.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const path = require("path");
33
const fs = require("fs");
44
const { Parser } = require("gherkin");
55
const log = require("debug")("cypress:cucumber");
6+
const jsStringEscape = require("js-string-escape");
67

78
const { getStepDefinitionsPaths } = require("./getStepDefinitionsPaths");
89
const { cucumberTemplate } = require("./cucumberTemplate");
@@ -33,7 +34,9 @@ const createCucumber = (
3334
.find(fileSteps => fileSteps[filePath])
3435
[filePath].join("\n")}
3536
36-
createTestsFromFeature('${path.basename(filePath)}', \`${spec}\`);
37+
createTestsFromFeature('${path.basename(filePath)}', \`${jsStringEscape(
38+
spec
39+
)}\`);
3740
})
3841
`
3942
)

lib/loader.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
const log = require("debug")("cypress:cucumber");
22
const path = require("path");
33
const { Parser } = require("gherkin");
4+
const jsStringEscape = require("js-string-escape");
45
const { getStepDefinitionsPaths } = require("./getStepDefinitionsPaths");
56
const { cucumberTemplate } = require("./cucumberTemplate");
67
const { getCucumberJsonConfig } = require("./getCucumberJsonConfig");
8+
79
// This is the template for the file that we will send back to cypress instead of the text of a
810
// feature file
911
const createCucumber = (filePath, cucumberJson, spec, toRequire, name) =>
@@ -13,7 +15,7 @@ const createCucumber = (filePath, cucumberJson, spec, toRequire, name) =>
1315
window.cucumberJson = ${JSON.stringify(cucumberJson)};
1416
describe(\`${name}\`, function() {
1517
${toRequire.join("\n")}
16-
createTestsFromFeature('${filePath}', \`${spec}\`);
18+
createTestsFromFeature('${filePath}', \`${jsStringEscape(spec)}\`);
1719
});
1820
`;
1921

0 commit comments

Comments
 (0)