Skip to content

Commit

Permalink
fix(tests): use config defaults
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wraithgar committed May 7, 2021
1 parent 6fb386d commit ee1e5b9
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 22 deletions.
1 change: 1 addition & 0 deletions lib/dist-tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class DistTag extends BaseCommand {
spec = npa(spec || '')
const version = spec.rawSpec
const defaultTag = tag || this.npm.config.get('tag')
console.log(`defaultTag`, defaultTag)

log.verbose('dist-tag add', defaultTag, 'to', spec.name + '@' + version)

Expand Down
2 changes: 1 addition & 1 deletion lib/outdated.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class Outdated extends BaseCommand {
: edge.dev ? 'devDependencies'
: 'dependencies'

for (const omitType of this.npm.config.get('omit') || []) {
for (const omitType of this.npm.config.get('omit')) {
if (node[omitType])
return
}
Expand Down
4 changes: 2 additions & 2 deletions tap-snapshots/test/lib/view.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ exports[`test/lib/view.js TAP workspaces all workspaces > must match snapshot 1`
green@1.0.0 | ACME | deps: 2 | versions: 2
green is a very important color
[1m[31mDEPRECATED[39m[22m!! - true
[1m[31mDEPRECATED[39m[22m ⚠️ - true
keywords:colors, green, crayola
Expand Down Expand Up @@ -425,7 +425,7 @@ exports[`test/lib/view.js TAP workspaces one specific workspace > must match sna
green@1.0.0 | ACME | deps: 2 | versions: 2
green is a very important color
[1m[31mDEPRECATED[39m[22m!! - true
[1m[31mDEPRECATED[39m[22m ⚠️ - true
keywords:colors, green, crayola
Expand Down
8 changes: 5 additions & 3 deletions test/fixtures/mock-npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// npm.config You still need a separate flatOptions but this is the first step
// to eventually just using npm itself

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

const mockLog = {
clearProgress: () => {},
disableProgress: () => {},
Expand All @@ -25,10 +27,10 @@ const mockNpm = (base = {}) => {
config: {
// for now just set `find` to what config.find should return
// this works cause `find` is not an existing config entry
find: (k) => config[k],
get: (k) => config[k],
find: (k) => ({...realConfig.defaults, ...config})[k],
get: (k) => ({...realConfig.defaults, ...config})[k],
set: (k, v) => config[k] = v,
list: [config]
list: [{ ...realConfig.defaults, ...config}]
},
}
}
Expand Down
9 changes: 6 additions & 3 deletions test/lib/dist-tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,9 @@ const DistTag = t.mock('../../lib/dist-tag.js', {
},
})

const config = {}
const npm = mockNpm({
config: {
global: false,
},
config,
output: msg => {
result = result ? [result, msg].join('\n') : msg
},
Expand Down Expand Up @@ -349,6 +348,10 @@ t.test('add using valid semver range as name', (t) => {

t.test('add missing args', (t) => {
npm.prefix = t.testdir({})
config.tag = ''
t.teardown(() => {
delete config.tag
})
distTag.exec(['add', '@scoped/[email protected]'], (err) => {
t.matchSnapshot(err, 'should exit usage error message')
t.end()
Expand Down
15 changes: 2 additions & 13 deletions test/lib/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ t.test('should log package info', t => {
},
})
const npm = mockNpm({
config: { global: false },
config: { unicode: false },
})
const view = new View(npm)

Expand All @@ -269,7 +269,6 @@ t.test('should log package info', t => {
})
const unicodeNpm = mockNpm({
config: {
global: false,
unicode: true,
},
})
Expand Down Expand Up @@ -358,7 +357,6 @@ t.test('should log info of package in current working dir', t => {
prefix: testDir,
config: {
tag: '1.0.0',
global: false,
},
})
const view = new View(npm)
Expand Down Expand Up @@ -389,7 +387,6 @@ t.test('should log info by field name', t => {
const jsonNpm = mockNpm({
config: {
json: true,
global: false,
},
})

Expand All @@ -400,9 +397,7 @@ t.test('should log info by field name', t => {
packument,
},
})
const npm = mockNpm({
config: { global: false },
})
const npm = mockNpm()
const view = new View(npm)

t.test('readme', t => {
Expand Down Expand Up @@ -489,7 +484,6 @@ t.test('throw ENOENT error if package.json misisng', (t) => {
const View = t.mock('../../lib/view.js')
const npm = mockNpm({
prefix: testDir,
config: { global: false },
})
const view = new View(npm)
view.exec([], (err) => {
Expand All @@ -506,7 +500,6 @@ t.test('throw EJSONPARSE error if package.json not json', (t) => {
const View = t.mock('../../lib/view.js')
const npm = mockNpm({
prefix: testDir,
config: { global: false },
})
const view = new View(npm)
view.exec([], (err) => {
Expand All @@ -523,7 +516,6 @@ t.test('throw error if package.json has no name', (t) => {
const View = t.mock('../../lib/view.js')
const npm = mockNpm({
prefix: testDir,
config: { global: false },
})
const view = new View(npm)
view.exec([], (err) => {
Expand All @@ -541,7 +533,6 @@ t.test('throws when unpublished', (t) => {
const npm = mockNpm({
config: {
tag: '1.0.1',
global: false,
},
})
const view = new View(npm)
Expand Down Expand Up @@ -684,7 +675,6 @@ t.test('completion', async t => {
const npm = mockNpm({
config: {
tag: '1.0.1',
global: false,
},
})
const view = new View(npm)
Expand All @@ -700,7 +690,6 @@ t.test('no registry completion', async t => {
const npm = mockNpm({
config: {
tag: '1.0.1',
global: false,
},
})
const view = new View(npm)
Expand Down

0 comments on commit ee1e5b9

Please sign in to comment.