Skip to content

Commit

Permalink
test: update to t.mock tap api
Browse files Browse the repository at this point in the history
- Replaces all usage of `requireInject` with `t.mock`
- Standardizes usage of tap as: `const t = require('tap')`
- Fixes some rare cases in which mocks were relying in the fact that
`requireInject` placed mocks within the `require.cache` and made mocks
available for nested files, this type of scenario now requires an extra
`t.mock` declaration in order to define mocks to that nested file.
  • Loading branch information
ruyadorno committed Dec 17, 2020
1 parent e8c3796 commit b5391c1
Show file tree
Hide file tree
Showing 87 changed files with 608 additions and 684 deletions.
4 changes: 2 additions & 2 deletions test/bin/npm-cli.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const t = require('tap')
const requireInject = require('require-inject')

t.test('loading the bin calls the implementation', t => {
requireInject('../../bin/npm-cli.js', {
t.mock('../../bin/npm-cli.js', {
'../../lib/cli.js': proc => {
t.equal(proc, process, 'called implementation with process object')
t.end()
Expand Down
13 changes: 6 additions & 7 deletions test/bin/npx-cli.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const t = require('tap')
const requireInject = require('require-inject')
const npx = require.resolve('../../bin/npx-cli.js')
const cli = require.resolve('../../lib/cli.js')
const npm = require.resolve('../../bin/npm-cli.js')
Expand All @@ -14,35 +13,35 @@ t.afterEach(cb => {

t.test('npx foo -> npm exec -- foo', t => {
process.argv = ['node', npx, 'foo']
requireInject(npx, { [cli]: () => {} })
t.mock(npx, { [cli]: () => {} })
t.strictSame(process.argv, ['node', npm, 'exec', '--', 'foo'])
t.end()
})

t.test('npx -- foo -> npm exec -- foo', t => {
process.argv = ['node', npx, '--', 'foo']
requireInject(npx, { [cli]: () => {} })
t.mock(npx, { [cli]: () => {} })
t.strictSame(process.argv, ['node', npm, 'exec', '--', 'foo'])
t.end()
})

t.test('npx -x y foo -z -> npm exec -x y -- foo -z', t => {
process.argv = ['node', npx, '-x', 'y', 'foo', '-z']
requireInject(npx, { [cli]: () => {} })
t.mock(npx, { [cli]: () => {} })
t.strictSame(process.argv, ['node', npm, 'exec', '-x', 'y', '--', 'foo', '-z'])
t.end()
})

t.test('npx --x=y --no-install foo -z -> npm exec --x=y -- foo -z', t => {
process.argv = ['node', npx, '--x=y', '--no-install', 'foo', '-z']
requireInject(npx, { [cli]: () => {} })
t.mock(npx, { [cli]: () => {} })
t.strictSame(process.argv, ['node', npm, 'exec', '--x=y', '--yes=false', '--', 'foo', '-z'])
t.end()
})

t.test('transform renamed options into proper values', t => {
process.argv = ['node', npx, '-y', '--shell=bash', '-p', 'foo', '-c', 'asdf']
requireInject(npx, { [cli]: () => {} })
t.mock(npx, { [cli]: () => {} })
t.strictSame(process.argv, ['node', npm, 'exec', '--yes', '--script-shell=bash', '--package', 'foo', '--call', 'asdf'])
t.end()
})
Expand Down Expand Up @@ -80,7 +79,7 @@ t.test('use a bunch of deprecated switches and options', t => {
'--',
'foobar',
]
requireInject(npx, { [cli]: () => {} })
t.mock(npx, { [cli]: () => {} })
t.strictSame(process.argv, expect)
t.strictSame(logs, [
['npx: the --npm argument has been removed.'],
Expand Down
91 changes: 45 additions & 46 deletions test/lib/access.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
const { test } = require('tap')
const requireInject = require('require-inject')
const t = require('tap')

const emptyMock = requireInject('../../lib/access.js', {
const emptyMock = t.mock('../../lib/access.js', {
'../../lib/npm.js': {
flatOptions: {},
},
})

test('completion', t => {
t.test('completion', t => {
const { completion } = emptyMock

const testComp = (argv, expect) => {
Expand Down Expand Up @@ -49,15 +48,15 @@ test('completion', t => {
t.end()
})

test('subcommand required', t => {
t.test('subcommand required', t => {
const access = emptyMock
access([], (err) => {
t.equal(err, '\nUsage: Subcommand is required.\n\n' + access.usage)
t.end()
})
})

test('unrecognized subcommand', (t) => {
t.test('unrecognized subcommand', (t) => {
const access = emptyMock

access(['blerg'], (err) => {
Expand All @@ -70,7 +69,7 @@ test('unrecognized subcommand', (t) => {
})
})

test('edit', (t) => {
t.test('edit', (t) => {
const access = emptyMock

access([
Expand All @@ -86,13 +85,13 @@ test('edit', (t) => {
})
})

test('access public on unscoped package', (t) => {
t.test('access public on unscoped package', (t) => {
const prefix = t.testdir({
'package.json': JSON.stringify({
name: 'npm-access-public-pkg',
}),
})
const access = requireInject('../../lib/access.js', {
const access = t.mock('../../lib/access.js', {
'../../lib/npm.js': { prefix },
})
access([
Expand All @@ -107,13 +106,13 @@ test('access public on unscoped package', (t) => {
})
})

test('access public on scoped package', (t) => {
t.test('access public on scoped package', (t) => {
t.plan(4)
const name = '@scoped/npm-access-public-pkg'
const prefix = t.testdir({
'package.json': JSON.stringify({ name }),
})
const access = requireInject('../../lib/access.js', {
const access = t.mock('../../lib/access.js', {
libnpmaccess: {
public: (pkg, { registry }) => {
t.equal(pkg, name, 'should use pkg name ref')
Expand All @@ -140,11 +139,11 @@ test('access public on scoped package', (t) => {
})
})

test('access public on missing package.json', (t) => {
t.test('access public on missing package.json', (t) => {
const prefix = t.testdir({
node_modules: {},
})
const access = requireInject('../../lib/access.js', {
const access = t.mock('../../lib/access.js', {
'../../lib/npm.js': { prefix },
})
access([
Expand All @@ -159,12 +158,12 @@ test('access public on missing package.json', (t) => {
})
})

test('access public on invalid package.json', (t) => {
t.test('access public on invalid package.json', (t) => {
const prefix = t.testdir({
'package.json': '{\n',
node_modules: {},
})
const access = requireInject('../../lib/access.js', {
const access = t.mock('../../lib/access.js', {
'../../lib/npm.js': { prefix },
})
access([
Expand All @@ -179,13 +178,13 @@ test('access public on invalid package.json', (t) => {
})
})

test('access restricted on unscoped package', (t) => {
t.test('access restricted on unscoped package', (t) => {
const prefix = t.testdir({
'package.json': JSON.stringify({
name: 'npm-access-restricted-pkg',
}),
})
const access = requireInject('../../lib/access.js', {
const access = t.mock('../../lib/access.js', {
'../../lib/npm.js': { prefix },
})
access([
Expand All @@ -200,13 +199,13 @@ test('access restricted on unscoped package', (t) => {
})
})

test('access restricted on scoped package', (t) => {
t.test('access restricted on scoped package', (t) => {
t.plan(4)
const name = '@scoped/npm-access-restricted-pkg'
const prefix = t.testdir({
'package.json': JSON.stringify({ name }),
})
const access = requireInject('../../lib/access.js', {
const access = t.mock('../../lib/access.js', {
libnpmaccess: {
restricted: (pkg, { registry }) => {
t.equal(pkg, name, 'should use pkg name ref')
Expand All @@ -233,11 +232,11 @@ test('access restricted on scoped package', (t) => {
})
})

test('access restricted on missing package.json', (t) => {
t.test('access restricted on missing package.json', (t) => {
const prefix = t.testdir({
node_modules: {},
})
const access = requireInject('../../lib/access.js', {
const access = t.mock('../../lib/access.js', {
'../../lib/npm.js': { prefix },
})
access([
Expand All @@ -252,12 +251,12 @@ test('access restricted on missing package.json', (t) => {
})
})

test('access restricted on invalid package.json', (t) => {
t.test('access restricted on invalid package.json', (t) => {
const prefix = t.testdir({
'package.json': '{\n',
node_modules: {},
})
const access = requireInject('../../lib/access.js', {
const access = t.mock('../../lib/access.js', {
'../../lib/npm.js': { prefix },
})
access([
Expand All @@ -272,9 +271,9 @@ test('access restricted on invalid package.json', (t) => {
})
})

test('access grant read-only', (t) => {
t.test('access grant read-only', (t) => {
t.plan(5)
const access = requireInject('../../lib/access.js', {
const access = t.mock('../../lib/access.js', {
libnpmaccess: {
grant: (spec, team, permissions) => {
t.equal(spec, '@scoped/another', 'should use expected spec')
Expand All @@ -296,9 +295,9 @@ test('access grant read-only', (t) => {
})
})

test('access grant read-write', (t) => {
t.test('access grant read-write', (t) => {
t.plan(5)
const access = requireInject('../../lib/access.js', {
const access = t.mock('../../lib/access.js', {
libnpmaccess: {
grant: (spec, team, permissions) => {
t.equal(spec, '@scoped/another', 'should use expected spec')
Expand All @@ -320,14 +319,14 @@ test('access grant read-write', (t) => {
})
})

test('access grant current cwd', (t) => {
t.test('access grant current cwd', (t) => {
t.plan(5)
const prefix = t.testdir({
'package.json': JSON.stringify({
name: 'yargs',
}),
})
const access = requireInject('../../lib/access.js', {
const access = t.mock('../../lib/access.js', {
libnpmaccess: {
grant: (spec, team, permissions) => {
t.equal(spec, 'yargs', 'should use expected spec')
Expand All @@ -348,7 +347,7 @@ test('access grant current cwd', (t) => {
})
})

test('access grant others', (t) => {
t.test('access grant others', (t) => {
const access = emptyMock

access([
Expand All @@ -366,7 +365,7 @@ test('access grant others', (t) => {
})
})

test('access grant missing team args', (t) => {
t.test('access grant missing team args', (t) => {
const access = emptyMock

access([
Expand All @@ -384,7 +383,7 @@ test('access grant missing team args', (t) => {
})
})

test('access grant malformed team arg', (t) => {
t.test('access grant malformed team arg', (t) => {
const access = emptyMock

access([
Expand All @@ -402,9 +401,9 @@ test('access grant malformed team arg', (t) => {
})
})

test('access 2fa-required/2fa-not-required', t => {
t.test('access 2fa-required/2fa-not-required', t => {
t.plan(2)
const access = requireInject('../../lib/access.js', {
const access = t.mock('../../lib/access.js', {
libnpmaccess: {
tfaRequired: (spec) => {
t.equal(spec, '@scope/pkg', 'should use expected spec')
Expand All @@ -429,9 +428,9 @@ test('access 2fa-required/2fa-not-required', t => {
})
})

test('access revoke', (t) => {
t.test('access revoke', (t) => {
t.plan(4)
const access = requireInject('../../lib/access.js', {
const access = t.mock('../../lib/access.js', {
libnpmaccess: {
revoke: (spec, team) => {
t.equal(spec, '@scoped/another', 'should use expected spec')
Expand All @@ -451,7 +450,7 @@ test('access revoke', (t) => {
})
})

test('access revoke missing team args', (t) => {
t.test('access revoke missing team args', (t) => {
const access = emptyMock

access([
Expand All @@ -468,7 +467,7 @@ test('access revoke missing team args', (t) => {
})
})

test('access revoke malformed team arg', (t) => {
t.test('access revoke malformed team arg', (t) => {
const access = emptyMock

access([
Expand All @@ -485,9 +484,9 @@ test('access revoke malformed team arg', (t) => {
})
})

test('npm access ls-packages with no team', (t) => {
t.test('npm access ls-packages with no team', (t) => {
t.plan(3)
const access = requireInject('../../lib/access.js', {
const access = t.mock('../../lib/access.js', {
libnpmaccess: {
lsPackages: (entity) => {
t.equal(entity, 'foo', 'should use expected entity')
Expand All @@ -506,9 +505,9 @@ test('npm access ls-packages with no team', (t) => {
})
})

test('access ls-packages on team', (t) => {
t.test('access ls-packages on team', (t) => {
t.plan(3)
const access = requireInject('../../lib/access.js', {
const access = t.mock('../../lib/access.js', {
libnpmaccess: {
lsPackages: (entity) => {
t.equal(entity, 'myorg:myteam', 'should use expected entity')
Expand All @@ -527,14 +526,14 @@ test('access ls-packages on team', (t) => {
})
})

test('access ls-collaborators on current', (t) => {
t.test('access ls-collaborators on current', (t) => {
t.plan(3)
const prefix = t.testdir({
'package.json': JSON.stringify({
name: 'yargs',
}),
})
const access = requireInject('../../lib/access.js', {
const access = t.mock('../../lib/access.js', {
libnpmaccess: {
lsCollaborators: (spec) => {
t.equal(spec, 'yargs', 'should use expected spec')
Expand All @@ -552,9 +551,9 @@ test('access ls-collaborators on current', (t) => {
})
})

test('access ls-collaborators on spec', (t) => {
t.test('access ls-collaborators on spec', (t) => {
t.plan(3)
const access = requireInject('../../lib/access.js', {
const access = t.mock('../../lib/access.js', {
libnpmaccess: {
lsCollaborators: (spec) => {
t.equal(spec, 'yargs', 'should use expected spec')
Expand Down

0 comments on commit b5391c1

Please sign in to comment.