Skip to content

Commit

Permalink
add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jehy committed Jul 30, 2020
1 parent a943400 commit 7055e23
Show file tree
Hide file tree
Showing 4 changed files with 458 additions and 6 deletions.
5 changes: 3 additions & 2 deletions index.js
Expand Up @@ -3,7 +3,7 @@
'use strict';

const program = require('commander');
const {init, processData, output} = require('./lib.js');
const {init, processData, output, getPackageFiles} = require('./lib.js');

program
.usage('[moduleName] [options]')
Expand All @@ -12,5 +12,6 @@ program
.parse(process.argv);

const options = init(program);
const {worst, data} = processData(options);
const allPackageFiles = getPackageFiles();
const {worst, data} = processData(allPackageFiles, options);
output(worst, data, options);
10 changes: 8 additions & 2 deletions lib.js
Expand Up @@ -58,8 +58,13 @@ function init(opts = {}) {
return options;
}

function processData(options) {
const allPackageFiles = glob.sync('node_modules/**/package.json', process.cwd());
/* istanbul ignore next */
function getPackageFiles()
{
return glob.sync('node_modules/**/package.json', process.cwd());
}

function processData(allPackageFiles, options) {
const data = allPackageFiles.reduce((res, file)=>{
const child = JSON.parse(fs.readFileSync(file, 'utf8'));
let parent;
Expand Down Expand Up @@ -141,4 +146,5 @@ module.exports = {
output,
getAdvice,
printModulesInfo,
getPackageFiles,
};
32 changes: 30 additions & 2 deletions test/index.js
Expand Up @@ -2,8 +2,36 @@

'use strict';

describe('All your bases', () => {
const {assert} = require('chai');
const fs = require('fs');
const path = require('path');

it('should belong to us', () => {
const funcs = require('../lib');

describe('WtfWith', () => {

it('should be able to get some advices', () => {
for (let n = 0; n < 1000; n++)
{
const advice1 = funcs.getAdvice('badModuleName', true);
const advice2 = funcs.getAdvice('badModuleName', false);
const advice3 = funcs.getAdvice('everything', false);
assert.isOk(advice1);
assert.isOk(advice2);
assert.isOk(advice3);
}
});

const options = {minSearch: 3, arg: 'everything'};
let worst;
let data;
it('should be able to process data', () => {
const files = JSON.parse(fs.readFileSync(path.join(__dirname, './packageDataSample.json'), 'utf8'));
const res = funcs.processData(files, options);
worst = res.worst;
data = res.data;
});
it('should be able to output data', () => {
funcs.output(worst, data, options);
});
});

0 comments on commit 7055e23

Please sign in to comment.