Skip to content

Commit 87f67d9

Browse files
committed
chore(tests): expose real mock npm object
Consolidates existing "real npm" mocks, labels it real. We can now migrate tests to this one at a time instead of trying to make one giant PR that does it all at once. PR-URL: #3458 Credit: @wraithgar Close: #3458 Reviewed-by: @nlf
1 parent d0f50b1 commit 87f67d9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+160
-166
lines changed

test/fixtures/mock-npm.js

+53-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,52 @@
1-
// Basic npm fixture that you can give a config object that acts like
2-
// npm.config You still need a separate flatOptions but this is the first step
3-
// to eventually just using npm itself
1+
const npmlog = require('npmlog')
2+
const perf = require('../../lib/utils/perf.js')
3+
perf.reset()
4+
const procLog = require('../../lib/utils/proc-log-listener.js')
5+
procLog.reset()
6+
7+
const realLog = {}
8+
for (const level of ['silly', 'verbose', 'timing', 'notice', 'warn', 'error'])
9+
realLog[level] = npmlog[level]
10+
11+
const { title, execPath } = process
12+
13+
const RealMockNpm = (t, otherMocks = {}) => {
14+
t.teardown(() => {
15+
for (const level of ['silly', 'verbose', 'timing', 'notice', 'warn', 'error'])
16+
npmlog[level] = realLog[level]
17+
perf.reset()
18+
procLog.reset()
19+
process.title = title
20+
process.execPath = execPath
21+
delete process.env.npm_command
22+
delete process.env.COLOR
23+
})
24+
const logs = []
25+
const outputs = []
26+
const npm = t.mock('../../lib/npm.js', otherMocks)
27+
const command = async (command, args = []) => {
28+
return new Promise((resolve, reject) => {
29+
npm.commands[command](args, err => {
30+
if (err)
31+
return reject(err)
32+
return resolve()
33+
})
34+
})
35+
}
36+
for (const level of ['silly', 'verbose', 'timing', 'notice', 'warn', 'error']) {
37+
npmlog[level] = (...msg) => {
38+
logs.push([level, ...msg])
39+
}
40+
}
41+
npm.output = (...msg) => outputs.push(msg)
42+
return { npm, logs, outputs, command }
43+
}
444

545
const realConfig = require('../../lib/utils/config')
646

