Skip to content

Commit 173bc89

Browse files
authored
chore: convert more util tests to use mock-npm (#6475)
1 parent e71010a commit 173bc89

File tree

7 files changed

+279
-482
lines changed

7 files changed

+279
-482
lines changed

tap-snapshots/test/lib/utils/open-url-prompt.js.test.cjs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,10 @@
66
*/
77
'use strict'
88
exports[`test/lib/utils/open-url-prompt.js TAP opens a url > must match snapshot 1`] = `
9-
Array [
10-
Array [
11-
String(
12-
npm home:
13-
https://www.npmjs.com
14-
),
15-
],
16-
]
9+
npm home:
10+
https://www.npmjs.com
1711
`
1812

1913
exports[`test/lib/utils/open-url-prompt.js TAP prints json output > must match snapshot 1`] = `
20-
Array [
21-
Array [
22-
"{\\"title\\":\\"npm home\\",\\"url\\":\\"https://www.npmjs.com\\"}",
23-
],
24-
]
14+
{"title":"npm home","url":"https://www.npmjs.com"}
2515
`

tap-snapshots/test/lib/utils/reify-finish.js.test.cjs

Lines changed: 0 additions & 15 deletions
This file was deleted.

test/lib/utils/completion/installed-deep.js

Lines changed: 18 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,6 @@
1-
const { resolve } = require('path')
21
const t = require('tap')
32
const installedDeep = require('../../../../lib/utils/completion/installed-deep.js')
4-
5-
let prefix
6-
let globalDir = 'MISSING_GLOBAL_DIR'
7-
const _flatOptions = {
8-
depth: Infinity,
9-
global: false,
10-
workspacesEnabled: true,
11-
Arborist: require('@npmcli/arborist'),
12-
get prefix () {
13-
return prefix
14-
},
15-
}
16-
const npm = {
17-
flatOptions: _flatOptions,
18-
get prefix () {
19-
return _flatOptions.prefix
20-
},
21-
get globalDir () {
22-
return globalDir
23-
},
24-
config: {
25-
get (key) {
26-
return _flatOptions[key]
27-
},
28-
},
29-
}
3+
const mockNpm = require('../../../fixtures/mock-npm')
304

315
const fixture = {
326
'package.json': JSON.stringify({
@@ -153,16 +127,23 @@ const globalFixture = {
153127
},
154128
}
155129

156-
t.test('get list of package names', async t => {
157-
const fix = t.testdir({
158-
local: fixture,
159-
global: globalFixture,
130+
const mockDeep = async (t, config) => {
131+
const mock = await mockNpm(t, {
132+
prefixDir: fixture,
133+
globalPrefixDir: globalFixture,
134+
config: {
135+
depth: Infinity,
136+
...config,
137+
},
160138
})
161139

162-
prefix = resolve(fix, 'local')
163-
globalDir = resolve(fix, 'global/node_modules')
140+
const res = await installedDeep(mock.npm)
164141

165-
const res = await installedDeep(npm, null)
142+
return res
143+
}
144+
145+
t.test('get list of package names', async t => {
146+
const res = await mockDeep(t)
166147
t.same(
167148
res,
168149
[
@@ -179,17 +160,7 @@ t.test('get list of package names', async t => {
179160
})
180161

181162
t.test('get list of package names as global', async t => {
182-
const fix = t.testdir({
183-
local: fixture,
184-
global: globalFixture,
185-
})
186-
187-
prefix = resolve(fix, 'local')
188-
globalDir = resolve(fix, 'global/node_modules')
189-
190-
_flatOptions.global = true
191-
192-
const res = await installedDeep(npm, null)
163+
const res = await mockDeep(t, { global: true })
193164
t.same(
194165
res,
195166
[
@@ -199,22 +170,10 @@ t.test('get list of package names as global', async t => {
199170
],
200171
'should return list of global packages with no extra flags'
201172
)
202-
_flatOptions.global = false
203-
t.end()
204173
})
205174

206175
t.test('limit depth', async t => {
207-
const fix = t.testdir({
208-
local: fixture,
209-
global: globalFixture,
210-
})
211-
212-
prefix = resolve(fix, 'local')
213-
globalDir = resolve(fix, 'global/node_modules')
214-
215-
_flatOptions.depth = 0
216-
217-
const res = await installedDeep(npm, null)
176+
const res = await mockDeep(t, { depth: 0 })
218177
t.same(
219178
res,
220179
[
@@ -229,23 +188,10 @@ t.test('limit depth', async t => {
229188
],
230189
'should print only packages up to the specified depth'
231190
)
232-
_flatOptions.depth = 0
233-
t.end()
234191
})
235192

236193
t.test('limit depth as global', async t => {
237-
const fix = t.testdir({
238-
local: fixture,
239-
global: globalFixture,
240-
})
241-
242-
prefix = resolve(fix, 'local')
243-
globalDir = resolve(fix, 'global/node_modules')
244-
245-
_flatOptions.global = true
246-
_flatOptions.depth = 0
247-
248-
const res = await installedDeep(npm, null)
194+
const res = await mockDeep(t, { depth: 0, global: true })
249195
t.same(
250196
res,
251197
[
@@ -256,7 +202,4 @@ t.test('limit depth as global', async t => {
256202
],
257203
'should reorder so that packages above that level depth goes last'
258204
)
259-
_flatOptions.global = false
260-
_flatOptions.depth = 0
261-
t.end()
262205
})
Lines changed: 13 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
const t = require('tap')
2-
const { resolve } = require('path')
32
const installed = require('../../../../lib/utils/completion/installed-shallow.js')
3+
const mockNpm = require('../../../fixtures/mock-npm')
44

5-
const flatOptions = { global: false }
6-
const npm = { flatOptions }
7-
8-
t.test('global not set, include globals with -g', async t => {
9-
const dir = t.testdir({
10-
global: {
5+
const mockShallow = async (t, config) => {
6+
const res = await mockNpm(t, {
7+
globalPrefixDir: {
118
node_modules: {
129
x: {},
1310
'@scope': {
1411
y: {},
1512
},
1613
},
1714
},
18-
local: {
15+
prefixDir: {
1916
node_modules: {
2017
a: {},
2118
'@scope': {
2219
b: {},
2320
},
2421
},
2522
},
23+
config: { global: false, ...config },
2624
})
27-
npm.globalDir = resolve(dir, 'global/node_modules')
28-
npm.localDir = resolve(dir, 'local/node_modules')
29-
flatOptions.global = false
25+
return res
26+
}
27+
28+
t.test('global not set, include globals with -g', async t => {
29+
const { npm } = await mockShallow(t)
3030
const opt = { conf: { argv: { remain: [] } } }
3131
const res = await installed(npm, opt)
3232
t.strictSame(res.sort(), [
@@ -35,64 +35,21 @@ t.test('global not set, include globals with -g', async t => {
3535
'a',
3636
'@scope/b',
3737
].sort())
38-
t.end()
3938
})
4039

4140
t.test('global set, include globals and not locals', async t => {
42-
const dir = t.testdir({
43-
global: {
44-
node_modules: {
45-
x: {},
46-
'@scope': {
47-
y: {},
48-
},
49-
},
50-
},
51-
local: {
52-
node_modules: {
53-
a: {},
54-
'@scope': {
55-
b: {},
56-
},
57-
},
58-
},
59-
})
60-
npm.globalDir = resolve(dir, 'global/node_modules')
61-
npm.localDir = resolve(dir, 'local/node_modules')
62-
flatOptions.global = true
41+
const { npm } = await mockShallow(t, { global: true })
6342
const opt = { conf: { argv: { remain: [] } } }
6443
const res = await installed(npm, opt)
6544
t.strictSame(res.sort(), [
6645
'@scope/y',
6746
'x',
6847
].sort())
69-
t.end()
7048
})
7149

7250
t.test('more than 3 items in argv, skip it', async t => {
73-
const dir = t.testdir({
74-
global: {
75-
node_modules: {
76-
x: {},
77-
'@scope': {
78-
y: {},
79-
},
80-
},
81-
},
82-
local: {
83-
node_modules: {
84-
a: {},
85-
'@scope': {
86-
b: {},
87-
},
88-
},
89-
},
90-
})
91-
npm.globalDir = resolve(dir, 'global/node_modules')
92-
npm.localDir = resolve(dir, 'local/node_modules')
93-
flatOptions.global = false
51+
const { npm } = await mockShallow(t)
9452
const opt = { conf: { argv: { remain: [1, 2, 3, 4, 5, 6] } } }
9553
const res = await installed(npm, opt)
9654
t.strictSame(res, null)
97-
t.end()
9855
})

0 commit comments

Comments
 (0)