Skip to content

Commit

Permalink
Switch to esmock for tests (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
iambumblehead committed Jul 19, 2022
1 parent 3b6b9b1 commit 3046d0f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 33 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"node": ">=14.16"
},
"scripts": {
"test": "xo && ava"
"test": "xo && NODE_OPTIONS='--loader=esmock --no-warnings' ava"
},
"files": [
"index.js",
Expand Down Expand Up @@ -56,7 +56,7 @@
"ava": "^4.3.0",
"clear-module": "^4.1.2",
"fixture-stdout": "^0.2.1",
"mock-require": "^3.0.3",
"esmock": "^1.7.8",
"strip-ansi": "^7.0.1",
"xo": "^0.50.0"
},
Expand Down
40 changes: 17 additions & 23 deletions test/notify.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import process from 'node:process';
import {inherits} from 'node: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';
import esmock from 'esmock';

const stderr = new FixtureStdout({
stream: process.stderr,
Expand All @@ -20,16 +19,12 @@ function Control(shouldNotifyInNpmScript) {
}

const setupTest = async isNpmReturnValue => {
for (const name of ['..', 'is-npm']) {
clearModule(name);
}

process.stdout.isTTY = true;

// TODO: Switch to https://github.com/iambumblehead/esmock
mock('is-npm', {isNpmOrYarn: isNpmReturnValue || false});
const UpdateNotifier = await esmock('../update-notifier.js', {
'is-npm': {isNpmOrYarn: isNpmReturnValue || false},
});

const {default: UpdateNotifier} = await import('../update-notifier.js');
inherits(Control, UpdateNotifier);
};

Expand All @@ -45,12 +40,11 @@ test.beforeEach(async () => {
});

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

test.failing('use pretty boxen message by default', t => {
test('use pretty boxen message by default', t => {
const notifier = new Control();
notifier.notify({defer: false, isGlobal: true});

Expand All @@ -67,7 +61,7 @@ test.failing('use pretty boxen message by default', t => {
`);
});

test.failing('supports custom message', t => {
test('supports custom message', t => {
const notifier = new Control();
notifier.notify({
defer: false,
Expand All @@ -78,7 +72,7 @@ test.failing('supports custom message', t => {
t.true(stripAnsi(errorLogs).includes('custom message'));
});

test.failing('supports message with placeholders', t => {
test('supports message with placeholders', t => {
const notifier = new Control();
notifier.notify({
defer: false,
Expand All @@ -104,42 +98,42 @@ test.failing('supports message with placeholders', t => {
`);
});

test.failing('exclude -g argument when `isGlobal` option is `false`', t => {
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.failing('shouldNotifyInNpmScript should default to false', t => {
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);
test('suppress output when running as npm script', async t => {
await setupTest(true);
const notifier = new Control();
notifier.notify({defer: false});
t.false(stripAnsi(errorLogs).includes('Update available'));
});

test('should output if running as npm script and shouldNotifyInNpmScript option set', t => {
setupTest(true);
test('should output if running as npm script and shouldNotifyInNpmScript option set', async t => {
await 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);
test('should not output if current version is the latest', async t => {
await setupTest(true);
const notifier = new Control(true);
notifier.update.current = '1.0.0';
notifier.notify({defer: false});
t.false(stripAnsi(errorLogs).includes('Update available'));
});

test('should not output if current version is more recent than the reported latest', t => {
setupTest(true);
test('should not output if current version is more recent than the reported latest', async t => {
await setupTest(true);
const notifier = new Control(true);
notifier.update.current = '1.0.1';
notifier.notify({defer: false});
Expand Down
21 changes: 13 additions & 8 deletions test/update-notifier.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import process from 'node:process';
import fs from 'node:fs';
import test from 'ava';
import mockRequire from 'mock-require';
import updateNotifier from '../index.js';

mockRequire('is-ci', false);
import esmock from 'esmock';

const generateSettings = (options = {}) => ({
pkg: {
Expand All @@ -22,7 +19,6 @@ test.beforeEach(() => {
process.env.NODE_ENV = 'ava-test';

argv = [...process.argv];
configstorePath = updateNotifier(generateSettings()).config.path;
});

test.afterEach(() => {
Expand All @@ -34,30 +30,39 @@ test.afterEach(() => {
});

test('fetch info', async t => {
const updateNotifier = await esmock('../index.js', undefined, {'is-ci': false});
configstorePath = updateNotifier(generateSettings()).config.path;
const update = await updateNotifier(generateSettings()).fetchInfo();
console.log(update);
t.is(update.latest, '0.0.2');
});

test('fetch info with dist-tag', async t => {
const updateNotifier = await esmock('../index.js', undefined, {'is-ci': false});
configstorePath = updateNotifier(generateSettings()).config.path;
const update = await updateNotifier(generateSettings({distTag: '0.0.3-rc1'})).fetchInfo();
t.is(update.latest, '0.0.3-rc1');
});

test('don\'t initialize configStore when NO_UPDATE_NOTIFIER is set', t => {
test('don\'t initialize configStore when NO_UPDATE_NOTIFIER is set', async t => {
const updateNotifier = await esmock('../index.js', undefined, {'is-ci': false});
configstorePath = updateNotifier(generateSettings()).config.path;
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 => {
test('don\'t initialize configStore when --no-update-notifier is set', async t => {
const updateNotifier = await esmock('../index.js', undefined, {'is-ci': false});
configstorePath = updateNotifier(generateSettings()).config.path;
process.argv.push('--no-update-notifier');
const notifier = updateNotifier(generateSettings());
t.is(notifier.config, undefined);
});

test('don\'t initialize configStore when NODE_ENV === "test"', t => {
test('don\'t initialize configStore when NODE_ENV === "test"', async t => {
process.env.NODE_ENV = 'test';
const updateNotifier = await esmock('../index.js', undefined, {'is-ci': false});
const notifier = updateNotifier(generateSettings());
t.is(notifier.config, undefined);
});

0 comments on commit 3046d0f

Please sign in to comment.