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

WIP: Adds CLI Tests #90

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"node": ">=6.1.0"
},
"scripts": {
"test": "ava tests/**/*.test.js --verbose --serial --fail-fast",
"lint": "eslint ."
},
"repository": {
Expand Down Expand Up @@ -79,5 +80,8 @@
"webpack-hot-middleware": "^2.12.2",
"webpack-merge": "^0.14.0",
"webpack-node-externals": "^1.3.3"
},
"devDependencies": {
"ps-tree": "^1.1.0"
}
}
9 changes: 9 additions & 0 deletions tests/pkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "kyt-test",
"version": "1.0.0",
"dependencies": {
"kyt": "file:../../../."
},
"author": "",
"license": "ISC"
}
56 changes: 56 additions & 0 deletions tests/tests-e2e/02setup.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import test from 'ava';
import shell from 'shelljs';
import path from 'path';

test.before(t => {
const pkgJsonPath = path.join(__dirname, './../pkg.json');
if (shell.test('-d', 'cli-test')) {
shell.rm('-rf', 'cli-test');
}
shell.mkdir('cli-test');
shell.cd('cli-test');
shell.cp(pkgJsonPath, 'package.json');
const output = shell.exec('npm install');
if (output.code !== 0) {
console.log(output.stderr);
process.exit();
}
});
test.serial('installation', t => {
t.true(shell.test('-f', 'package.json'));
t.true(shell.test('-d', 'node_modules'));
});

test.serial('setup', t => {
const output = shell.exec('node_modules/.bin/kyt setup -r git@github.com:nytm/wf-kyt-starter-test.git');
t.is(output.code, 0);
const setupArr = output.stdout.split('\n');
t.true(setupArr.includes('🔥 Setting up starter-kyt'));
t.true(setupArr.includes('👍 Added kyt scripts into your package.json scripts'));
t.true(setupArr.includes('👍 Added new dependencies to package.json'));
t.true(setupArr.includes('ℹ️ Cleaning node modules and reinstalling. This may take a couple of minutes...'));
t.true(setupArr.includes('👍 Installed new modules'));
t.true(setupArr.includes('👍 Copied kyt default ESLint config'));
t.true(setupArr.includes('👍 Copied default Stylelint config'));
});

test.serial('setup-files', t => {
t.true(shell.test('-d', 'src'));
t.true(shell.test('-f', 'kyt.config.js'));
t.true(shell.test('-f', '.editorconfig'));
t.true(shell.test('-f', '.eslintrc'));
t.true(shell.test('-f', '.stylelintrc'));
t.true(shell.test('-f', 'prototype.js'));
});

test.serial('setup-package-json', t => {
let userPackageJSON = require('./cli-test/package.json');
let scripts = userPackageJSON.scripts;
t.is(scripts.dev, 'kyt dev');
t.is(scripts.build, 'kyt build');
t.is(scripts.test, 'kyt test');
t.is(scripts.lint, 'kyt lint');
t.is(scripts['lint-style'], 'kyt lint-style');
t.is(scripts.proto, 'kyt proto');
t.is(scripts['kyt:help'], 'kyt --help');
});
13 changes: 13 additions & 0 deletions tests/tests-e2e/03lint.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import test from 'ava';
import shell from 'shelljs';

test.before(t => {
shell.cd('cli-test');
});

test.serial('lint', t => {
let output = shell.exec('npm run lint');
t.is(output.code, 0);
const outputArr = output.stdout.split('\n');
t.false(outputArr.includes('You do not have an .eslintrc file'));
});
14 changes: 14 additions & 0 deletions tests/tests-e2e/04lintStyle.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import test from 'ava';
import shell from 'shelljs';

test.before(t => {
shell.cd('cli-test');
});

test.serial('lint-style', t => {
console.log('lintstyle');
let output = shell.exec('node_modules/.bin/kyt lint-style');
t.is(output.code, 0);
const outputArr = output.stdout.split('\n');
t.true(outputArr.includes('✅ Your styles look good! ✨'));
});
16 changes: 16 additions & 0 deletions tests/tests-e2e/05test.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import test from 'ava';
import shell from 'shelljs';

