From 5089cdd00a5264d8108a8911a4513c336abf2deb Mon Sep 17 00:00:00 2001 From: Gar Date: Wed, 23 Jun 2021 06:53:56 -0700 Subject: [PATCH] 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. --- test/fixtures/mock-npm.js | 59 +++++++++++++++++++++++--- test/lib/audit.js | 2 +- test/lib/bin.js | 2 +- test/lib/birthday.js | 2 +- test/lib/cache.js | 2 +- test/lib/ci.js | 2 +- test/lib/cli.js | 32 +++++---------- test/lib/dedupe.js | 64 +++++++++++------------------ test/lib/diff.js | 4 +- test/lib/dist-tag.js | 2 +- test/lib/docs.js | 2 +- test/lib/exec.js | 2 +- test/lib/fund.js | 2 +- test/lib/help-search.js | 2 +- test/lib/init.js | 4 +- test/lib/install.js | 2 +- test/lib/link.js | 4 +- test/lib/load-all-commands.js | 18 ++++---- test/lib/logout.js | 2 +- test/lib/ls.js | 2 +- test/lib/npm.js | 57 +++++++------------------ test/lib/outdated.js | 2 +- test/lib/owner.js | 2 +- test/lib/pack.js | 2 +- test/lib/ping.js | 2 +- test/lib/profile.js | 2 +- test/lib/publish.js | 2 +- test/lib/rebuild.js | 4 +- test/lib/repo.js | 2 +- test/lib/run-script.js | 4 +- test/lib/search.js | 4 +- test/lib/set-script.js | 2 +- test/lib/shrinkwrap.js | 2 +- test/lib/star.js | 2 +- test/lib/uninstall.js | 4 +- test/lib/unpublish.js | 2 +- test/lib/update.js | 4 +- test/lib/utils/read-package-name.js | 10 ++--- test/lib/version.js | 2 +- test/lib/view.js | 2 +- test/lib/whoami.js | 2 +- 41 files changed, 160 insertions(+), 166 deletions(-) diff --git a/test/fixtures/mock-npm.js b/test/fixtures/mock-npm.js index c972c35b31861..1de080eb10b4a 100644 --- a/test/fixtures/mock-npm.js +++ b/test/fixtures/mock-npm.js @@ -1,9 +1,52 @@ -// Basic npm fixture that you can give a config object that acts like -// npm.config You still need a separate flatOptions but this is the first step -// to eventually just using npm itself +const npmlog = require('npmlog') +const perf = require('../../lib/utils/perf.js') +perf.reset() +const procLog = require('../../lib/utils/proc-log-listener.js') +procLog.reset() + +const realLog = {} +for (const level of ['silly', 'verbose', 'timing', 'notice', 'warn', 'error']) + realLog[level] = npmlog[level] + +const { title, execPath } = process + +const RealMockNpm = (t, otherMocks = {}) => { + t.teardown(() => { + for (const level of ['silly', 'verbose', 'timing', 'notice', 'warn', 'error']) + npmlog[level] = realLog[level] + perf.reset() + procLog.reset() + process.title = title + process.execPath = execPath + delete process.env.npm_command + delete process.env.COLOR + }) + const logs = [] + const outputs = [] + const npm = t.mock('../../lib/npm.js', otherMocks) + const command = async (command, args = []) => { + return new Promise((resolve, reject) => { + npm.commands[command](args, err => { + if (err) + return reject(err) + return resolve() + }) + }) + } + for (const level of ['silly', 'verbose', 'timing', 'notice', 'warn', 'error']) { + npmlog[level] = (...msg) => { + logs.push([level, ...msg]) + } + } + npm.output = (...msg) => outputs.push(msg) + return { npm, logs, outputs, command } +} const realConfig = require('../../lib/utils/config') +// Basic npm fixture that you can give a config object that acts like +// npm.config You still need a separate flatOptions. Tests should migrate to +// using the real npm mock above class MockNpm { constructor (base = {}) { this._mockOutputs = [] @@ -51,7 +94,11 @@ class MockNpm { } } -// TODO export MockNpm, and change tests to use new MockNpm() -module.exports = (base = {}) => { - return new MockNpm(base) +const FakeMockNpm = (base = {}) => { + return new MockNpm(base) +} + +module.exports = { + fake: FakeMockNpm, + real: RealMockNpm } diff --git a/test/lib/audit.js b/test/lib/audit.js index bb6f06debc51f..561765a0270b5 100644 --- a/test/lib/audit.js +++ b/test/lib/audit.js @@ -1,5 +1,5 @@ const t = require('tap') -const mockNpm = require('../fixtures/mock-npm') +const { fake: mockNpm } = require('../fixtures/mock-npm') t.test('should audit using Arborist', t => { let ARB_ARGS = null diff --git a/test/lib/bin.js b/test/lib/bin.js index 898e7ba439b88..8ceca8280f52d 100644 --- a/test/lib/bin.js +++ b/test/lib/bin.js @@ -1,5 +1,5 @@ const t = require('tap') -const mockNpm = require('../fixtures/mock-npm') +const { fake: mockNpm } = require('../fixtures/mock-npm') t.test('bin', (t) => { t.plan(4) diff --git a/test/lib/birthday.js b/test/lib/birthday.js index 0589be7a8eedb..05660d6fa3f20 100644 --- a/test/lib/birthday.js +++ b/test/lib/birthday.js @@ -1,5 +1,5 @@ const t = require('tap') -const mockNpm = require('../fixtures/mock-npm') +const { fake: mockNpm } = require('../fixtures/mock-npm') const config = { yes: false, diff --git a/test/lib/cache.js b/test/lib/cache.js index bad0ede89e101..d3d6f5b8845de 100644 --- a/test/lib/cache.js +++ b/test/lib/cache.js @@ -1,5 +1,5 @@ const t = require('tap') -const mockNpm = require('../fixtures/mock-npm') +const { fake: mockNpm } = require('../fixtures/mock-npm.js') const path = require('path') const usageUtil = () => 'usage instructions' diff --git a/test/lib/ci.js b/test/lib/ci.js index b60375c289842..b6b2af9c111db 100644 --- a/test/lib/ci.js +++ b/test/lib/ci.js @@ -4,7 +4,7 @@ const readdir = util.promisify(fs.readdir) const t = require('tap') -const mockNpm = require('../fixtures/mock-npm') +const { fake: mockNpm } = require('../fixtures/mock-npm') t.test('should ignore scripts with --ignore-scripts', (t) => { const SCRIPTS = [] diff --git a/test/lib/cli.js b/test/lib/cli.js index 89c3736e886d2..b85c981cd008b 100644 --- a/test/lib/cli.js +++ b/test/lib/cli.js @@ -1,8 +1,6 @@ const t = require('tap') -// NOTE lib/npm.js is wrapped in t.mock() every time because it's currently a -// singleton. In the next semver major we will export the class and lib/cli.js -// can call `new` on it and then we won't have to do that anymore +const { real: mockNpm } = require('../fixtures/mock-npm.js') const unsupportedMock = { checkForBrokenNode: () => {}, @@ -36,13 +34,6 @@ const cliMock = (npm) => t.mock('../../lib/cli.js', { npmlog: npmlogMock, }) -const npmOutputs = [] -const npmMock = () => { - const npm = t.mock('../../lib/npm.js') - npm.output = (...msg) => npmOutputs.push(msg) - return npm -} - const processMock = (proc) => { const mocked = { ...process, @@ -59,7 +50,6 @@ const { argv } = process t.afterEach(() => { logs.length = 0 process.argv = argv - npmOutputs.length = 0 exitHandlerCalled = null exitHandlerNpm = null }) @@ -70,7 +60,7 @@ t.test('print the version, and treat npm_g as npm -g', async t => { version: process.version, }) - const npm = npmMock() + const { npm, outputs } = mockNpm(t) const cli = cliMock(npm) await cli(proc) @@ -82,7 +72,7 @@ t.test('print the version, and treat npm_g as npm -g', async t => { ['info', 'using', 'npm@%s', npm.version], ['info', 'using', 'node@%s', process.version], ]) - t.strictSame(npmOutputs, [[npm.version]]) + t.strictSame(outputs, [[npm.version]]) t.strictSame(exitHandlerCalled, []) }) @@ -90,7 +80,7 @@ t.test('calling with --versions calls npm version with no args', async t => { const proc = processMock({ argv: ['node', 'npm', 'install', 'or', 'whatever', '--versions'], }) - const npm = npmMock() + const { npm, outputs } = mockNpm(t) const cli = cliMock(npm) let versionArgs @@ -110,7 +100,7 @@ t.test('calling with --versions calls npm version with no args', async t => { ['info', 'using', 'node@%s', process.version], ]) - t.strictSame(npmOutputs, []) + t.strictSame(outputs, []) t.strictSame(exitHandlerCalled, []) }) @@ -119,10 +109,10 @@ t.test('print usage if no params provided', async t => { argv: ['node', 'npm'], }) - const npm = npmMock() + const { npm, outputs } = mockNpm(t) const cli = cliMock(npm) await cli(proc) - t.match(npmOutputs[0][0], 'Usage:', 'outputs npm usage') + t.match(outputs[0][0], 'Usage:', 'outputs npm usage') t.match(exitHandlerCalled, [], 'should call exitHandler with no args') t.ok(exitHandlerNpm, 'exitHandler npm is set') t.match(proc.exitCode, 1) @@ -133,11 +123,11 @@ t.test('print usage if non-command param provided', async t => { argv: ['node', 'npm', 'tset'], }) - const npm = npmMock() + const { npm, outputs } = mockNpm(t) const cli = cliMock(npm) await cli(proc) - t.match(npmOutputs[0][0], 'Unknown command: "tset"') - t.match(npmOutputs[0][0], 'Did you mean this?') + t.match(outputs[0][0], 'Unknown command: "tset"') + t.match(outputs[0][0], 'Did you mean this?') t.match(exitHandlerCalled, [], 'should call exitHandler with no args') t.ok(exitHandlerNpm, 'exitHandler npm is set') t.match(proc.exitCode, 1) @@ -148,7 +138,7 @@ t.test('load error calls error handler', async t => { argv: ['node', 'npm', 'asdf'], }) - const npm = npmMock() + const { npm } = mockNpm(t) const cli = cliMock(npm) const er = new Error('test load error') npm.load = () => Promise.reject(er) diff --git a/test/lib/dedupe.js b/test/lib/dedupe.js index 801e3c96de3cf..30f8a380e8ea3 100644 --- a/test/lib/dedupe.js +++ b/test/lib/dedupe.js @@ -1,21 +1,20 @@ const t = require('tap') -const mockNpm = require('../fixtures/mock-npm') +const { real: mockNpm } = require('../fixtures/mock-npm') -t.test('should throw in global mode', (t) => { - const Dedupe = t.mock('../../lib/dedupe.js') - const npm = mockNpm({ - config: { 'dry-run': false, global: true }, - }) - const dedupe = new Dedupe(npm) - - dedupe.exec([], er => { - t.match(er, { code: 'EDEDUPEGLOBAL' }, 'throws EDEDUPEGLOBAL') - t.end() - }) +t.test('should throw in global mode', async (t) => { + const { npm, command } = mockNpm(t) + await npm.load() + npm.config.set('global', true) + t.rejects( + command('dedupe'), + { code: 'EDEDUPEGLOBAL' }, + 'throws EDEDUPEGLOBALE' + ) }) -t.test('should remove dupes using Arborist', (t) => { - const Dedupe = t.mock('../../lib/dedupe.js', { +t.test('should remove dupes using Arborist', async (t) => { + t.plan(5) + const { npm, command } = mockNpm(t, { '@npmcli/arborist': function (args) { t.ok(args, 'gets options object') t.ok(args.path, 'gets path option') @@ -28,37 +27,24 @@ t.test('should remove dupes using Arborist', (t) => { t.ok(arb, 'gets arborist tree') }, }) - const npm = mockNpm({ - prefix: 'foo', - config: { - 'dry-run': 'true', - }, - }) - const dedupe = new Dedupe(npm) - dedupe.exec([], er => { - if (er) - throw er - t.ok(true, 'callback is called') - t.end() - }) + await npm.load() + npm.config.set('prefix', 'foo') + npm.config.set('dry-run', 'true') + await command('dedupe') }) -t.test('should remove dupes using Arborist - no arguments', (t) => { - const Dedupe = t.mock('../../lib/dedupe.js', { +t.test('should remove dupes using Arborist - no arguments', async (t) => { + t.plan(1) + const { npm, command } = mockNpm(t, { '@npmcli/arborist': function (args) { t.ok(args.dryRun, 'gets dryRun from config') this.dedupe = () => {} }, '../../lib/utils/reify-output.js': () => {}, + '../../lib/utils/reify-finish.js': () => {}, }) - const npm = mockNpm({ - prefix: 'foo', - config: { - 'dry-run': 'true', - }, - }) - const dedupe = new Dedupe(npm) - dedupe.exec(null, () => { - t.end() - }) + await npm.load() + npm.config.set('prefix', 'foo') + npm.config.set('dry-run', true) + await command('dedupe') }) diff --git a/test/lib/diff.js b/test/lib/diff.js index 2fb38c9b127e4..fcba802d93b87 100644 --- a/test/lib/diff.js +++ b/test/lib/diff.js @@ -1,6 +1,6 @@ -const { resolve, join } = require('path') const t = require('tap') -const mockNpm = require('../fixtures/mock-npm') +const { resolve, join } = require('path') +const { fake: mockNpm } = require('../fixtures/mock-npm') const noop = () => null let libnpmdiff = noop diff --git a/test/lib/dist-tag.js b/test/lib/dist-tag.js index 9af90c309c77c..1fb5cb3b6ee62 100644 --- a/test/lib/dist-tag.js +++ b/test/lib/dist-tag.js @@ -1,5 +1,5 @@ -const mockNpm = require('../fixtures/mock-npm') const t = require('tap') +const { fake: mockNpm } = require('../fixtures/mock-npm') let result = '' let log = '' diff --git a/test/lib/docs.js b/test/lib/docs.js index 0da070d9a7d13..fbd7584201247 100644 --- a/test/lib/docs.js +++ b/test/lib/docs.js @@ -1,5 +1,5 @@ const t = require('tap') -const mockNpm = require('../fixtures/mock-npm.js') +const { fake: mockNpm } = require('../fixtures/mock-npm.js') const { join, sep } = require('path') const pkgDirs = t.testdir({ diff --git a/test/lib/exec.js b/test/lib/exec.js index dff067619f2ce..6d99c7959d88e 100644 --- a/test/lib/exec.js +++ b/test/lib/exec.js @@ -1,5 +1,5 @@ const t = require('tap') -const mockNpm = require('../fixtures/mock-npm') +const { fake: mockNpm } = require('../fixtures/mock-npm') const { resolve, delimiter } = require('path') const ARB_CTOR = [] diff --git a/test/lib/fund.js b/test/lib/fund.js index 65778fca50bd7..784989827edc1 100644 --- a/test/lib/fund.js +++ b/test/lib/fund.js @@ -1,5 +1,5 @@ const t = require('tap') -const mockNpm = require('../fixtures/mock-npm') +const { fake: mockNpm } = require('../fixtures/mock-npm') const version = '1.0.0' const funding = { diff --git a/test/lib/help-search.js b/test/lib/help-search.js index 845b3873e3cf9..2df862d4fc570 100644 --- a/test/lib/help-search.js +++ b/test/lib/help-search.js @@ -1,6 +1,6 @@ const t = require('tap') const { join } = require('path') -const mockNpm = require('../fixtures/mock-npm') +const { fake: mockNpm } = require('../fixtures/mock-npm') const ansicolors = require('ansicolors') const OUTPUT = [] diff --git a/test/lib/init.js b/test/lib/init.js index 44a2af5bcc02b..9b32eabe2c82c 100644 --- a/test/lib/init.js +++ b/test/lib/init.js @@ -1,7 +1,7 @@ +const t = require('tap') const fs = require('fs') const { resolve } = require('path') -const t = require('tap') -const mockNpm = require('../fixtures/mock-npm') +const { fake: mockNpm } = require('../fixtures/mock-npm') const npmLog = { disableProgress: () => null, diff --git a/test/lib/install.js b/test/lib/install.js index b7929bddafdba..6412b34c16f25 100644 --- a/test/lib/install.js +++ b/test/lib/install.js @@ -1,7 +1,7 @@ const t = require('tap') const Install = require('../../lib/install.js') -const mockNpm = require('../fixtures/mock-npm') +const { fake: mockNpm } = require('../fixtures/mock-npm') t.test('should install using Arborist', (t) => { const SCRIPTS = [] diff --git a/test/lib/link.js b/test/lib/link.js index 64375cfc13c2c..736d18cab9906 100644 --- a/test/lib/link.js +++ b/test/lib/link.js @@ -1,9 +1,9 @@ +const t = require('tap') const { resolve } = require('path') const fs = require('fs') const Arborist = require('@npmcli/arborist') -const t = require('tap') -const mockNpm = require('../fixtures/mock-npm') +const { fake: mockNpm } = require('../fixtures/mock-npm') const redactCwd = (path) => { const normalizePath = p => p diff --git a/test/lib/load-all-commands.js b/test/lib/load-all-commands.js index b3b2012d23aa2..e5f10099cf365 100644 --- a/test/lib/load-all-commands.js +++ b/test/lib/load-all-commands.js @@ -3,17 +3,16 @@ // name, a description, and if it has completion it is a function. That it // renders also ensures that any params we've defined in our commands work. const t = require('tap') -const npm = t.mock('../../lib/npm.js') +const { real: mockNpm } = require('../fixtures/mock-npm.js') const { cmdList } = require('../../lib/utils/cmd-list.js') -let npmOutput = [] -npm.output = (msg) => { - npmOutput = msg -} +const { npm, outputs } = mockNpm(t) + t.test('load each command', t => { - t.plan(cmdList.length + 1) + t.plan(cmdList.length) npm.load((er) => { - t.notOk(er) + if (er) + throw er npm.config.set('usage', true) for (const cmd of cmdList.sort((a, b) => a.localeCompare(b, 'en'))) { t.test(cmd, t => { @@ -26,13 +25,14 @@ t.test('load each command', t => { t.match(impl.usage, cmd, 'usage contains the command') impl([], (err) => { t.notOk(err) - t.match(npmOutput, impl.usage, 'usage is what is output') + t.match(outputs[0][0], impl.usage, 'usage is what is output') // This ties usage to a snapshot so we have to re-run snap if usage // changes, which rebuilds the man pages - t.matchSnapshot(npmOutput) + t.matchSnapshot(outputs[0][0]) t.end() }) }) + outputs.length = 0 } }) }) diff --git a/test/lib/logout.js b/test/lib/logout.js index b130d439c88ca..7cb5c2790d621 100644 --- a/test/lib/logout.js +++ b/test/lib/logout.js @@ -1,5 +1,5 @@ -const mockNpm = require('../fixtures/mock-npm') const t = require('tap') +const { fake: mockNpm } = require('../fixtures/mock-npm') const config = { registry: 'https://registry.npmjs.org/', diff --git a/test/lib/ls.js b/test/lib/ls.js index 64ece6bd8a3fd..4d6fef52b629e 100644 --- a/test/lib/ls.js +++ b/test/lib/ls.js @@ -3,7 +3,7 @@ // of them contain the tap testdir folders, which are auto-generated and // may change when node-tap is updated. const t = require('tap') -const mockNpm = require('../fixtures/mock-npm') +const { fake: mockNpm } = require('../fixtures/mock-npm.js') const { resolve } = require('path') const { utimesSync } = require('fs') diff --git a/test/lib/npm.js b/test/lib/npm.js index 1ccfbbd7ea507..291a58955ceed 100644 --- a/test/lib/npm.js +++ b/test/lib/npm.js @@ -1,8 +1,7 @@ const t = require('tap') const npmlog = require('npmlog') -const perf = require('../../lib/utils/perf.js') -const procLog = require('../../lib/utils/proc-log-listener.js') +const { real: mockNpm } = require('../fixtures/mock-npm.js') // delete this so that we don't have configs from the fact that it // is being run by 'npm test' @@ -42,10 +41,6 @@ const bePosix = () => { } const argv = [...process.argv] -const realLog = {} -for (const level of ['silly', 'verbose', 'timing', 'notice', 'warn', 'error']) - realLog[level] = npmlog[level] - t.afterEach(() => { for (const env of Object.keys(process.env).filter(e => /^npm_/.test(e))) delete process.env[env] @@ -55,30 +50,13 @@ t.afterEach(() => { value: actualPlatform, configurable: true, }) - for (const level of ['silly', 'verbose', 'timing', 'notice', 'warn', 'error']) - npmlog[level] = realLog[level] - perf.reset() - procLog.reset() }) -const npmMock = () => { - const logs = [] - const outputs = [] - const npm = t.mock('../../lib/npm.js') - for (const level of ['silly', 'verbose', 'timing', 'notice', 'warn', 'error']) { - npmlog[level] = (...msg) => { - logs.push([level, ...msg]) - } - } - npm.output = (...msg) => outputs.push(msg) - return { npm, logs, outputs } -} - const CACHE = t.testdir() process.env.npm_config_cache = CACHE t.test('not yet loaded', t => { - const { npm, logs } = npmMock() + const { npm, logs } = mockNpm(t) t.match(npm, { started: Number, command: null, @@ -104,7 +82,7 @@ t.test('not yet loaded', t => { t.test('npm.load', t => { t.test('callback must be a function', t => { - const { npm, logs } = npmMock() + const { npm, logs } = mockNpm(t) const er = new TypeError('callback must be a function if provided') t.throws(() => npm.load({}), er) t.same(logs, []) @@ -112,7 +90,7 @@ t.test('npm.load', t => { }) t.test('callback style', t => { - const { npm } = npmMock() + const { npm } = mockNpm(t) npm.load((err) => { if (err) throw err @@ -122,7 +100,7 @@ t.test('npm.load', t => { }) t.test('load error', async t => { - const { npm } = npmMock() + const { npm } = mockNpm(t) const loadError = new Error('load error') npm.config.load = async () => { throw loadError @@ -141,7 +119,7 @@ t.test('npm.load', t => { }) t.test('basic loading', async t => { - const { npm, logs } = npmMock() + const { npm, logs } = mockNpm(t) const dir = t.testdir({ node_modules: {}, }) @@ -209,7 +187,7 @@ t.test('npm.load', t => { t.test('forceful loading', async t => { process.argv = [...process.argv, '--force', '--color', 'always'] - const { npm, logs } = npmMock() + const { npm, logs } = mockNpm(t) await npm.load() t.match(logs.filter(l => l[0] !== 'timing'), [ [ @@ -229,7 +207,6 @@ t.test('npm.load', t => { const PATH = process.env.PATH || process.env.Path process.env.PATH = resolve(dir, 'bin') - const { execPath } = process process.argv = [ node, process.argv[1], @@ -244,10 +221,9 @@ t.test('npm.load', t => { t.teardown(() => { process.env.PATH = PATH - process.execPath = execPath }) - const { npm, logs, outputs } = npmMock() + const { npm, logs, outputs } = mockNpm(t) await npm.load() t.equal(npm.config.get('scope'), '@foo', 'added the @ sign to scope') t.match(logs.filter(l => l[0] !== 'timing' || !/^config:/.test(l[1])), [ @@ -342,10 +318,8 @@ t.test('npm.load', t => { '.npmrc': '', }) - const { execPath } = process - process.argv = [ - execPath, + process.execPath, process.argv[1], '--userconfig', resolve(dir, '.npmrc'), @@ -355,7 +329,7 @@ t.test('npm.load', t => { 'true', ] - const { npm, outputs } = npmMock() + const { npm, outputs } = mockNpm(t) await npm.load() npm.localPrefix = dir @@ -411,9 +385,8 @@ t.test('npm.load', t => { workspaces: ['./packages/*'], }), }) - const { execPath } = process process.argv = [ - execPath, + process.execPath, process.argv[1], '--userconfig', resolve(dir, '.npmrc'), @@ -423,7 +396,7 @@ t.test('npm.load', t => { '--global', 'true', ] - const { npm } = npmMock() + const { npm } = mockNpm(t) await npm.load() npm.localPrefix = dir await new Promise((res, rej) => { @@ -464,7 +437,7 @@ t.test('set process.title', t => { '--scope=foo', 'ls', ] - const { npm } = npmMock() + const { npm } = mockNpm(t) await npm.load() t.equal(npm.title, 'npm ls') t.equal(process.title, 'npm ls') @@ -480,7 +453,7 @@ t.test('set process.title', t => { 'revoke', 'deadbeefcafebad', ] - const { npm } = npmMock() + const { npm } = mockNpm(t) await npm.load() t.equal(npm.title, 'npm token revoke ***') t.equal(process.title, 'npm token revoke ***') @@ -495,7 +468,7 @@ t.test('set process.title', t => { 'token', 'revoke', ] - const { npm } = npmMock() + const { npm } = mockNpm(t) await npm.load() t.equal(npm.title, 'npm token revoke') t.equal(process.title, 'npm token revoke') diff --git a/test/lib/outdated.js b/test/lib/outdated.js index 462ec0fc62b6c..34a0aa6c9e03e 100644 --- a/test/lib/outdated.js +++ b/test/lib/outdated.js @@ -1,5 +1,5 @@ const t = require('tap') -const mockNpm = require('../fixtures/mock-npm') +const { fake: mockNpm } = require('../fixtures/mock-npm') const packument = spec => { const mocks = { diff --git a/test/lib/owner.js b/test/lib/owner.js index 10ceb03030a5a..32944a84edbc4 100644 --- a/test/lib/owner.js +++ b/test/lib/owner.js @@ -1,5 +1,5 @@ const t = require('tap') -const mockNpm = require('../fixtures/mock-npm.js') +const { fake: mockNpm } = require('../fixtures/mock-npm.js') let result = '' let readPackageNamePrefix = null diff --git a/test/lib/pack.js b/test/lib/pack.js index 523ba5d6b535d..3d61abdaf74ca 100644 --- a/test/lib/pack.js +++ b/test/lib/pack.js @@ -1,5 +1,5 @@ const t = require('tap') -const mockNpm = require('../fixtures/mock-npm') +const { fake: mockNpm } = require('../fixtures/mock-npm') const pacote = require('pacote') const path = require('path') diff --git a/test/lib/ping.js b/test/lib/ping.js index c2d53efef5a7e..f0a10718c46d0 100644 --- a/test/lib/ping.js +++ b/test/lib/ping.js @@ -1,5 +1,5 @@ const t = require('tap') -const mockNpm = require('../fixtures/mock-npm') +const { fake: mockNpm } = require('../fixtures/mock-npm') t.test('pings', (t) => { t.plan(8) diff --git a/test/lib/profile.js b/test/lib/profile.js index 17bcf99b9b653..112aa5c3b75e1 100644 --- a/test/lib/profile.js +++ b/test/lib/profile.js @@ -1,5 +1,5 @@ const t = require('tap') -const mockNpm = require('../fixtures/mock-npm') +const { fake: mockNpm } = require('../fixtures/mock-npm') let result = '' const config = { diff --git a/test/lib/publish.js b/test/lib/publish.js index e34f00b477ee1..56590478fc1ae 100644 --- a/test/lib/publish.js +++ b/test/lib/publish.js @@ -1,5 +1,5 @@ const t = require('tap') -const mockNpm = require('../fixtures/mock-npm') +const { fake: mockNpm } = require('../fixtures/mock-npm') const fs = require('fs') // The way we set loglevel is kind of convoluted, and there is no way to affect diff --git a/test/lib/rebuild.js b/test/lib/rebuild.js index e686b6a32ce53..81768a21fb3b7 100644 --- a/test/lib/rebuild.js +++ b/test/lib/rebuild.js @@ -1,7 +1,7 @@ +const t = require('tap') const fs = require('fs') const { resolve } = require('path') -const mockNpm = require('../fixtures/mock-npm') -const t = require('tap') +const { fake: mockNpm } = require('../fixtures/mock-npm') let result = '' diff --git a/test/lib/repo.js b/test/lib/repo.js index 0701750b58c63..e1ac90b1e7577 100644 --- a/test/lib/repo.js +++ b/test/lib/repo.js @@ -1,5 +1,5 @@ const t = require('tap') -const mockNpm = require('../fixtures/mock-npm.js') +const { fake: mockNpm } = require('../fixtures/mock-npm.js') const { join, sep } = require('path') const pkgDirs = t.testdir({ diff --git a/test/lib/run-script.js b/test/lib/run-script.js index 897c80817e254..a3f04ea6790fa 100644 --- a/test/lib/run-script.js +++ b/test/lib/run-script.js @@ -1,6 +1,6 @@ -const { resolve } = require('path') const t = require('tap') -const mockNpm = require('../fixtures/mock-npm') +const { resolve } = require('path') +const { fake: mockNpm } = require('../fixtures/mock-npm') const normalizePath = p => p .replace(/\\+/g, '/') diff --git a/test/lib/search.js b/test/lib/search.js index ac894a9e3369d..510a470f48088 100644 --- a/test/lib/search.js +++ b/test/lib/search.js @@ -1,6 +1,6 @@ -const Minipass = require('minipass') const t = require('tap') -const mockNpm = require('../fixtures/mock-npm') +const Minipass = require('minipass') +const { fake: mockNpm } = require('../fixtures/mock-npm') const libnpmsearchResultFixture = require('../fixtures/libnpmsearch-stream-result.js') diff --git a/test/lib/set-script.js b/test/lib/set-script.js index 96b455785c509..8038e8db3eb7d 100644 --- a/test/lib/set-script.js +++ b/test/lib/set-script.js @@ -1,7 +1,7 @@ const t = require('tap') const fs = require('fs') const parseJSON = require('json-parse-even-better-errors') -const mockNpm = require('../fixtures/mock-npm.js') +const { fake: mockNpm } = require('../fixtures/mock-npm') const { resolve } = require('path') const flatOptions = {} diff --git a/test/lib/shrinkwrap.js b/test/lib/shrinkwrap.js index 8bd18f7fbd7c6..ab3b8d0ffe447 100644 --- a/test/lib/shrinkwrap.js +++ b/test/lib/shrinkwrap.js @@ -1,6 +1,6 @@ const t = require('tap') const fs = require('fs') -const mockNpm = require('../fixtures/mock-npm') +const { fake: mockNpm } = require('../fixtures/mock-npm') const config = { global: false, diff --git a/test/lib/star.js b/test/lib/star.js index 0c584e4a3f0cf..8820d6e9cfb0b 100644 --- a/test/lib/star.js +++ b/test/lib/star.js @@ -1,5 +1,5 @@ -const mockNpm = require('../fixtures/mock-npm') const t = require('tap') +const { fake: mockNpm } = require('../fixtures/mock-npm') let result = '' diff --git a/test/lib/uninstall.js b/test/lib/uninstall.js index 8cf35bd428d3b..272adb8683602 100644 --- a/test/lib/uninstall.js +++ b/test/lib/uninstall.js @@ -1,7 +1,7 @@ +const t = require('tap') const fs = require('fs') const { resolve } = require('path') -const t = require('tap') -const mockNpm = require('../fixtures/mock-npm') +const { fake: mockNpm } = require('../fixtures/mock-npm') const npm = mockNpm({ globalDir: '', diff --git a/test/lib/unpublish.js b/test/lib/unpublish.js index 6ad45e958c22f..9199b8aed9442 100644 --- a/test/lib/unpublish.js +++ b/test/lib/unpublish.js @@ -1,5 +1,5 @@ const t = require('tap') -const mockNpm = require('../fixtures/mock-npm') +const { fake: mockNpm } = require('../fixtures/mock-npm') let result = '' const noop = () => null diff --git a/test/lib/update.js b/test/lib/update.js index 411d07592a155..487b12e5fa297 100644 --- a/test/lib/update.js +++ b/test/lib/update.js @@ -1,6 +1,6 @@ -const { resolve } = require('path') const t = require('tap') -const mockNpm = require('../fixtures/mock-npm') +const { resolve } = require('path') +const { fake: mockNpm } = require('../fixtures/mock-npm') const config = { depth: 0, diff --git a/test/lib/utils/read-package-name.js b/test/lib/utils/read-package-name.js index c8f88bacd4b84..a1a1b4a1504dc 100644 --- a/test/lib/utils/read-package-name.js +++ b/test/lib/utils/read-package-name.js @@ -1,17 +1,15 @@ const t = require('tap') -const mockNpm = require('../../fixtures/mock-npm') -const npm = mockNpm() const readPackageName = require('../../../lib/utils/read-package-name.js') t.test('read local package.json', async (t) => { - npm.prefix = t.testdir({ + const prefix = t.testdir({ 'package.json': JSON.stringify({ name: 'my-local-package', version: '1.0.0', }), }) - const packageName = await readPackageName(npm.prefix) + const packageName = await readPackageName(prefix) t.equal( packageName, 'my-local-package', @@ -20,13 +18,13 @@ t.test('read local package.json', async (t) => { }) t.test('read local scoped-package.json', async (t) => { - npm.prefix = t.testdir({ + const prefix = t.testdir({ 'package.json': JSON.stringify({ name: '@my-scope/my-local-package', version: '1.0.0', }), }) - const packageName = await readPackageName(npm.prefix) + const packageName = await readPackageName(prefix) t.equal( packageName, '@my-scope/my-local-package', diff --git a/test/lib/version.js b/test/lib/version.js index 6a83375b0025c..df6d0dd797d0a 100644 --- a/test/lib/version.js +++ b/test/lib/version.js @@ -1,5 +1,5 @@ const t = require('tap') -const mockNpm = require('../fixtures/mock-npm') +const { fake: mockNpm } = require('../fixtures/mock-npm') let result = [] diff --git a/test/lib/view.js b/test/lib/view.js index 4544d7d5d1a94..5f2e5a8add5e9 100644 --- a/test/lib/view.js +++ b/test/lib/view.js @@ -3,7 +3,7 @@ const t = require('tap') // run the same as tap does when running directly with node process.stdout.columns = undefined -const mockNpm = require('../fixtures/mock-npm') +const { fake: mockNpm } = require('../fixtures/mock-npm') let logs const cleanLogs = () => { diff --git a/test/lib/whoami.js b/test/lib/whoami.js index 5e350a32ebfb4..9190e3858b137 100644 --- a/test/lib/whoami.js +++ b/test/lib/whoami.js @@ -1,5 +1,5 @@ const t = require('tap') -const mockNpm = require('../fixtures/mock-npm') +const { fake: mockNpm } = require('../fixtures/mock-npm') t.test('whoami', (t) => { t.plan(3)