Skip to content

Commit d4fcbc2

Browse files
OiYouYeahYouJason
and
Jason
authored
Tidy up tests (#294)
* Tidy up tests * Add nyc coverage tool * Remove `env-test`: it's a single line for the cost of a module * Set NODE_ENV in custom set up file * Stub console for each test individually globally * Plus: a `sinon.restore()` in global afterEach prevents cross test leaks * Add most basic test for `Tldr` class, just to trigger coverage * Remove surplus mocha args: * It always looks in test dir * `mocha.opts` provides a more consistent approach * Ignore coverage files now created by nyc * Add `'use strict'` * Fix missing semi * Use `.mocharc.js` instead of `mocha.opts` * fix package lock to use main npm registry Co-authored-by: Jason <[email protected]>
1 parent 27c6bfa commit d4fcbc2

9 files changed

+1171
-51
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ node_modules
22
.DS_Store
33
npm-debug.log
44
.idea
5+
coverage/
6+
.nyc_output

.mocharc.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
recursive: true,
3+
file:['./test/mocha.js'],
4+
};

package-lock.json

+1,136-36
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,12 @@
4141
"scripts": {
4242
"start": "node ./bin/tldr",
4343
"example": "node ./bin/tldr tar",
44-
"test": "mocha test --require=env-test",
45-
"test:quiet": "npm test --reporter=dot",
44+
"test": "mocha",
45+
"test:quiet": "mocha --reporter=dot",
4646
"lint": "eslint lib test bin/tldr",
47-
"watch": "mocha test --require=env-test --reporter=min --watch --growl",
47+
"watch": "mocha --reporter=min --watch --growl",
4848
"test:functional": "bash test/functional-test.sh",
49+
"test:coverage": "nyc mocha",
4950
"test:all": "npm run lint && npm test && npm run test:functional"
5051
},
5152
"dependencies": {
@@ -62,10 +63,10 @@
6263
"request": "^2.88.0"
6364
},
6465
"devDependencies": {
65-
"env-test": "^1.0.0",
6666
"eslint": "^5.16.0",
6767
"husky": "^2.4.1",
6868
"mocha": "^6.2.3",
69+
"nyc": "^15.0.1",
6970
"should": "^13.2.3",
7071
"sinon": "^7.3.2"
7172
},

test/mocha.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
process.env.NODE_ENV = 'test';
4+
5+
const sinon = require('sinon');
6+
7+
beforeEach(() => {
8+
sinon.stub(console);
9+
});
10+
11+
afterEach(() => {
12+
sinon.restore();
13+
});

test/mocha.opts

-3
This file was deleted.

test/render.spec.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,11 @@ describe('Render', () => {
100100
'theme': 'bad'
101101
};
102102

103-
let spy = sinon.spy(console, 'error');
104103
let text = render.toANSI({
105104
name: 'tar',
106105
description: 'archive utility',
107106
}, config);
108107
should.not.exist(text);
109-
spy.getCall(0).args[0].should.equal('invalid theme: bad');
108+
console.error.getCall(0).args[0].should.equal('invalid theme: bad');
110109
});
111110
});

test/search.spec.js

-6
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ let restoreStubs = (stubs) => {
156156
describe('Search', () => {
157157
it('should create index', function(done) {
158158
let stubs = [];
159-
sinon.stub(console, 'log').returns();
160159
stubs.push(sinon.stub(utils, 'glob').callsFake(fakes.utils.glob));
161160
stubs.push(sinon.stub(fs, 'readFile').callsFake(fakes.fs.readFile));
162161
stubs.push(sinon.stub(fs, 'writeFile').callsFake(fakes.fs.writeFile));
@@ -165,19 +164,16 @@ describe('Search', () => {
165164
Object.keys(data.invertedIndex).length.should.equal(56); // eslint-disable-line
166165
data.invertedIndex['roxi'][0].should.equal('/path/to/file-11.md');
167166
testData.corpus = data;
168-
console.log.restore();
169167
done();
170168
}).catch((error) => {
171169
should.not.exist(error);
172-
console.log.restore();
173170
done(error);
174171
}).then(() => {
175172
restoreStubs(stubs);
176173
});
177174
});
178175
it('should perform searches', function(done) {
179176
let stubs = [];
180-
sinon.stub(console, 'log').returns();
181177
stubs.push(sinon.stub(fs, 'readFile').callsFake(fakes.fs.readFile));
182178
stubs.push(sinon.stub(fs, 'writeFile').callsFake(fakes.fs.writeFile));
183179
stubs.push(sinon.stub(index, 'getShortIndex').callsFake(fakes.index.getShortIndex));
@@ -194,13 +190,11 @@ describe('Search', () => {
194190
return search.getResults('Joe and Roxie').then((data) => {
195191
data.length.should.equal(8); // eslint-disable-line
196192
data[1].file.should.equal('/path/to/file-16.md');
197-
console.log.restore();
198193
done();
199194
return Promise.resolve();
200195
});
201196
}).catch((error) => {
202197
should.not.exist(error);
203-
console.log.restore();
204198
done(error);
205199
}).then(() => {
206200
restoreStubs(stubs);

test/tldr.spec.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
const TLDR = require('../lib/tldr');
4+
5+
describe('TLDR class', () => {
6+
it('should construct', () => {
7+
const tldr = new TLDR({ cache: 'some-random-string' });
8+
tldr.should.exist;
9+
});
10+
});

0 commit comments

Comments
 (0)