Skip to content

Commit 6dff207

Browse files
jp7677lgandecki
authored andcommitted
feat: allow extensions for the rc configuration file.
Current cosmiconfig versions advertises extensions for rc files. Enabling 'rcExtensions' also allows cosmiconfig4 to search for rc files ending with .json, .js etc.
1 parent 1b042c2 commit 6dff207

File tree

6 files changed

+48
-89
lines changed

6 files changed

+48
-89
lines changed

lib/getConfig.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+
3+
let explorer;
4+
5+
exports.getConfig = () => {
6+
if (!explorer) {
7+
explorer = cosmiconfig("cypress-cucumber-preprocessor", {
8+
sync: true,
9+
rcExtensions: true
10+
});
11+
}
12+
13+
const loaded = explorer.load();
14+
return loaded && loaded.config;
15+
};

lib/getCucumberJsonConfig.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
const cosmiconfig = require("cosmiconfig");
21
const log = require("debug")("cypress:cucumber");
2+
const { getConfig } = require("./getConfig");
33

44
exports.getCucumberJsonConfig = () => {
5-
const explorer = cosmiconfig("cypress-cucumber-preprocessor", { sync: true });
6-
const loaded = explorer.load();
7-
5+
const config = getConfig();
86
const cucumberJson =
9-
loaded && loaded.config && loaded.config.cucumberJson
10-
? loaded.config.cucumberJson
11-
: { generate: false };
7+
config && config.cucumberJson ? config.cucumberJson : { generate: false };
128
log("cucumber.json", JSON.stringify(cucumberJson));
139

1410
return cucumberJson;

lib/getStepDefinitionsPaths.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
const glob = require("glob");
2-
const cosmiconfig = require("cosmiconfig");
2+
const { getConfig } = require("./getConfig");
33
const stepDefinitionPath = require("./stepDefinitionPath.js");
44
const { getStepDefinitionPathsFrom } = require("./getStepDefinitionPathsFrom");
55

66
const getStepDefinitionsPaths = filePath => {
77
let paths = [];
8-
const explorer = cosmiconfig("cypress-cucumber-preprocessor", { sync: true });
9-
const loaded = explorer.load();
10-
if (loaded && loaded.config && loaded.config.nonGlobalStepDefinitions) {
8+
const config = getConfig();
9+
if (config && config.nonGlobalStepDefinitions) {
1110
const nonGlobalPattern = `${getStepDefinitionPathsFrom(
1211
filePath
1312
)}/**/*.+(js|ts)`;
14-
const commonPath =
15-
loaded.config.commonPath || `${stepDefinitionPath()}/common/`;
13+
const commonPath = config.commonPath || `${stepDefinitionPath()}/common/`;
1614
const commonDefinitionsPattern = `${commonPath}**/*.+(js|ts)`;
1715
paths = paths.concat(glob.sync(nonGlobalPattern));
1816
paths = paths.concat(glob.sync(commonDefinitionsPattern));
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
const cosmiconfig = require("cosmiconfig");
1+
const { getConfig } = require("./getConfig");
22

33
exports.isNonGlobalStepDefinitionsMode = () => {
4-
const explorer = cosmiconfig("cypress-cucumber-preprocessor", { sync: true });
5-
const loaded = explorer.load();
6-
return loaded && loaded.config && loaded.config.nonGlobalStepDefinitions;
4+
const config = getConfig();
5+
return config && config.nonGlobalStepDefinitions;
76
};

lib/stepDefinitionPath.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
const path = require("path");
2-
const cosmiconfig = require("cosmiconfig");
32
const fs = require("fs");
3+
const { getConfig } = require("./getConfig");
44

55
module.exports = () => {
66
const appRoot = process.cwd();
7-
8-
const explorer = cosmiconfig("cypress-cucumber-preprocessor", { sync: true });
9-
const loaded = explorer.load();
10-
if (loaded && loaded.config) {
11-
const { config, filepath } = loaded;
12-
7+
const config = getConfig();
8+
if (config) {
139
// left for backward compability, but we need the consistency with other configuration options
1410
const confStepDefinitions = config.step_definitions
1511
? config.step_definitions
@@ -21,7 +17,7 @@ module.exports = () => {
2117

2218
if (!fs.existsSync(stepsPath)) {
2319
throw new Error(
24-
`We've tried to resolve your step definitions at ${relativePath}, but that doesn't seem to exist. As of version 2.0.0 it's required to set step_definitions in your cypress-cucumber-preprocessor configuration. Currently your configuration is in: ${filepath}. Look for nonGlobalStepDefinitions and add stepDefinitions right next to it. It should match your cypress configuration has set for integrationFolder. We no longer rely on getting information from that file as it was unreliable and problematic across Linux/MacOS/Windows especially since the config file could have been passed as an argument to cypress.`
20+
`We've tried to resolve your step definitions at ${relativePath}, but that doesn't seem to exist. As of version 2.0.0 it's required to set step_definitions in your cypress-cucumber-preprocessor configuration. Look for nonGlobalStepDefinitions and add stepDefinitions right next to it. It should match your cypress configuration has set for integrationFolder. We no longer rely on getting information from that file as it was unreliable and problematic across Linux/MacOS/Windows especially since the config file could have been passed as an argument to cypress.`
2521
);
2622
}
2723
return stepsPath;

lib/stepDefinitionPath.test.js

Lines changed: 19 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,44 @@
11
const fs = require("fs");
2-
const cosmiconfig = require("cosmiconfig");
3-
const stepDefinitionPath = require("./stepDefinitionPath.js");
2+
const { getConfig } = require("./getConfig");
3+
const stepDefinitionPath = require("./stepDefinitionPath");
44

5+
jest.mock("./getConfig");
56
jest.mock("fs", () => ({
67
existsSync: jest.fn()
78
}));
89

9-
jest.mock("cosmiconfig");
10-
1110
const defaultNonGlobalStepDefinitionsPath = "cypress/integration";
1211

1312
describe("load path from step definitions", () => {
1413
beforeEach(() => {
15-
cosmiconfig.mockReset();
14+
getConfig.mockReset();
1615
fs.existsSync.mockReset();
1716
});
1817

1918
test("Should throw an error if nonGlobalStepDefinitions and stepDefinitions are not set and the default is wrong", () => {
20-
const COMICONFIG_FILEPATH = "package.json";
21-
const loadMock = jest.fn().mockReturnValue({
22-
config: {
23-
nonGlobalStepDefinitions: true
24-
},
25-
filepath: COMICONFIG_FILEPATH
26-
});
27-
cosmiconfig.mockReturnValue({
28-
load: loadMock
19+
getConfig.mockReturnValue({
20+
nonGlobalStepDefinitions: true
2921
});
30-
fs.existsSync.mockReturnValue(false);
3122

32-
const errorMessage = `We've tried to resolve your step definitions at ${defaultNonGlobalStepDefinitionsPath}, but that doesn't seem to exist. As of version 2.0.0 it's required to set step_definitions in your cypress-cucumber-preprocessor configuration. Currently your configuration is in: ${COMICONFIG_FILEPATH}. Look for nonGlobalStepDefinitions and add stepDefinitions right next to it. It should match your cypress configuration has set for integrationFolder. We no longer rely on getting information from that file as it was unreliable and problematic across Linux/MacOS/Windows especially since the config file could have been passed as an argument to cypress.`;
23+
const errorMessage = `We've tried to resolve your step definitions at ${defaultNonGlobalStepDefinitionsPath}, but that doesn't seem to exist. As of version 2.0.0 it's required to set step_definitions in your cypress-cucumber-preprocessor configuration. Look for nonGlobalStepDefinitions and add stepDefinitions right next to it. It should match your cypress configuration has set for integrationFolder. We no longer rely on getting information from that file as it was unreliable and problematic across Linux/MacOS/Windows especially since the config file could have been passed as an argument to cypress.`;
3324
expect(stepDefinitionPath).throw(errorMessage);
3425
});
3526

3627
test("Should throw an error if nonGlobalStepDefinitions and stepDefinitions are set but the folder doesn't exist", () => {
37-
const COMICONFIG_FILEPATH = "package.json";
3828
const stepDefinitions = "cypress/stepDefinitions";
39-
const loadMock = jest.fn().mockReturnValue({
40-
config: {
41-
nonGlobalStepDefinitions: true,
42-
stepDefinitions
43-
},
44-
filepath: COMICONFIG_FILEPATH
45-
});
46-
cosmiconfig.mockReturnValue({
47-
load: loadMock
29+
getConfig.mockReturnValue({
30+
nonGlobalStepDefinitions: true,
31+
stepDefinitions
4832
});
49-
fs.existsSync.mockReturnValue(false);
5033

51-
const errorMessage = `We've tried to resolve your step definitions at ${stepDefinitions}, but that doesn't seem to exist. As of version 2.0.0 it's required to set step_definitions in your cypress-cucumber-preprocessor configuration. Currently your configuration is in: ${COMICONFIG_FILEPATH}. Look for nonGlobalStepDefinitions and add stepDefinitions right next to it. It should match your cypress configuration has set for integrationFolder. We no longer rely on getting information from that file as it was unreliable and problematic across Linux/MacOS/Windows especially since the config file could have been passed as an argument to cypress.`;
34+
const errorMessage = `We've tried to resolve your step definitions at ${stepDefinitions}, but that doesn't seem to exist. As of version 2.0.0 it's required to set step_definitions in your cypress-cucumber-preprocessor configuration. Look for nonGlobalStepDefinitions and add stepDefinitions right next to it. It should match your cypress configuration has set for integrationFolder. We no longer rely on getting information from that file as it was unreliable and problematic across Linux/MacOS/Windows especially since the config file could have been passed as an argument to cypress.`;
5235
expect(stepDefinitionPath).throw(errorMessage);
5336
});
5437

5538
test("should use the default stepDefinitions path for nonGlobalStepDefinitions", () => {
5639
const appRoot = process.cwd();
57-
const loadMock = jest.fn().mockReturnValue({
58-
config: {
59-
nonGlobalStepDefinitions: true
60-
}
61-
});
62-
cosmiconfig.mockReturnValue({
63-
load: loadMock
40+
getConfig.mockReturnValue({
41+
nonGlobalStepDefinitions: true
6442
});
6543
fs.existsSync.mockReturnValue(true);
6644

@@ -71,16 +49,9 @@ describe("load path from step definitions", () => {
7149

7250
test("should use the stepDefinitions path for nonGlobalStepDefinitions", () => {
7351
const appRoot = process.cwd();
74-
const loadMock = jest.fn().mockReturnValue({
75-
config: {
76-
stepDefinitions: "./e2e/support/step-definitions",
77-
nonGlobalStepDefinitions: true
78-
}
79-
});
80-
cosmiconfig.mockReturnValue({
81-
load: loadMock
52+
getConfig.mockReturnValue({
53+
step_definitions: "./e2e/support/step-definitions"
8254
});
83-
fs.existsSync.mockReturnValue(true);
8455

8556
expect(stepDefinitionPath()).to.equal(
8657
`${appRoot}/e2e/support/step-definitions`
@@ -89,15 +60,9 @@ describe("load path from step definitions", () => {
8960

9061
test("should use the stepDefinitions path", () => {
9162
const appRoot = process.cwd();
92-
const loadMock = jest.fn().mockReturnValue({
93-
config: {
94-
stepDefinitions: "./e2e/support/step-definitions"
95-
}
63+
getConfig.mockReturnValue({
64+
step_definitions: "./e2e/support/step-definitions"
9665
});
97-
cosmiconfig.mockReturnValue({
98-
load: loadMock
99-
});
100-
fs.existsSync.mockReturnValue(true);
10166

10267
expect(stepDefinitionPath()).to.equal(
10368
`${appRoot}/e2e/support/step-definitions`
@@ -106,26 +71,16 @@ describe("load path from step definitions", () => {
10671

10772
test("should return default path if stepDefinition are not configured and nonGlobalStepDefinitions are not set", () => {
10873
const appRoot = process.cwd();
109-
cosmiconfig.mockReturnValue({
110-
load: jest.fn()
111-
});
112-
11374
expect(stepDefinitionPath()).to.equal(
11475
`${appRoot}/cypress/support/step_definitions`
11576
);
11677
});
11778

11879
test("should allow the backward compatible use of step_definitions in cosmiconfig", () => {
11980
const appRoot = process.cwd();
120-
const loadMock = jest.fn().mockReturnValue({
121-
config: {
122-
step_definitions: "./e2e/support/step-definitions"
123-
}
124-
});
125-
cosmiconfig.mockReturnValue({
126-
load: loadMock
81+
getConfig.mockReturnValue({
82+
step_definitions: "./e2e/support/step-definitions"
12783
});
128-
fs.existsSync.mockReturnValue(true);
12984

13085
expect(stepDefinitionPath()).to.equal(
13186
`${appRoot}/e2e/support/step-definitions`

0 commit comments

Comments
 (0)