Skip to content

Commit

Permalink
e2e tests (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
ccpricenytimes authored and jaredmcdonald committed Oct 21, 2016
1 parent d05aad9 commit 46b95de
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -32,3 +32,4 @@ node_modules
# OS-specific temporary files
Thumbs.db
.DS_Store
cli-test
2 changes: 1 addition & 1 deletion .travis.yml
@@ -1,4 +1,4 @@
language: node_js
node_js:
- 6.0
script: npm run lint && npm test
script: npm run lint && npm test && npm run e2e
5 changes: 5 additions & 0 deletions e2e_tests/jest.config.json
@@ -0,0 +1,5 @@
{
"testPathDirs": [
"<rootDir>/e2e_tests"
]
}
9 changes: 9 additions & 0 deletions e2e_tests/pkg.json
@@ -0,0 +1,9 @@
{
"name": "kyt-test",
"version": "1.0.0",
"dependencies": {
"kyt": "file:../."
},
"author": "",
"license": "ISC"
}
145 changes: 145 additions & 0 deletions e2e_tests/tests/cli.test.js
@@ -0,0 +1,145 @@
const path = require('path');
const shell = require('shelljs');
const kill = require('../../utils/psKill');

const pkgJsonPath = path.join(__dirname, './../pkg.json');

describe('KYT CLI', () => {
it('installs kyt', () => {
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) {
process.exit();
}

expect(shell.test('-f', 'package.json')).toBe(true);
expect(shell.test('-d', 'node_modules')).toBe(true);
});
it('sets up a starter-kyt', () => {
let setupURL = 'git@github.com:NYTimes/kyt-starter-test.git';
if (process.env.TEST_TOKEN) {
setupURL = `https://${process.env.TEST_TOKEN}@github.com/NYTimes/kyt-starter-test.git`;
}
const output = shell.exec(`node_modules/.bin/kyt setup -r ${setupURL}`);
expect(output.code).toBe(0);
const setupArr = output.stdout.split('\n');
expect(setupArr.includes('🔥 Setting up starter-kyt')).toBe(true);
expect(setupArr.includes('👍 Added kyt scripts into your package.json scripts')).toBe(true);
expect(setupArr.includes('👍 Added new dependencies to package.json')).toBe(true);
expect(setupArr.includes('👍 Installed new modules')).toBe(true);
expect(setupArr.includes('👍 Created .eslintrc.json file')).toBe(true);
expect(setupArr.includes('👍 Created .stylelintrc.json file')).toBe(true);
expect(setupArr.includes('👍 Created kyt.config.js file')).toBe(true);
expect(setupArr.includes('👍 Created .editorconfig file')).toBe(true);
expect(setupArr.includes('👍 Created .gitignore file')).toBe(true);
expect(setupArr.includes('👍 Created src directory')).toBe(true);
});
it('sets up with the correct files', () => {
expect(shell.test('-d', 'src')).toBe(true);
expect(shell.test('-f', 'kyt.config.js')).toBe(true);
expect(shell.test('-f', '.editorconfig')).toBe(true);
expect(shell.test('-f', '.eslintrc.json')).toBe(true);
expect(shell.test('-f', '.stylelintrc.json')).toBe(true);
expect(shell.test('-f', 'prototype.js')).toBe(true);
});
it('sets up the package json scripts', () => {
// eslint-disable-next-line import/no-unresolved
const userPackageJSON = require.requireActual('../../cli-test/package.json');
const scripts = userPackageJSON.scripts;
expect(scripts.dev).toBe('kyt dev');
expect(scripts.build).toBe('kyt build');
expect(scripts.test).toBe('kyt test');
expect(scripts.lint).toBe('kyt lint');
expect(scripts['lint-style']).toBe('kyt lint-style');
expect(scripts.proto).toBe('kyt proto');
expect(scripts['kyt:help']).toBe('kyt --help');
});

it('runs the lint command', () => {
expect(true).toBe(true);
const output = shell.exec('npm run lint');
expect(output.code).toBe(0);
const outputArr = output.stdout.split('\n');
expect(outputArr.includes('✅ Your JS looks great ✨')).toBe(true);
});

it('runs the lint-style command', () => {
const output = shell.exec('node_modules/.bin/kyt lint-style');
expect(output.code).toBe(0);
const outputArr = output.stdout.split('\n');
expect(outputArr.includes('✅ Your styles look good! ✨')).toBe(true);
});

it('runs the tests command', () => {
const output = shell.exec('npm run test');
expect(output.code).toBe(0);
});

it('runs the build command', () => {
const output = shell.exec('npm run build');
expect(output.code).toBe(0);
expect(shell.test('-d', 'build')).toBe(true);
expect(shell.test('-d', 'build/server')).toBe(true);
expect(shell.test('-f', 'build/publicAssets.json')).toBe(true);
expect(shell.test('-d', 'build/public')).toBe(true);
});

// eslint-disable-next-line
window.jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000000;

it('starts the app', (done) => {
shell.exec('npm run build');
const child = shell.exec('npm run start', () => {
done();
});
child.stdout.on('data', (data) => {
if (data.includes('Server running')) {
shell.exec('sleep 3');
const output = shell.exec('curl -I localhost:3100');
expect(output.includes('200'));
kill(child.pid);
}
});
});


it('dev', (done) => {
const child = shell.exec('npm run dev', () => {
done();
});
child.stdout.on('data', (data) => {
if (data.includes('✅ Development started')) {
shell.exec('sleep 2');
const output = shell.exec('curl -I localhost:3100');
expect(output.includes('200'));
kill(child.pid);
}
});
});

it('proto', (done) => {
const child = shell.exec('npm run proto', () => {
done();
});
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');
expect(output.includes('200'));
kill(child.pid);
}
});
});

afterAll(() => {
shell.cd('..');
shell.rm('-rf', 'cli-test');
});
});
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -11,7 +11,8 @@
},
"scripts": {
"lint": "eslint .",
"test": "jest",
"test": "jest --testPathPattern /__tests__/",
"e2e": "jest --config ./e2e_tests/jest.config.json --verbose --no-cache",
"test-watch": "jest --watch",
"test-coverage": "jest --coverage"
},
Expand Down Expand Up @@ -61,6 +62,7 @@
"node-sass": "3.10.1",
"nodemon": "1.10.2",
"postcss-loader": "0.13.0",
"ps-tree": "1.1.0",
"ramda": "0.22.1",
"react-hot-loader": "3.0.0-beta.5",
"sass-loader": "4.0.2",
Expand Down
22 changes: 22 additions & 0 deletions utils/psKill.js
@@ -0,0 +1,22 @@
const psTree = require('ps-tree');

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

0 comments on commit 46b95de

Please sign in to comment.