Skip to content

Commit

Permalink
Move to AVA for testing (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
timdeschryver authored and sindresorhus committed Sep 29, 2017
1 parent 4bfb8b4 commit 6f2b074
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 164 deletions.
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -13,7 +13,7 @@
"node": ">=4"
},
"scripts": {
"test": "xo && mocha --timeout 20000"
"test": "xo && ava --timeout=20s"
},
"files": [
"index.js",
Expand Down Expand Up @@ -43,9 +43,9 @@
"xdg-basedir": "^3.0.0"
},
"devDependencies": {
"ava": "*",
"clear-module": "^2.1.0",
"fixture-stdout": "^0.2.1",
"mocha": "*",
"strip-ansi": "^3.0.1",
"xo": "^0.18.2"
}
Expand Down
162 changes: 0 additions & 162 deletions test.js

This file was deleted.

21 changes: 21 additions & 0 deletions test/fs-error.js
@@ -0,0 +1,21 @@
import clearModule from 'clear-module';
import test from 'ava';

let updateNotifier;

test.before(() => {
['.', 'configstore', 'xdg-basedir'].forEach(clearModule);
// Set configstore.config to something
// that requires root access
process.env.XDG_CONFIG_HOME = '/usr';
updateNotifier = require('../');
});

test('fail gracefully', t => {
t.notThrows(() => {
updateNotifier({
packageName: 'npme',
packageVersion: '3.7.0'
});
});
});
66 changes: 66 additions & 0 deletions test/notify.js
@@ -0,0 +1,66 @@
import util from 'util';
import clearModule from 'clear-module';
import FixtureStdout from 'fixture-stdout';
import stripAnsi from 'strip-ansi';
import test from 'ava';

const stderr = new FixtureStdout({
stream: process.stderr
});
let updateNotifier = require('../');

test.before(() => {
['.', 'is-npm'].forEach(clearModule);
['npm_config_username', 'npm_package_name', 'npm_config_heading'].forEach(name => {
delete process.env[name];
});
process.stdout.isTTY = true;
updateNotifier = require('../');
});

function Control() {
this.packageName = 'update-notifier-tester';
this.update = {
current: '0.0.2',
latest: '1.0.0'
};
}
util.inherits(Control, updateNotifier.UpdateNotifier);

let errorLogs = '';

test.beforeEach(() => {
stderr.capture(s => {
errorLogs += s;
return false;
});
});

test.afterEach(() => {
stderr.release();
errorLogs = '';
});

test('use pretty boxen message by default', t => {
const notifier = new Control();
notifier.notify({defer: false});
t.is(stripAnsi(errorLogs), [
'',
'',
' ╭───────────────────────────────────────────────────╮',
' │ │',
' │ Update available 0.0.2 → 1.0.0 │',
' │ Run npm i -g update-notifier-tester to update │',
' │ │',
' ╰───────────────────────────────────────────────────╯',
'',
''
].join('\n'));
});

test('exclude -g argument when `isGlobal` option is `false`', t => {
const notifier = new Control();
notifier.notify({defer: false, isGlobal: false});
t.not(-1, stripAnsi(errorLogs)
.indexOf('Run npm i update-notifier-tester to update'));
});
58 changes: 58 additions & 0 deletions test/update-notifier.js
@@ -0,0 +1,58 @@
import fs from 'fs';
import test from 'ava';
import updateNotifier from '../';

const generateSettings = options => {
options = options || {};
return {
pkg: {
name: 'update-notifier-tester',
version: '0.0.2'
},
callback: options.callback || null
};
};

let argv;
let configstorePath;

test.beforeEach(() => {
argv = process.argv.slice();
configstorePath = updateNotifier(generateSettings()).config.path;
});

test.afterEach(() => {
delete process.env.NO_UPDATE_NOTIFIER;
process.argv = argv;
setTimeout(() => {
fs.unlinkSync(configstorePath);
}, 10000);
});

test('check for update', async t => {
const update = await updateNotifier(generateSettings()).checkNpm();
t.is(update.current, '0.0.2');
});

test.cb('check for update with callback', t => {
t.plan(1);

updateNotifier(generateSettings({
callback: () => {
t.pass();
t.end();
}
}));
});

test('don\'t initialize configStore when NO_UPDATE_NOTIFIER is set', t => {
process.env.NO_UPDATE_NOTIFIER = '1';
const notifier = updateNotifier(generateSettings());
t.is(notifier.config, undefined);
});

test('don\'t initialize configStore when --no-update-notifier is set', t => {
process.argv.push('--no-update-notifier');
const notifier = updateNotifier(generateSettings());
t.is(notifier.config, undefined);
});

0 comments on commit 6f2b074

Please sign in to comment.