From a8d3df650ba86624bc8816dca6283b4b4f28465b Mon Sep 17 00:00:00 2001 From: Emmanuel DEMEY Date: Thu, 7 Feb 2019 19:00:30 +0100 Subject: [PATCH] feat(step_definition): Add option to define path for the common folder (#138) * feat(step_definition): Add option to define path for the common folder 136 * feat: add doc about step_definition and nonGlobalStepDefinitions options --- .eslintrc | 8 ++---- README.md | 6 ++++ lib/getStepDefinitionsPaths.js | 5 +++- lib/getStepDefinitionsPaths.test.js | 44 +++++++++++++++++++++++++++++ package-lock.json | 39 +++++++++++++++++++++++++ package.json | 2 +- 6 files changed, 97 insertions(+), 7 deletions(-) create mode 100644 lib/getStepDefinitionsPaths.test.js diff --git a/.eslintrc b/.eslintrc index 404c6ef6..e13d78b8 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,12 +1,10 @@ { "extends": ["airbnb-base", "plugin:prettier/recommended"], - "plugins": [ - "cypress", - "prettier" - ], + "plugins": ["cypress", "prettier"], "env": { "browser": true, - "cypress/globals": true + "cypress/globals": true, + "jest": true }, "rules": { "prettier/prettier": "error" diff --git a/README.md b/README.md index 6f98cfc6..9b73c0b4 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,12 @@ First please use [cosmiconfig](https://github.com/davidtheclark/cosmiconfig) to ``` inside your package.json. (this will become the default option in a future version) +Option | Default value | Description +------------ | ------------- | ------------- +commonPath | a common folder inside the step_definitions folder | Define the path to a folder containing all common step definitions of your tests +nonGlobalStepDefinitions | true | If true use the Cypress Cucumber Preprocessor Style pattern for placing step definitions files. If false, we will use the "oldschool" (everything is global) Cucumber style +step_definitions | | Path to the folder containing our step definitions + ###### Step definitions creation Then put your step definitions in cypress/integration with the folder name matching the .feature filename. Easier to show than to explain, so, assuming the feature file is in cypress/integration/Google.feature , as proposed above, the preprocessor will read all the files inside cypress/integration/Google/, so: diff --git a/lib/getStepDefinitionsPaths.js b/lib/getStepDefinitionsPaths.js index eb440f4d..51cd840e 100644 --- a/lib/getStepDefinitionsPaths.js +++ b/lib/getStepDefinitionsPaths.js @@ -11,7 +11,10 @@ const getStepDefinitionsPaths = filePath => { const nonGlobalPattern = `${getStepDefinitionPathsFrom( filePath )}/**/*.+(js|ts)`; - const commonDefinitionsPattern = `${stepDefinitionPath()}/common/**/*.+(js|ts)`; + + const commonPath = + loaded.config.commonPath || `${stepDefinitionPath()}/common/`; + const commonDefinitionsPattern = `${commonPath}**/*.+(js|ts)`; paths = paths.concat(glob.sync(nonGlobalPattern)); paths = paths.concat(glob.sync(commonDefinitionsPattern)); } else { diff --git a/lib/getStepDefinitionsPaths.test.js b/lib/getStepDefinitionsPaths.test.js new file mode 100644 index 00000000..34c3e705 --- /dev/null +++ b/lib/getStepDefinitionsPaths.test.js @@ -0,0 +1,44 @@ +jest.mock("./stepDefinitionPath.js", () => () => "stepDefinitionPath"); +jest.mock("glob", () => ({ + sync(pattern) { + return pattern; + } +})); + +describe("getStepDefinitionsPaths", () => { + it("should return the default common folder", () => { + jest.resetModules(); + jest.mock("cosmiconfig", () => () => ({ + load: () => ({ + config: { + nonGlobalStepDefinitions: true + } + }) + })); + // eslint-disable-next-line global-require + const { getStepDefinitionsPaths } = require("./getStepDefinitionsPaths"); + + const actual = getStepDefinitionsPaths("/path"); + const expected = "stepDefinitionPath/common/**/*.+(js|ts)"; + + expect(actual).to.include(expected); + }); + it("should return the common folder defined by the developper", () => { + jest.resetModules(); + jest.mock("cosmiconfig", () => () => ({ + load: () => ({ + config: { + nonGlobalStepDefinitions: true, + commonPath: "myPath/" + } + }) + })); + // eslint-disable-next-line global-require + const { getStepDefinitionsPaths } = require("./getStepDefinitionsPaths"); + + const actual = getStepDefinitionsPaths("/path"); + const expected = "myPath/**/*.+(js|ts)"; + + expect(actual).to.include(expected); + }); +}); diff --git a/package-lock.json b/package-lock.json index dee13610..ea76273e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5146,6 +5146,16 @@ "minimatch": "^3.0.3" } }, + "fill-keys": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/fill-keys/-/fill-keys-1.0.2.tgz", + "integrity": "sha1-mo+jb06K1jTjv2tPPIiCVRRS6yA=", + "dev": true, + "requires": { + "is-object": "~1.0.1", + "merge-descriptors": "~1.0.0" + } + }, "fill-range": { "version": "2.2.4", "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-2.2.4.tgz", @@ -6745,6 +6755,12 @@ "integrity": "sha1-PkcprB9f3gJc19g6iW2rn09n2w8=", "dev": true }, + "is-object": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/is-object/-/is-object-1.0.1.tgz", + "integrity": "sha1-iVJojF7C/9awPsyF52ngKQMINHA=", + "dev": true + }, "is-observable": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/is-observable/-/is-observable-1.1.0.tgz", @@ -9173,6 +9189,12 @@ "integrity": "sha512-VjFo4P5Whtj4vsLzsYBu5ayHhoHJ0UqNm7ibvShmbmoz7tGi0vXaoJbGdB+GmDMLUdg8DpQXEIeVDAe8MaABvQ==", "dev": true }, + "merge-descriptors": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz", + "integrity": "sha1-sAqqVW3YtEVoFQ7J0blT8/kMu2E=", + "dev": true + }, "merge-stream": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/merge-stream/-/merge-stream-1.0.1.tgz", @@ -9332,6 +9354,12 @@ "xtend": "^4.0.0" } }, + "module-not-found-error": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/module-not-found-error/-/module-not-found-error-1.0.1.tgz", + "integrity": "sha1-z4tP9PKWQGdNbN0CsOO8UjwrvcA=", + "dev": true + }, "moment": { "version": "2.22.2", "resolved": "https://registry.npmjs.org/moment/-/moment-2.22.2.tgz", @@ -13255,6 +13283,17 @@ "sisteransi": "^0.1.1" } }, + "proxyquire": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/proxyquire/-/proxyquire-2.1.0.tgz", + "integrity": "sha512-kptdFArCfGRtQFv3Qwjr10lwbEV0TBJYvfqzhwucyfEXqVgmnAkyEw/S3FYzR5HI9i5QOq4rcqQjZ6AlknlCDQ==", + "dev": true, + "requires": { + "fill-keys": "^1.0.2", + "module-not-found-error": "^1.0.0", + "resolve": "~1.8.1" + } + }, "pseudomap": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/pseudomap/-/pseudomap-1.0.2.tgz", diff --git a/package.json b/package.json index 198b4973..aae3891a 100644 --- a/package.json +++ b/package.json @@ -56,8 +56,8 @@ "eslint-plugin-cypress": "^2.0.1", "eslint-plugin-import": "^2.12.0", "eslint-plugin-prettier": "^2.6.0", - "jest": "^23.6.0", "husky": "^1.1.1", + "jest": "^23.6.0", "lint-staged": "^7.3.0", "prettier": "^1.13.5", "semantic-release": "^15.12.3"