From 0faa3ef75fdb757ab68c688702b99f23f2837361 Mon Sep 17 00:00:00 2001 From: Ando NARY <36852616+andonary@users.noreply.github.com> Date: Tue, 28 May 2024 17:37:46 +0200 Subject: [PATCH 1/3] fix(cli): gherkin command init with TypeScript fix this command: npx codeceptjs gherkin:init --- lib/command/gherkin/init.js | 27 ++++-- lib/command/utils.js | 10 +- .../gherkin/config_js/codecept.conf.init.js | 16 ++++ .../gherkin/config_ts/codecept.conf.init.ts | 15 +++ test/runner/gherkin_test.js | 93 +++++++++++++++++++ 5 files changed, 149 insertions(+), 12 deletions(-) create mode 100644 test/data/sandbox/configs/gherkin/config_js/codecept.conf.init.js create mode 100644 test/data/sandbox/configs/gherkin/config_ts/codecept.conf.init.ts create mode 100644 test/runner/gherkin_test.js diff --git a/lib/command/gherkin/init.js b/lib/command/gherkin/init.js index b0aabe4a8..b665bb65e 100644 --- a/lib/command/gherkin/init.js +++ b/lib/command/gherkin/init.js @@ -27,6 +27,19 @@ Given('I have a defined step', () => { module.exports = function (genPath) { const testsPath = getTestRoot(genPath); const config = getConfig(testsPath); + let extension = "js"; + let configFile = path.join(testsPath, `codecept.conf.${extension}`); + + if (!fileExists(configFile)) { + extension = "ts"; + configFile = path.join(testsPath, `codecept.conf.${extension}`); + if (!fileExists(configFile)) { + output.error( + `Can't initializing Gherkin. This command must run on already initialized project` + ); + process.exit(1); + } + } output.print('Initializing Gherkin (Cucumber BDD) for CodeceptJS'); output.print('--------------------------'); @@ -53,18 +66,18 @@ module.exports = function (genPath) { output.success(`Created ${dir}, place step definitions into it`); } - if (safeFileWrite(path.join(dir, 'steps.js'), stepsFile)) { - output.success('Created sample steps file: step_definitions/steps.js'); + if (safeFileWrite(path.join(dir, `steps.${extension}`), stepsFile)) { + output.success( + `Created sample steps file: step_definitions/steps.${extension}` + ); } config.gherkin = { - features: './features/*.feature', - steps: [ - './step_definitions/steps.js', - ], + features: "./features/*.feature", + steps: [`./step_definitions/steps.${extension}`], }; - updateConfig(testsPath, config); + updateConfig(testsPath, config, extension); output.success('Gherkin setup is done.'); output.success('Start writing feature files and implement corresponding steps.'); diff --git a/lib/command/utils.js b/lib/command/utils.js index ac3cf7fd6..1d13b25a7 100644 --- a/lib/command/utils.js +++ b/lib/command/utils.js @@ -41,15 +41,15 @@ function fail(msg) { module.exports.fail = fail; -function updateConfig(testsPath, config, key, extension = 'js') { - const configFile = path.join(testsPath, `codecept.conf.${extension}`); +function updateConfig(testsPath, config, extension) { + let configFile = path.join(testsPath, `codecept.conf.${extension}`); if (!fileExists(configFile)) { - console.log(); const msg = `codecept.conf.${extension} config can\'t be updated automatically`; + console.log(); console.log(`${output.colors.bold.red(msg)}`); - console.log('Please update it manually:'); + console.log(`${output.colors.bold.red("Please update it manually:")}`); console.log(); - console.log(`${key}: ${config[key]}`); + console.log(config); console.log(); return; } diff --git a/test/data/sandbox/configs/gherkin/config_js/codecept.conf.init.js b/test/data/sandbox/configs/gherkin/config_js/codecept.conf.init.js new file mode 100644 index 000000000..61e56f1dd --- /dev/null +++ b/test/data/sandbox/configs/gherkin/config_js/codecept.conf.init.js @@ -0,0 +1,16 @@ +/** @type {CodeceptJS.MainConfig} */ +exports.config = { + tests: "./*_test.js", + output: "./output", + helpers: { + Playwright: { + browser: "chromium", + url: "http://localhost", + show: true, + }, + }, + include: { + I: "./steps_file.js", + }, + name: "CodeceptJS", +}; diff --git a/test/data/sandbox/configs/gherkin/config_ts/codecept.conf.init.ts b/test/data/sandbox/configs/gherkin/config_ts/codecept.conf.init.ts new file mode 100644 index 000000000..86fe45f4f --- /dev/null +++ b/test/data/sandbox/configs/gherkin/config_ts/codecept.conf.init.ts @@ -0,0 +1,15 @@ +export const config: CodeceptJS.MainConfig = { + tests: "./*_test.ts", + output: "./output", + helpers: { + Playwright: { + browser: "chromium", + url: "http://localhost", + show: true + } + }, + include: { + I: "./steps_file" + }, + name: "CodeceptJS" +} diff --git a/test/runner/gherkin_test.js b/test/runner/gherkin_test.js new file mode 100644 index 000000000..7a1dbd94a --- /dev/null +++ b/test/runner/gherkin_test.js @@ -0,0 +1,93 @@ +const assert = require("assert"); +const path = require("path"); +const fs = require("fs"); +const exec = require("child_process").exec; + +const runner = path.join(__dirname, "/../../bin/codecept.js"); +const codecept_dir = path.join(__dirname, "/../data/sandbox/configs/gherkin/"); + +describe("gherkin bdd commands", () => { + describe("bdd:init", () => { + let codecept_dir_js = path.join(codecept_dir, "config_js"); + let codecept_dir_ts = path.join(codecept_dir, "config_ts"); + + beforeEach(() => { + fs.copyFileSync( + path.join(codecept_dir_js, "codecept.conf.init.js"), + path.join(codecept_dir_js, "codecept.conf.js") + ); + fs.copyFileSync( + path.join(codecept_dir_ts, "codecept.conf.init.ts"), + path.join(codecept_dir_ts, "codecept.conf.ts") + ); + }); + + afterEach(() => { + try { + fs.rmSync(path.join(codecept_dir_js, "codecept.conf.js")); + fs.rmSync(path.join(codecept_dir_js, "features"), { + recursive: true, + }); + fs.rmSync(path.join(codecept_dir_js, "step_definitions"), { + recursive: true, + }); + } catch (e) {} + try { + fs.rmSync(path.join(codecept_dir_ts, "codecept.conf.ts")); + fs.rmSync(path.join(codecept_dir_ts, "features"), { + recursive: true, + }); + fs.rmSync(path.join(codecept_dir_ts, "step_definitions"), { + recursive: true, + }); + } catch (e) {} + }); + + [ + { + codecept_dir_test: codecept_dir_js, + extension: "js", + }, + { + codecept_dir_test: codecept_dir_ts, + extension: "ts", + }, + ].forEach(({ codecept_dir_test, extension }) => { + it(`prepare CodeceptJS to run feature files (codecept.conf.${extension})`, (done) => { + exec(`${runner} gherkin:init ${codecept_dir_test}`, (err, stdout) => { + let dir = path.join(codecept_dir_test, "features"); + + stdout.should.include( + "Initializing Gherkin (Cucumber BDD) for CodeceptJS" + ); + stdout.should.include( + `Created ${dir}, place your *.feature files in it` + ); + stdout.should.include( + "Created sample feature file: features/basic.feature" + ); + + dir = path.join(codecept_dir_test, "step_definitions"); + stdout.should.include( + `Created ${dir}, place step definitions into it` + ); + stdout.should.include( + `Created sample steps file: step_definitions/steps.${extension}` + ); + assert(!err); + + const configResult = fs + .readFileSync( + path.join(codecept_dir_test, `codecept.conf.${extension}`) + ) + .toString(); + configResult.should.contain(`features: './features/*.feature'`); + configResult.should.contain( + `steps: ['./step_definitions/steps.${extension}']` + ); + done(); + }); + }); + }); + }); +}); From be8fa311dbfa19e3bab193b038d8841269fb568b Mon Sep 17 00:00:00 2001 From: Ando NARY <36852616+andonary@users.noreply.github.com> Date: Tue, 28 May 2024 18:02:44 +0200 Subject: [PATCH 2/3] fix(cli): nitpick let into const --- lib/command/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/command/utils.js b/lib/command/utils.js index 1d13b25a7..a7a7a6ad7 100644 --- a/lib/command/utils.js +++ b/lib/command/utils.js @@ -42,7 +42,7 @@ function fail(msg) { module.exports.fail = fail; function updateConfig(testsPath, config, extension) { - let configFile = path.join(testsPath, `codecept.conf.${extension}`); + const configFile = path.join(testsPath, `codecept.conf.${extension}`); if (!fileExists(configFile)) { const msg = `codecept.conf.${extension} config can\'t be updated automatically`; console.log(); From 844ae549ee3ddf24c8dfe1861f16192c43951238 Mon Sep 17 00:00:00 2001 From: Ando NARY <36852616+andonary@users.noreply.github.com> Date: Thu, 30 May 2024 23:06:50 +0200 Subject: [PATCH 3/3] fix(cli): findConfigFile instead --- lib/command/gherkin/init.js | 25 +++++++++++-------------- lib/command/utils.js | 11 +++++++++++ 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/lib/command/gherkin/init.js b/lib/command/gherkin/init.js index b665bb65e..7ac70231c 100644 --- a/lib/command/gherkin/init.js +++ b/lib/command/gherkin/init.js @@ -4,7 +4,7 @@ const mkdirp = require('mkdirp'); const output = require('../../output'); const { fileExists } = require('../../utils'); const { - getConfig, getTestRoot, updateConfig, safeFileWrite, + getConfig, getTestRoot, updateConfig, safeFileWrite, findConfigFile, } = require('../utils'); const featureFile = `Feature: Business rules @@ -26,21 +26,18 @@ Given('I have a defined step', () => { module.exports = function (genPath) { const testsPath = getTestRoot(genPath); - const config = getConfig(testsPath); - let extension = "js"; - let configFile = path.join(testsPath, `codecept.conf.${extension}`); - - if (!fileExists(configFile)) { - extension = "ts"; - configFile = path.join(testsPath, `codecept.conf.${extension}`); - if (!fileExists(configFile)) { - output.error( - `Can't initializing Gherkin. This command must run on already initialized project` - ); - process.exit(1); - } + const configFile = findConfigFile(testsPath); + + if (!configFile) { + output.error( + "Can't initialize Gherkin. This command must be run in an already initialized project." + ); + process.exit(1); } + const config = getConfig(testsPath); + const extension = path.extname(configFile).substring(1); + output.print('Initializing Gherkin (Cucumber BDD) for CodeceptJS'); output.print('--------------------------'); diff --git a/lib/command/utils.js b/lib/command/utils.js index a7a7a6ad7..b8c9ca2fa 100644 --- a/lib/command/utils.js +++ b/lib/command/utils.js @@ -104,3 +104,14 @@ module.exports.createOutputDir = (config, testRoot) => { mkdirp.sync(outputDir); } }; + +module.exports.findConfigFile = (testsPath) => { + const extensions = ['js', 'ts']; + for (const ext of extensions) { + const configFile = path.join(testsPath, `codecept.conf.${ext}`); + if (fileExists(configFile)) { + return configFile; + } + } + return null; +}