/
notify.js
93 lines (78 loc) · 2.79 KB
/
notify.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import util from 'util';
import clearModule from 'clear-module';
import FixtureStdout from 'fixture-stdout';
import stripAnsi from 'strip-ansi';
import test from 'ava';
import mock from 'mock-require';
const stderr = new FixtureStdout({
stream: process.stderr
});
function Control(shouldNotifyInNpmScript) {
this.packageName = 'update-notifier-tester';
this.update = {
current: '0.0.2',
latest: '1.0.0'
};
this.shouldNotifyInNpmScript = shouldNotifyInNpmScript;
}
const setupTest = isNpmReturnValue => {
['..', 'is-npm'].forEach(clearModule);
process.stdout.isTTY = true;
mock('is-npm', {isNpmOrYarn: isNpmReturnValue || false});
const updateNotifier = require('..');
util.inherits(Control, updateNotifier.UpdateNotifier);
};
let errorLogs = '';
test.beforeEach(() => {
setupTest();
stderr.capture(s => {
errorLogs += s;
return false;
});
});
test.afterEach(() => {
mock.stopAll();
stderr.release();
errorLogs = '';
});
test('use pretty boxen message by default', t => {
const notifier = new Control();
notifier.notify({defer: false, isGlobal: true});
t.is(stripAnsi(errorLogs), `
╭───────────────────────────────────────────────────╮
│ │
│ Update available 0.0.2 → 1.0.0 │
│ Run npm i -g update-notifier-tester to update │
│ │
╰───────────────────────────────────────────────────╯
`);
});
test('exclude -g argument when `isGlobal` option is `false`', t => {
const notifier = new Control();
notifier.notify({defer: false, isGlobal: false});
t.not(stripAnsi(errorLogs).indexOf('Run npm i update-notifier-tester to update'), -1);
});
test('shouldNotifyInNpmScript should default to false', t => {
const notifier = new Control();
notifier.notify({defer: false});
t.not(stripAnsi(errorLogs).indexOf('Update available'), -1);
});
test('suppress output when running as npm script', t => {
setupTest(true);
const notifier = new Control();
notifier.notify({defer: false});
t.false(stripAnsi(errorLogs).includes('Update available'));
});
test('should ouput if running as npm script and shouldNotifyInNpmScript option set', t => {
setupTest(true);
const notifier = new Control(true);
notifier.notify({defer: false});
t.true(stripAnsi(errorLogs).includes('Update available'));
});
test('should not output if current version is the latest', t => {
setupTest(true);
const notifier = new Control(true);
notifier.update.current = '1.0.0';
notifier.notify({defer: false});
t.false(stripAnsi(errorLogs).includes('Update available'));
});