Skip to content

Commit 3046d0f

Browse files
authoredJul 19, 2022
Switch to esmock for tests (#225)
1 parent 3b6b9b1 commit 3046d0f

File tree

3 files changed

+32
-33
lines changed

3 files changed

+32
-33
lines changed
 

‎package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"node": ">=14.16"
1717
},
1818
"scripts": {
19-
"test": "xo && ava"
19+
"test": "xo && NODE_OPTIONS='--loader=esmock --no-warnings' ava"
2020
},
2121
"files": [
2222
"index.js",
@@ -56,7 +56,7 @@
5656
"ava": "^4.3.0",
5757
"clear-module": "^4.1.2",
5858
"fixture-stdout": "^0.2.1",
59-
"mock-require": "^3.0.3",
59+
"esmock": "^1.7.8",
6060
"strip-ansi": "^7.0.1",
6161
"xo": "^0.50.0"
6262
},

‎test/notify.js

+17-23
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import process from 'node:process';
22
import {inherits} from 'node:util';
3-
import clearModule from 'clear-module';
43
import FixtureStdout from 'fixture-stdout';
54
import stripAnsi from 'strip-ansi';
65
import test from 'ava';
7-
import mock from 'mock-require';
6+
import esmock from 'esmock';
87

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

2221
const setupTest = async isNpmReturnValue => {
23-
for (const name of ['..', 'is-npm']) {
24-
clearModule(name);
25-
}
26-
2722
process.stdout.isTTY = true;
2823

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

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

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

4742
test.afterEach(() => {
48-
mock.stopAll();
4943
stderr.release();
5044
errorLogs = '';
5145
});
5246

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

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

70-
test.failing('supports custom message', t => {
64+
test('supports custom message', t => {
7165
const notifier = new Control();
7266
notifier.notify({
7367
defer: false,
@@ -78,7 +72,7 @@ test.failing('supports custom message', t => {
7872
t.true(stripAnsi(errorLogs).includes('custom message'));
7973
});
8074

81-
test.failing('supports message with placeholders', t => {
75+
test('supports message with placeholders', t => {
8276
const notifier = new Control();
8377
notifier.notify({
8478
defer: false,
@@ -104,42 +98,42 @@ test.failing('supports message with placeholders', t => {
10498
`);
10599
});
106100

107-
test.failing('exclude -g argument when `isGlobal` option is `false`', t => {
101+
test('exclude -g argument when `isGlobal` option is `false`', t => {
108102
const notifier = new Control();
109103
notifier.notify({defer: false, isGlobal: false});
110104
t.not(stripAnsi(errorLogs).indexOf('Run npm i update-notifier-tester to update'), -1);
111105
});
112106

113-
test.failing('shouldNotifyInNpmScript should default to false', t => {
107+
test('shouldNotifyInNpmScript should default to false', t => {
114108
const notifier = new Control();
115109
notifier.notify({defer: false});
116110
t.not(stripAnsi(errorLogs).indexOf('Update available'), -1);
117111
});
118112

119-
test('suppress output when running as npm script', t => {
120-
setupTest(true);
113+
test('suppress output when running as npm script', async t => {
114+
await setupTest(true);
121115
const notifier = new Control();
122116
notifier.notify({defer: false});
123117
t.false(stripAnsi(errorLogs).includes('Update available'));
124118
});
125119

126-
test('should output if running as npm script and shouldNotifyInNpmScript option set', t => {
127-
setupTest(true);
120+
test('should output if running as npm script and shouldNotifyInNpmScript option set', async t => {
121+
await setupTest(true);
128122
const notifier = new Control(true);
129123
notifier.notify({defer: false});
130124
t.true(stripAnsi(errorLogs).includes('Update available'));
131125
});
132126

133-
test('should not output if current version is the latest', t => {
134-
setupTest(true);
127+
test('should not output if current version is the latest', async t => {
128+
await setupTest(true);
135129
const notifier = new Control(true);
136130
notifier.update.current = '1.0.0';
137131
notifier.notify({defer: false});
138132
t.false(stripAnsi(errorLogs).includes('Update available'));
139133
});
140134

141-
test('should not output if current version is more recent than the reported latest', t => {
142-
setupTest(true);
135+
test('should not output if current version is more recent than the reported latest', async t => {
136+
await setupTest(true);
143137
const notifier = new Control(true);
144138
notifier.update.current = '1.0.1';
145139
notifier.notify({defer: false});

‎test/update-notifier.js

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import process from 'node:process';
22
import fs from 'node:fs';
33
import test from 'ava';
4-
import mockRequire from 'mock-require';
5-
import updateNotifier from '../index.js';
6-
7-
mockRequire('is-ci', false);
4+
import esmock from 'esmock';
85

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

2421
argv = [...process.argv];
25-
configstorePath = updateNotifier(generateSettings()).config.path;
2622
});
2723

2824
test.afterEach(() => {
@@ -34,30 +30,39 @@ test.afterEach(() => {
3430
});
3531

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

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

47-
test('don\'t initialize configStore when NO_UPDATE_NOTIFIER is set', t => {
47+
test('don\'t initialize configStore when NO_UPDATE_NOTIFIER is set', async t => {
48+
const updateNotifier = await esmock('../index.js', undefined, {'is-ci': false});
49+
configstorePath = updateNotifier(generateSettings()).config.path;
4850
process.env.NO_UPDATE_NOTIFIER = '1';
4951
const notifier = updateNotifier(generateSettings());
5052
t.is(notifier.config, undefined);
5153
});
5254

53-
test('don\'t initialize configStore when --no-update-notifier is set', t => {
55+
test('don\'t initialize configStore when --no-update-notifier is set', async t => {
56+
const updateNotifier = await esmock('../index.js', undefined, {'is-ci': false});
57+
configstorePath = updateNotifier(generateSettings()).config.path;
5458
process.argv.push('--no-update-notifier');
5559
const notifier = updateNotifier(generateSettings());
5660
t.is(notifier.config, undefined);
5761
});
5862

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

0 commit comments

Comments
 (0)
Please sign in to comment.