Skip to content

Commit

Permalink
fix: cli init translation use correct actor instead of I
Browse files Browse the repository at this point in the history
  • Loading branch information
andonary committed Sep 1, 2023
1 parent 49e774e commit e4a07ad
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 6 deletions.
40 changes: 36 additions & 4 deletions lib/command/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,13 @@ module.exports = function (initPath) {
// no extra step file for typescript (as it doesn't match TS conventions)
const stepFile = `./steps_file.${extension}`;
fs.writeFileSync(path.join(testsPath, stepFile), extension === 'ts' ? defaultActorTs : defaultActor);
config.include.I = isTypeScript === true ? './steps_file' : stepFile;

if (isTypeScript) {
config.include = _actorTranslation('./steps_file', config.translation);
} else {
config.include = _actorTranslation(stepFile, config.translation);
}

print(`Steps file created at ${stepFile}`);

let configSource;
Expand Down Expand Up @@ -316,7 +322,7 @@ module.exports = function (initPath) {
};

print('Configure helpers...');
inquirer.prompt(helperConfigs).then((helperResult) => {
inquirer.prompt(helperConfigs).then(async (helperResult) => {
if (helperResult.Playwright_browser === 'electron') {
delete helperResult.Playwright_url;
delete helperResult.Playwright_show;
Expand All @@ -336,12 +342,12 @@ module.exports = function (initPath) {
});

print('');
finish();
await finish();
});
});
};

function install(dependencies, verbose) {
function install(dependencies) {
let command;
let args;

Expand Down Expand Up @@ -374,9 +380,35 @@ function install(dependencies, verbose) {
].concat(dependencies);
}

if (process.env._INIT_DRY_RUN_INSTALL) {
args.push('--dry-run');
}

const { status } = spawn.sync(command, args, { stdio: 'inherit' });
if (status !== 0) {
throw new Error(`${command} ${args.join(' ')} failed`);
}
return true;
}

function _actorTranslation(stepFile, translationSelected) {
let actor;

for (const translationAvailable of translations) {
if (translationSelected === translationAvailable) {
const nameOfActor = require('../../translations')[translationAvailable].I;

actor = {
[nameOfActor]: stepFile,
};
}
}

if (!actor) {
actor = {
I: stepFile,
};
}

return actor;
}
60 changes: 58 additions & 2 deletions test/runner/init_test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,76 @@
const { DOWN, ENTER } = require('inquirer-test');
const run = require('inquirer-test');
const path = require('path');
const fs = require('fs');

const runner = path.join(__dirname, '../../bin/codecept.js');
const codecept_dir = path.join(__dirname, '/../data/sandbox/configs/init');

describe('Init Command', function () {
this.timeout(20000);

it('steps are showing', async () => {
const result = await run([runner, 'init'], ['Y', ENTER, ENTER, DOWN, DOWN, DOWN, ENTER, 'y']);
beforeEach(() => {
process.env._INIT_DRY_RUN_INSTALL = true;
});

afterEach(() => {
try {
fs.unlinkSync(`${codecept_dir}/codecept.conf.ts`);
fs.unlinkSync(`${codecept_dir}/steps_file.ts`);
fs.unlinkSync(`${codecept_dir}/tsconfig.json`);
} catch (e) {
// continue regardless of error
}

try {
fs.unlinkSync(`${codecept_dir}/codecept.conf.js`);
fs.unlinkSync(`${codecept_dir}/steps_file.js`);
fs.unlinkSync(`${codecept_dir}/jsconfig.json`);
} catch (e) {
// continue regardless of error
}

delete process.env._INIT_DRY_RUN_INSTALL;
});

it('should init Codecept with TypeScript REST JSONResponse English', async () => {
const result = await run([runner, 'init', codecept_dir], ['Y', ENTER, ENTER, DOWN, DOWN, DOWN, ENTER, 'y', ENTER, codecept_dir, ENTER, ENTER, ENTER, ENTER]);

result.should.include('Welcome to CodeceptJS initialization tool');
result.should.include('It will prepare and configure a test environment for you');
result.should.include('Installing to');
result.should.include('? Do you plan to write tests in TypeScript? (y/N)');
result.should.include('Where are your tests located? ./*_test.ts');
result.should.include('What helpers do you want to use? REST');
result.should.include('? Do you want to use JSONResponse helper for assertions on JSON responses?');
result.should.include('? Where should logs, screenshots, and reports to be stored?');
result.should.include('? Do you want to enable localization for tests?');

const config = fs.readFileSync(`${codecept_dir}/codecept.conf.ts`).toString();
config.should.include('I: \'./steps_file\'');

fs.accessSync(`${codecept_dir}/steps_file.ts`, fs.constants.R_OK);
fs.accessSync(`${codecept_dir}/tsconfig.json`, fs.constants.R_OK);
});

it('should init Codecept with JavaScript REST JSONResponse de-DE', async () => {
const result = await run([runner, 'init', codecept_dir], [ENTER, ENTER, DOWN, DOWN, DOWN, ENTER, 'y', ENTER, codecept_dir, ENTER, DOWN, ENTER, ENTER, ENTER]);

result.should.include('Welcome to CodeceptJS initialization tool');
result.should.include('It will prepare and configure a test environment for you');
result.should.include('Installing to');
result.should.include('? Do you plan to write tests in TypeScript? (y/N)');
result.should.include('Where are your tests located? ./*_test.js');
result.should.include('What helpers do you want to use? REST');
result.should.include('? Do you want to use JSONResponse helper for assertions on JSON responses?');
result.should.include('? Where should logs, screenshots, and reports to be stored?');
result.should.include('? Do you want to enable localization for tests?');
result.should.include('de-DE');

const config = fs.readFileSync(`${codecept_dir}/codecept.conf.js`).toString();
config.should.include('Ich: \'./steps_file.js\'');

fs.accessSync(`${codecept_dir}/steps_file.js`, fs.constants.R_OK);
fs.accessSync(`${codecept_dir}/jsconfig.json`, fs.constants.R_OK);
});
});

0 comments on commit e4a07ad

Please sign in to comment.