Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: provision to have test runners #146

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
89 changes: 89 additions & 0 deletions src/commands/basic/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,92 @@ const showInstructions = () => {
makeInitialCommit();
};

/**
* Fetch the user reqired test runner
*
* @param {String} template
* @returns {Promise<Void>}
*/
const fetchTestRunner = async template => {
const { testRunner } = await inquirer.prompt([
{
name: 'testRunner',
type: 'list',
message: 'Please select your test framework for your project',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
message: 'Please select your test framework for your project',
message: 'Please select the test framework of choice',

choices: ['none', 'ava', 'jest', 'mocha'],
},
]);
if (testRunner === 'none') {
return;
}
if (testRunner === 'ava' && template === 'nuxt') {
return;
}
Comment on lines +87 to +89
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems Nuxt.js works pretty much with Ava. Can you clarify about this early return?


process.chdir(`${projectName}/client/`);

const fetchSpinner = new Spinner(`Installing the ${testRunner}`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const fetchSpinner = new Spinner(`Installing the ${testRunner}`);
const fetchSpinner = new Spinner(`Installing ${testRunner}`);

fetchSpinner.start();
try {
await execa('npm', ['install', '--save-dev', testRunner]);
} catch (err) {
fetchSpinner.fail('Something went wrong');
throw err;
}
fetchSpinner.stop();

//Make npm script 'test'
let packageJsonFile = JSON.parse(
fs.readFileSync('./package.json').toString(),
);
packageJsonFile.scripts['test'] = testRunner;
fs.writeFileSync('./package.json', JSON.stringify(packageJsonFile, null, 2));

//Create config file for testrunners
const testFolderName = template === 'nuxt' ? 'test' : 'tests';

switch (testRunner) {
case 'ava': {
const avaConfig = `export default {
files: ['${testFolderName}/**/*']
};`;
fs.writeFileSync('./ava.config.js', avaConfig);
break;
}
case 'jest': {
const jestConfig = `module.exports = {
verbose: true,
testMatch: ["${testFolderName}/**/*.test.js"]
};`;
fs.writeFileSync('./jest.config.js', jestConfig);
break;
}
case 'mocha': {
const mochaConfig = `'use strict';

// Here's a JavaScript-based config file.
// If you need conditional logic, you might want to use this type of config.
// Otherwise, JSON or YAML is recommended.

module.exports = {
diff: true,
extension: ['js'],
opts: false,
package: './package.json',
reporter: 'spec',
slow: 75,
timeout: 2000,
ui: 'bdd',
'watch-files': ['lib/**/*.js', '${testFolderName}/**/*.js'],
'watch-ignore': ['lib/vendor']
};`;
fs.writeFileSync('./.mocharc.js', mochaConfig);
break;
}
}
process.chdir('../../');
};

/**
* Fetch the boilerplate template of choice
*
Expand Down Expand Up @@ -209,6 +295,9 @@ const fetchTemplate = async templateBranch => {
: dockerComposeTemplate.slice(0, 7).join('\n'),
);

//Show user prompt for chosing test runner
await fetchTestRunner(templateBranch);

// Show up initial instructions to the user
showInstructions();
};
Expand Down