test.before(t => {
shell.cd('cli-test');
});
test.serial('test', t => {
let output = shell.exec('npm run test');
t.is(output.code, 0);
const outputArr = output.stdout.split('\n');
t.true(outputArr.includes('🔥 Running Test Command...'));
t.true(outputArr.includes('👍 Server webpack configuration compiled'));
t.true(outputArr.includes('ℹ️ Compiling...'));
t.true(outputArr.includes('👍 Server build successful'));
t.true(outputArr.includes('ℹ️ Starting test...'));
});
15 changes: 15 additions & 0 deletions tests/tests-e2e/06build.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import test from 'ava';
import shell from 'shelljs';

test.before(t => {
shell.cd('cli-test');
});

test.serial('build', t => {
let output = shell.exec('npm run build');
t.is(output.code, 0);
t.true(shell.test('-d', 'build'));
t.true(shell.test('-d', 'build/server'));
t.true(shell.test('-f', 'build/publicAssets.json'));
t.true(shell.test('-d', 'build/public'));
});
22 changes: 22 additions & 0 deletions tests/tests-e2e/07start.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import test from 'ava';
import shell from 'shelljs';
import kill from '../../utils/psKill';

test.before(t => {
shell.cd('cli-test');
});

test.cb('start', t => {
shell.exec('npm run build');
const child = shell.exec('npm run start', (code, stdout, stderr) => {
t.end();
});
child.stdout.on('data', (data) => {
if (data.includes('Server running')) {
shell.exec('sleep 3');
const output = shell.exec('curl -I localhost:3100');
t.true(output.includes('200'));
kill(child.pid);
}
});
});
44 changes: 44 additions & 0 deletions tests/tests-e2e/08dev.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import test from 'ava';
import shell from 'shelljs';
import kill from '../../utils/psKill';

test.before(t => {
shell.cd('cli-test');
});

test.cb('dev', t => {
const child = shell.exec('npm run dev', (code, stdout, stderr) => {
t.end();
});
child.stdout.on('data', (data) => {
if (data.includes('✅ Development started')) {
shell.exec('sleep 2');
const output = shell.exec('curl -I localhost:3100');
t.true(output.includes('200'));
kill(child.pid);
}
});
});

test.cb('change file and watch for reload', t => {
const child = shell.exec('npm run dev', (code, stdout, stderr) => {
t.end();
});
let stillAlive = true;
child.stdout.on('data', (data) => {
if (data.includes('✅ Development started')) {
shell.exec('sleep 2');
const output = shell.exec('curl -I localhost:3100');
t.true(output.includes('200'));
shell.exec('touch -am ./src/components/HelloWorld/index.js');
}

if (data.includes('👍 Development server restarted') && stillAlive) {
stillAlive = false;
shell.exec('sleep 3');
const output = shell.exec('curl -I localhost:3100');
t.true(output.includes('200'));
kill(child.pid);
}
});
});
28 changes: 28 additions & 0 deletions tests/tests-e2e/09proto.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import test from 'ava';
import shell from 'shelljs';
import kill from '../../utils/psKill';

test.before(t => {
shell.cd('cli-test');
});

test.cb('proto', t => {
const child = shell.exec('npm run proto', (code, stdout, stderr) => {
t.end();
});
let stillAlive = true;
child.stdout.on('data', (data) => {
if (data.includes('webpack: bundle is now VALID.') && stillAlive) {
stillAlive = false;
shell.exec('sleep 2');
const output = shell.exec('curl -I localhost:3102/prototype');
t.true(output.includes('200'));
kill(child.pid);
}
});
});

test.after.always(t => {
shell.cd('..');
shell.rm('-rf', 'cli-test');
});
24 changes: 24 additions & 0 deletions utils/psKill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var psTree = require('ps-tree');

// Loops through processes and kills them
module.exports = function (pid, signal, callback) {
signal = signal || 'SIGKILL';
callback = callback || function () {};
psTree(pid, function (err, children) {
let arr = [pid].concat(
children.map(function (p) {
return p.PID;
})
);
arr = arr.filter((item, poss) => {
return arr.indexOf(item) == poss;
});
arr.forEach(function (tpid) {
try { process.kill(tpid, signal) }
catch (ex) {
console.log('Could not kill process', tpid, ex);
}
});
callback();
});
};