Skip to content

Commit f3a662f

Browse files
committed
fix(tests): use config defaults
The mocked npm now uses all config defaults, ensuring we don't have bugs from the disconnect between our currently heavily mocked tests and reality. Eventually the npm mock will be closer to reality, this moves the needle. PR-URL: #3211 Credit: @wraithgar Close: #3211 Reviewed-by: @ruyadorno
1 parent 285976f commit f3a662f

File tree

4 files changed

+16
-23
lines changed

4 files changed

+16
-23
lines changed

lib/outdated.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ class Outdated extends BaseCommand {
149149
: edge.dev ? 'devDependencies'
150150
: 'dependencies'
151151

152-
for (const omitType of this.npm.config.get('omit') || []) {
152+
for (const omitType of this.npm.config.get('omit')) {
153153
if (node[omitType])
154154
return
155155
}

test/fixtures/mock-npm.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// npm.config You still need a separate flatOptions but this is the first step
33
// to eventually just using npm itself
44

5+
const realConfig = require('../../lib/utils/config')
6+
57
const mockLog = {
68
clearProgress: () => {},
79
disableProgress: () => {},
@@ -25,10 +27,10 @@ const mockNpm = (base = {}) => {
2527
config: {
2628
// for now just set `find` to what config.find should return
2729
// this works cause `find` is not an existing config entry
28-
find: (k) => config[k],
29-
get: (k) => config[k],
30+
find: (k) => ({...realConfig.defaults, ...config})[k],
31+
get: (k) => ({...realConfig.defaults, ...config})[k],
3032
set: (k, v) => config[k] = v,
31-
list: [config]
33+
list: [{ ...realConfig.defaults, ...config}]
3234
},
3335
}
3436
}

test/lib/dist-tag.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,9 @@ const DistTag = t.mock('../../lib/dist-tag.js', {
6868
},
6969
})
7070

71+
const config = {}
7172
const npm = mockNpm({
72-
config: {
73-
global: false,
74-
},
73+
config,
7574
output: msg => {
7675
result = result ? [result, msg].join('\n') : msg
7776
},
@@ -349,6 +348,10 @@ t.test('add using valid semver range as name', (t) => {
349348

350349
t.test('add missing args', (t) => {
351350
npm.prefix = t.testdir({})
351+
config.tag = ''
352+
t.teardown(() => {
353+
delete config.tag
354+
})
352355
distTag.exec(['add', '@scoped/[email protected]'], (err) => {
353356
t.matchSnapshot(err, 'should exit usage error message')
354357
t.end()

test/lib/view.js

+4-16
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ t.test('should log package info', t => {
248248
},
249249
})
250250
const npm = mockNpm({
251-
config: { global: false },
251+
config: { unicode: false },
252252
})
253253
const view = new View(npm)
254254

@@ -268,10 +268,7 @@ t.test('should log package info', t => {
268268
},
269269
})
270270
const unicodeNpm = mockNpm({
271-
config: {
272-
global: false,
273-
unicode: true,
274-
},
271+
config: { unicode: true },
275272
})
276273
const viewUnicode = new ViewUnicode(unicodeNpm)
277274

@@ -358,7 +355,6 @@ t.test('should log info of package in current working dir', t => {
358355
prefix: testDir,
359356
config: {
360357
tag: '1.0.0',
361-
global: false,
362358
},
363359
})
364360
const view = new View(npm)
@@ -389,7 +385,6 @@ t.test('should log info by field name', t => {
389385
const jsonNpm = mockNpm({
390386
config: {
391387
json: true,
392-
global: false,
393388
},
394389
})
395390

@@ -400,9 +395,7 @@ t.test('should log info by field name', t => {
400395
packument,
401396
},
402397
})
403-
const npm = mockNpm({
404-
config: { global: false },
405-
})
398+
const npm = mockNpm()
406399
const view = new View(npm)
407400

408401
t.test('readme', t => {
@@ -489,7 +482,6 @@ t.test('throw ENOENT error if package.json misisng', (t) => {
489482
const View = t.mock('../../lib/view.js')
490483
const npm = mockNpm({
491484
prefix: testDir,
492-
config: { global: false },
493485
})
494486
const view = new View(npm)
495487
view.exec([], (err) => {
@@ -506,7 +498,6 @@ t.test('throw EJSONPARSE error if package.json not json', (t) => {
506498
const View = t.mock('../../lib/view.js')
507499
const npm = mockNpm({
508500
prefix: testDir,
509-
config: { global: false },
510501
})
511502
const view = new View(npm)
512503
view.exec([], (err) => {
@@ -523,7 +514,6 @@ t.test('throw error if package.json has no name', (t) => {
523514
const View = t.mock('../../lib/view.js')
524515
const npm = mockNpm({
525516
prefix: testDir,
526-
config: { global: false },
527517
})
528518
const view = new View(npm)
529519
view.exec([], (err) => {
@@ -541,7 +531,6 @@ t.test('throws when unpublished', (t) => {
541531
const npm = mockNpm({
542532
config: {
543533
tag: '1.0.1',
544-
global: false,
545534
},
546535
})
547536
const view = new View(npm)
@@ -581,6 +570,7 @@ t.test('workspaces', t => {
581570
},
582571
})
583572
const config = {
573+
unicode: false,
584574
tag: 'latest',
585575
}
586576
let warnMsg
@@ -684,7 +674,6 @@ t.test('completion', async t => {
684674
const npm = mockNpm({
685675
config: {
686676
tag: '1.0.1',
687-
global: false,
688677
},
689678
})
690679
const view = new View(npm)
@@ -700,7 +689,6 @@ t.test('no registry completion', async t => {
700689
const npm = mockNpm({
701690
config: {
702691
tag: '1.0.1',
703-
global: false,
704692
},
705693
})
706694
const view = new View(npm)

0 commit comments

Comments
 (0)