Skip to content

Commit

Permalink
✅ Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gluons committed May 10, 2017
1 parent 2db78c2 commit d124dd1
Show file tree
Hide file tree
Showing 11 changed files with 359 additions and 18 deletions.
5 changes: 4 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": "gluons/es6"
"extends": "gluons/es6",
"env": {
"mocha": true
}
}
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
"npm": ">=3"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"pretest": "node ./script/prepare-test.js",
"test": "mocha",
"posttest": "node ./script/clean-test.js"
},
"repository": {
"type": "git",
Expand All @@ -31,8 +33,10 @@
},
"homepage": "https://github.com/gluons/npm-reinstall",
"devDependencies": {
"chai": "*",
"eslint": "^3.19.0",
"eslint-config-gluons": "^2.0.1"
"eslint-config-gluons": "^2.0.1",
"mocha": "*"
},
"dependencies": {
"chalk": "^1.1.3",
Expand Down
24 changes: 24 additions & 0 deletions script/clean-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const chalk = require('chalk');
const rimraf = require('rimraf');
const path = require('path');

const depsNodeModulesPath = path.resolve(__dirname, '../test/deps/node_modules');
const devDepsNodeModulesPath = path.resolve(__dirname, '../test/dev-deps/node_modules');

console.log(chalk.cyan('Cleaning test...'));

rimraf(depsNodeModulesPath, err => {
if (err) {
console.log(chalk.red('Clean dependencies fail.'));
} else {
console.log(chalk.green('Clean dependencies succeed.'));
}
});

rimraf(devDepsNodeModulesPath, err => {
if (err) {
console.log(chalk.red('Clean devDependencies fail.'));
} else {
console.log(chalk.green('Clean devDependencies succeed.'));
}
});
34 changes: 34 additions & 0 deletions script/prepare-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const chalk = require('chalk');
const spawn = require('cross-spawn');
const path = require('path');

const depsPath = path.resolve(__dirname, '../test/deps');
const devDepsPath = path.resolve(__dirname, '../test/dev-deps');

console.log(chalk.cyan('Preparing test...'));

let depsChild = spawn('npm', ['install'], {
cwd: depsPath,
stdio: 'ignore'
});

let devDepsChild = spawn('npm', ['install'], {
cwd: devDepsPath,
stdio: 'ignore'
});

depsChild.on('close', code => {
if (code === 0) {
console.log(chalk.green('Prepare dependencies succeed.'));
} else {
console.log(chalk.red('Prepare dependencies fail.'));
}
});

devDepsChild.on('close', code => {
if (code === 0) {
console.log(chalk.green('Prepare devDependencies succeed.'));
} else {
console.log(chalk.red('Prepare devDependencies fail.'));
}
});
41 changes: 41 additions & 0 deletions test/deps-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const expect = require('chai').expect;
const spawn = require('cross-spawn');
const path = require('path');

const deps = require('./deps');

let cwd = path.resolve(__dirname, './deps');
let reinstallPath = path.resolve(__dirname, '../reinstall');

describe('Dependencies', function () {
this.slow(5000);
this.timeout(10000);

it('should have dependencies before reinstallation', () => {
let result = deps();
expect(result).to.exist;
});
it('should have dependencies after reinstallation', () => new Promise((resolve, reject) => {
let child = spawn('node', [reinstallPath, '--save', 'vue', 'vue-router'], {
cwd
});
let isErrorThrown = false;

child.on('error', err => {
if (!isErrorThrown) {
isErrorThrown = true;
reject(err);
}
});
child.on('close', code => {
if (code === 0) {
resolve();
} else {
if (!isErrorThrown) {
isErrorThrown = true;
reject(code);
}
}
});
}));
});
8 changes: 8 additions & 0 deletions test/deps/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = function () {
let Vue = require('vue');
let VueRouter = require('vue-router');

Vue.use(VueRouter);

return new Vue();
};
17 changes: 17 additions & 0 deletions test/deps/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "deps",
"version": "1.0.0",
"description": "Deps",
"main": "./index.js",
"private": true,
"scripts": {
"test": "exit 0"
},
"keywords": [],
"author": "Saran Tanpituckpong <sarunta@gmail.com>",
"license": "UNLICENSED",
"dependencies": {
"vue": "^2.3.3",
"vue-router": "^2.5.3"
}
}
41 changes: 41 additions & 0 deletions test/dev-deps-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const expect = require('chai').expect;
const spawn = require('cross-spawn');
const path = require('path');

const devDeps = require('./dev-deps');

let cwd = path.resolve(__dirname, './dev-deps');
let reinstallPath = path.resolve(__dirname, '../reinstall');

describe('Dev Dependencies', function () {
this.slow(5000);
this.timeout(10000);

it('should have devDependencies before reinstallation', () => {
let result = devDeps();
expect(result).to.equal(1);
});
it('should have devDependencies after reinstallation', () => new Promise((resolve, reject) => {
let child = spawn('node', [reinstallPath, '--save-dev', 'evl', 'nvl'], {
cwd
});
let isErrorThrown = false;

child.on('error', err => {
if (!isErrorThrown) {
isErrorThrown = true;
reject(err);
}
});
child.on('close', code => {
if (code === 0) {
resolve();
} else {
if (!isErrorThrown) {
isErrorThrown = true;
reject(code);
}
}
});
}));
});
11 changes: 11 additions & 0 deletions test/dev-deps/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = function () {
let evl = require('evl');
let nvl = require('nvl');

return evl(
() => {
throw new Error();
},
() => nvl(null, 1)
);
};
17 changes: 17 additions & 0 deletions test/dev-deps/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "dev-deps",
"version": "1.0.0",
"description": "Dev Deps",
"main": "./index.js",
"private": true,
"scripts": {
"test": "exit 0"
},
"keywords": [],
"author": "Saran Tanpituckpong <sarunta@gmail.com>",
"license": "UNLICENSED",
"devDependencies": {
"evl": "^1.0.1",
"nvl": "^1.0.1"
}
}
Loading

0 comments on commit d124dd1

Please sign in to comment.