diff --git a/lib/command/gherkin/init.js b/lib/command/gherkin/init.js index b0aabe4a8..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,7 +26,17 @@ Given('I have a defined step', () => { module.exports = function (genPath) { const testsPath = getTestRoot(genPath); + 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('--------------------------'); @@ -53,18 +63,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..b8c9ca2fa 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') { +function updateConfig(testsPath, config, extension) { const 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; } @@ -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; +} 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(); + }); + }); + }); + }); +});