Skip to content

Commit 6f2b074

Browse files
timdeschryversindresorhus
authored andcommittedSep 29, 2017
Move to AVA for testing (#121)
1 parent 4bfb8b4 commit 6f2b074

File tree

5 files changed

+147
-164
lines changed

5 files changed

+147
-164
lines changed
 

‎package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"node": ">=4"
1414
},
1515
"scripts": {
16-
"test": "xo && mocha --timeout 20000"
16+
"test": "xo && ava --timeout=20s"
1717
},
1818
"files": [
1919
"index.js",
@@ -43,9 +43,9 @@
4343
"xdg-basedir": "^3.0.0"
4444
},
4545
"devDependencies": {
46+
"ava": "*",
4647
"clear-module": "^2.1.0",
4748
"fixture-stdout": "^0.2.1",
48-
"mocha": "*",
4949
"strip-ansi": "^3.0.1",
5050
"xo": "^0.18.2"
5151
}

‎test.js

-162
This file was deleted.

‎test/fs-error.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import clearModule from 'clear-module';
2+
import test from 'ava';
3+
4+
let updateNotifier;
5+
6+
test.before(() => {
7+
['.', 'configstore', 'xdg-basedir'].forEach(clearModule);
8+
// Set configstore.config to something
9+
// that requires root access
10+
process.env.XDG_CONFIG_HOME = '/usr';
11+
updateNotifier = require('../');
12+
});
13+
14+
test('fail gracefully', t => {
15+
t.notThrows(() => {
16+
updateNotifier({
17+
packageName: 'npme',
18+
packageVersion: '3.7.0'
19+
});
20+
});
21+
});

‎test/notify.js

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import util from 'util';
2+
import clearModule from 'clear-module';
3+
import FixtureStdout from 'fixture-stdout';
4+
import stripAnsi from 'strip-ansi';
5+
import test from 'ava';
6+
7+
const stderr = new FixtureStdout({
8+
stream: process.stderr
9+
});
10+
let updateNotifier = require('../');
11+
12+
test.before(() => {
13+
['.', 'is-npm'].forEach(clearModule);
14+
['npm_config_username', 'npm_package_name', 'npm_config_heading'].forEach(name => {
15+
delete process.env[name];
16+
});
17+
process.stdout.isTTY = true;
18+
updateNotifier = require('../');
19+
});
20+
21+
function Control() {
22+
this.packageName = 'update-notifier-tester';
23+
this.update = {
24+
current: '0.0.2',
25+
latest: '1.0.0'
26+
};
27+
}
28+
util.inherits(Control, updateNotifier.UpdateNotifier);
29+
30+
let errorLogs = '';
31+
32+
test.beforeEach(() => {
33+
stderr.capture(s => {
34+
errorLogs += s;
35+
return false;
36+
});
37+
});
38+
39+
test.afterEach(() => {
40+
stderr.release();
41+
errorLogs = '';
42+
});
43+
44+
test('use pretty boxen message by default', t => {
45+
const notifier = new Control();
46+
notifier.notify({defer: false});
47+
t.is(stripAnsi(errorLogs), [
48+
'',
49+
'',
50+
' ╭───────────────────────────────────────────────────╮',
51+
' │ │',
52+
' │ Update available 0.0.2 → 1.0.0 │',
53+
' │ Run npm i -g update-notifier-tester to update │',
54+
' │ │',
55+
' ╰───────────────────────────────────────────────────╯',
56+
'',
57+
''
58+
].join('\n'));
59+
});
60+
61+
test('exclude -g argument when `isGlobal` option is `false`', t => {
62+
const notifier = new Control();
63+
notifier.notify({defer: false, isGlobal: false});
64+
t.not(-1, stripAnsi(errorLogs)
65+
.indexOf('Run npm i update-notifier-tester to update'));
66+
});

‎test/update-notifier.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import fs from 'fs';
2+
import test from 'ava';
3+
import updateNotifier from '../';
4+
5+
const generateSettings = options => {
6+
options = options || {};
7+
return {
8+
pkg: {
9+
name: 'update-notifier-tester',
10+
version: '0.0.2'
11+
},
12+
callback: options.callback || null
13+
};
14+
};
15+
16+
let argv;
17+
let configstorePath;
18+
19+
test.beforeEach(() => {
20+
argv = process.argv.slice();
21+
configstorePath = updateNotifier(generateSettings()).config.path;
22+
});
23+
24+
test.afterEach(() => {
25+
delete process.env.NO_UPDATE_NOTIFIER;
26+
process.argv = argv;
27+
setTimeout(() => {
28+
fs.unlinkSync(configstorePath);
29+
}, 10000);
30+
});
31+
32+
test('check for update', async t => {
33+
const update = await updateNotifier(generateSettings()).checkNpm();
34+
t.is(update.current, '0.0.2');
35+
});
36+
37+
test.cb('check for update with callback', t => {
38+
t.plan(1);
39+
40+
updateNotifier(generateSettings({
41+
callback: () => {
42+
t.pass();
43+
t.end();
44+
}
45+
}));
46+
});
47+
48+
test('don\'t initialize configStore when NO_UPDATE_NOTIFIER is set', t => {
49+
process.env.NO_UPDATE_NOTIFIER = '1';
50+
const notifier = updateNotifier(generateSettings());
51+
t.is(notifier.config, undefined);
52+
});
53+
54+
test('don\'t initialize configStore when --no-update-notifier is set', t => {
55+
process.argv.push('--no-update-notifier');
56+
const notifier = updateNotifier(generateSettings());
57+
t.is(notifier.config, undefined);
58+
});

0 commit comments

Comments
 (0)
Please sign in to comment.