47+
// Basic npm fixture that you can give a config object that acts like
48+
// npm.config You still need a separate flatOptions. Tests should migrate to
49+
// using the real npm mock above
750
class MockNpm {
851
constructor (base = {}) {
952
this._mockOutputs = []
@@ -51,7 +94,11 @@ class MockNpm {
5194
}
5295
}
5396

54-
// TODO export MockNpm, and change tests to use new MockNpm()
55-
module.exports = (base = {}) => {
56-
return new MockNpm(base)
97+
const FakeMockNpm = (base = {}) => {
98+
return new MockNpm(base)
99+
}
100+
101+
module.exports = {
102+
fake: FakeMockNpm,
103+
real: RealMockNpm
57104
}

test/lib/audit.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const t = require('tap')
2-
const mockNpm = require('../fixtures/mock-npm')
2+
const { fake: mockNpm } = require('../fixtures/mock-npm')
33

44
t.test('should audit using Arborist', t => {
55
let ARB_ARGS = null

test/lib/bin.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const t = require('tap')
2-
const mockNpm = require('../fixtures/mock-npm')
2+
const { fake: mockNpm } = require('../fixtures/mock-npm')
33

44
t.test('bin', (t) => {
55
t.plan(4)

test/lib/birthday.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const t = require('tap')
2-
const mockNpm = require('../fixtures/mock-npm')
2+
const { fake: mockNpm } = require('../fixtures/mock-npm')
33

44
const config = {
55
yes: false,

test/lib/cache.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const t = require('tap')
2-
const mockNpm = require('../fixtures/mock-npm')
2+
const { fake: mockNpm } = require('../fixtures/mock-npm.js')
33
const path = require('path')
44

55
const usageUtil = () => 'usage instructions'

test/lib/ci.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const readdir = util.promisify(fs.readdir)
44

55
const t = require('tap')
66

7-
const mockNpm = require('../fixtures/mock-npm')
7+
const { fake: mockNpm } = require('../fixtures/mock-npm')
88

99
t.test('should ignore scripts with --ignore-scripts', (t) => {
1010
const SCRIPTS = []

test/lib/cli.js

+11-21
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
const t = require('tap')
22

3-
// NOTE lib/npm.js is wrapped in t.mock() every time because it's currently a
4-
// singleton. In the next semver major we will export the class and lib/cli.js
5-
// can call `new` on it and then we won't have to do that anymore
3+
const { real: mockNpm } = require('../fixtures/mock-npm.js')
64

75
const unsupportedMock = {
86
checkForBrokenNode: () => {},
@@ -36,13 +34,6 @@ const cliMock = (npm) => t.mock('../../lib/cli.js', {
3634
npmlog: npmlogMock,
3735
})
3836

39-
const npmOutputs = []
40-
const npmMock = () => {
41-
const npm = t.mock('../../lib/npm.js')
42-
npm.output = (...msg) => npmOutputs.push(msg)
43-
return npm
44-
}
45-
4637
const processMock = (proc) => {
4738
const mocked = {
4839
...process,
@@ -59,7 +50,6 @@ const { argv } = process
5950
t.afterEach(() => {
6051
logs.length = 0
6152
process.argv = argv
62-
npmOutputs.length = 0
6353
exitHandlerCalled = null
6454
exitHandlerNpm = null
6555
})
@@ -70,7 +60,7 @@ t.test('print the version, and treat npm_g as npm -g', async t => {
7060
version: process.version,
7161
})
7262

73-
const npm = npmMock()
63+
const { npm, outputs } = mockNpm(t)
7464
const cli = cliMock(npm)
7565
await cli(proc)
7666

@@ -82,15 +72,15 @@ t.test('print the version, and treat npm_g as npm -g', async t => {
8272
['info', 'using', 'npm@%s', npm.version],
8373
['info', 'using', 'node@%s', process.version],
8474
])
85-
t.strictSame(npmOutputs, [[npm.version]])
75+
t.strictSame(outputs, [[npm.version]])
8676
t.strictSame(exitHandlerCalled, [])
8777
})
8878

8979
t.test('calling with --versions calls npm version with no args', async t => {
9080
const proc = processMock({
9181
argv: ['node', 'npm', 'install', 'or', 'whatever', '--versions'],
9282
})
93-
const npm = npmMock()
83+
const { npm, outputs } = mockNpm(t)
9484
const cli = cliMock(npm)
9585

9686
let versionArgs
@@ -110,7 +100,7 @@ t.test('calling with --versions calls npm version with no args', async t => {
110100
['info', 'using', 'node@%s', process.version],
111101
])
112102

113-
t.strictSame(npmOutputs, [])
103+
t.strictSame(outputs, [])
114104
t.strictSame(exitHandlerCalled, [])
115105
})
116106

@@ -119,10 +109,10 @@ t.test('print usage if no params provided', async t => {
119109
argv: ['node', 'npm'],
120110
})
121111

122-
const npm = npmMock()
112+
const { npm, outputs } = mockNpm(t)
123113
const cli = cliMock(npm)
124114
await cli(proc)
125-
t.match(npmOutputs[0][0], 'Usage:', 'outputs npm usage')
115+
t.match(outputs[0][0], 'Usage:', 'outputs npm usage')
126116
t.match(exitHandlerCalled, [], 'should call exitHandler with no args')
127117
t.ok(exitHandlerNpm, 'exitHandler npm is set')
128118
t.match(proc.exitCode, 1)
@@ -133,11 +123,11 @@ t.test('print usage if non-command param provided', async t => {
133123
argv: ['node', 'npm', 'tset'],
134124
})
135125

136-
const npm = npmMock()
126+
const { npm, outputs } = mockNpm(t)
137127
const cli = cliMock(npm)
138128
await cli(proc)
139-
t.match(npmOutputs[0][0], 'Unknown command: "tset"')
140-
t.match(npmOutputs[0][0], 'Did you mean this?')
129+
t.match(outputs[0][0], 'Unknown command: "tset"')
130+
t.match(outputs[0][0], 'Did you mean this?')
141131
t.match(exitHandlerCalled, [], 'should call exitHandler with no args')
142132
t.ok(exitHandlerNpm, 'exitHandler npm is set')
143133
t.match(proc.exitCode, 1)
@@ -148,7 +138,7 @@ t.test('load error calls error handler', async t => {
148138
argv: ['node', 'npm', 'asdf'],
149139
})
150140

151-
const npm = npmMock()
141+
const { npm } = mockNpm(t)
152142
const cli = cliMock(npm)
153143
const er = new Error('test load error')
154144
npm.load = () => Promise.reject(er)

test/lib/dedupe.js

+25-39
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
const t = require('tap')
2-
const mockNpm = require('../fixtures/mock-npm')
2+
const { real: mockNpm } = require('../fixtures/mock-npm')
33

4-
t.test('should throw in global mode', (t) => {
5-
const Dedupe = t.mock('../../lib/dedupe.js')
6-
const npm = mockNpm({
7-
config: { 'dry-run': false, global: true },
8-
})
9-
const dedupe = new Dedupe(npm)
10-
11-
dedupe.exec([], er => {
12-
t.match(er, { code: 'EDEDUPEGLOBAL' }, 'throws EDEDUPEGLOBAL')
13-
t.end()
14-
})
4+
t.test('should throw in global mode', async (t) => {
5+
const { npm, command } = mockNpm(t)
6+
await npm.load()
7+
npm.config.set('global', true)
8+
t.rejects(
9+
command('dedupe'),
10+
{ code: 'EDEDUPEGLOBAL' },
11+
'throws EDEDUPEGLOBALE'
12+
)
1513
})
1614

17-
t.test('should remove dupes using Arborist', (t) => {
18-
const Dedupe = t.mock('../../lib/dedupe.js', {
15+
t.test('should remove dupes using Arborist', async (t) => {
16+
t.plan(5)
17+
const { npm, command } = mockNpm(t, {
1918
'@npmcli/arborist': function (args) {
2019
t.ok(args, 'gets options object')
2120
t.ok(args.path, 'gets path option')
@@ -28,37 +27,24 @@ t.test('should remove dupes using Arborist', (t) => {
2827
t.ok(arb, 'gets arborist tree')
2928
},
3029
})
31-
const npm = mockNpm({
32-
prefix: 'foo',
33-
config: {
34-
'dry-run': 'true',
35-
},
36-
})
37-
const dedupe = new Dedupe(npm)
38-
dedupe.exec([], er => {
39-
if (er)
40-
throw er
41-
t.ok(true, 'callback is called')
42-
t.end()
43-
})
30+
await npm.load()
31+
npm.config.set('prefix', 'foo')
32+
npm.config.set('dry-run', 'true')
33+
await command('dedupe')
4434
})
4535

46-
t.test('should remove dupes using Arborist - no arguments', (t) => {
47-
const Dedupe = t.mock('../../lib/dedupe.js', {
36+
t.test('should remove dupes using Arborist - no arguments', async (t) => {
37+
t.plan(1)
38+
const { npm, command } = mockNpm(t, {
4839
'@npmcli/arborist': function (args) {
4940
t.ok(args.dryRun, 'gets dryRun from config')
5041
this.dedupe = () => {}
5142
},
5243
'../../lib/utils/reify-output.js': () => {},
44+
'../../lib/utils/reify-finish.js': () => {},
5345
})
54-
const npm = mockNpm({
55-
prefix: 'foo',
56-
config: {
57-
'dry-run': 'true',
58-
},
59-
})
60-
const dedupe = new Dedupe(npm)
61-
dedupe.exec(null, () => {
62-
t.end()
63-
})
46+
await npm.load()
47+
npm.config.set('prefix', 'foo')
48+
npm.config.set('dry-run', true)
49+
await command('dedupe')
6450
})

test/lib/diff.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const { resolve, join } = require('path')
21
const t = require('tap')
3-
const mockNpm = require('../fixtures/mock-npm')
2+
const { resolve, join } = require('path')
3+
const { fake: mockNpm } = require('../fixtures/mock-npm')
44

55
const noop = () => null
66
let libnpmdiff = noop

test/lib/dist-tag.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const mockNpm = require('../fixtures/mock-npm')
21
const t = require('tap')
2+
const { fake: mockNpm } = require('../fixtures/mock-npm')
33

44
let result = ''
55
let log = ''

test/lib/docs.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const t = require('tap')
2-
const mockNpm = require('../fixtures/mock-npm.js')
2+
const { fake: mockNpm } = require('../fixtures/mock-npm.js')
33
const { join, sep } = require('path')
44

55
const pkgDirs = t.testdir({

test/lib/exec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const t = require('tap')
2-
const mockNpm = require('../fixtures/mock-npm')
2+
const { fake: mockNpm } = require('../fixtures/mock-npm')
33
const { resolve, delimiter } = require('path')
44

55
const ARB_CTOR = []

test/lib/fund.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const t = require('tap')
2-
const mockNpm = require('../fixtures/mock-npm')
2+
const { fake: mockNpm } = require('../fixtures/mock-npm')
33

44
const version = '1.0.0'
55
const funding = {

test/lib/help-search.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const t = require('tap')
22
const { join } = require('path')
3-
const mockNpm = require('../fixtures/mock-npm')
3+
const { fake: mockNpm } = require('../fixtures/mock-npm')
44
const ansicolors = require('ansicolors')
55

66
const OUTPUT = []

test/lib/init.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
const t = require('tap')
12
const fs = require('fs')
23
const { resolve } = require('path')
3-
const t = require('tap')
4-
const mockNpm = require('../fixtures/mock-npm')
4+
const { fake: mockNpm } = require('../fixtures/mock-npm')
55

66
const npmLog = {
77
disableProgress: () => null,

test/lib/install.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const t = require('tap')
22

33
const Install = require('../../lib/install.js')
4-
const mockNpm = require('../fixtures/mock-npm')
4+
const { fake: mockNpm } = require('../fixtures/mock-npm')
55

66
t.test('should install using Arborist', (t) => {
77
const SCRIPTS = []

test/lib/link.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
const t = require('tap')
12
const { resolve } = require('path')
23
const fs = require('fs')
34

45
const Arborist = require('@npmcli/arborist')
5-
const t = require('tap')
6-
const mockNpm = require('../fixtures/mock-npm')
6+
const { fake: mockNpm } = require('../fixtures/mock-npm')
77

88
const redactCwd = (path) => {
99
const normalizePath = p => p

0 commit comments

Comments
 (0)