diff options
author | Gar <gar+gh@danger.computer> | 2021-02-25 02:54:50 +0300 |
---|---|---|
committer | Ruy Adorno <ruyadorno@hotmail.com> | 2021-03-05 00:05:08 +0300 |
commit | 4a5dd3a5a200b3f4f7b47168497d8e03dca3a2ca (patch) | |
tree | d34a1ea229b719c3cfbdce85899ceaf67b43e7ab /test/lib/cache.js | |
parent | b33c760cea7fe2696d35b5530abc1b455980fef1 (diff) |
fix(npm) pass npm context everywhere
Instead of files randomly requiring the npm singleton,
we pass it where it needs to go so that tests don't need
to do so much require mocking everywhere
PR-URL: https://github.com/npm/cli/pull/2772
Credit: @wraithgar
Close: #2772
Reviewed-by: @ruyadorno
Diffstat (limited to 'test/lib/cache.js')
-rw-r--r-- | test/lib/cache.js | 25 |
1 files changed, 12 insertions, 13 deletions
diff --git a/test/lib/cache.js b/test/lib/cache.js index 05d269dd4..67499f37e 100644 --- a/test/lib/cache.js +++ b/test/lib/cache.js @@ -58,27 +58,26 @@ const cacache = { }, } -const mocks = { +const Cache = requireInject('../../lib/cache.js', { cacache, npmlog, pacote, rimraf, - '../../lib/npm.js': npm, '../../lib/utils/output.js': output, '../../lib/utils/usage.js': usageUtil, -} +}) -const cache = requireInject('../../lib/cache.js', mocks) +const cache = new Cache(npm) t.test('cache no args', t => { - cache([], err => { + cache.exec([], err => { t.equal(err.message, 'usage instructions', 'should throw usage instructions') t.end() }) }) t.test('cache clean', t => { - cache(['clean'], err => { + cache.exec(['clean'], err => { t.match(err.message, 'the npm cache self-heals', 'should throw warning') t.end() }) @@ -91,7 +90,7 @@ t.test('cache clean (force)', t => { flatOptions.force = false }) - cache(['clear'], err => { + cache.exec(['clear'], err => { t.ifError(err) t.equal(rimrafPath, path.join(npm.cache, '_cacache')) t.end() @@ -99,7 +98,7 @@ t.test('cache clean (force)', t => { }) t.test('cache clean with arg', t => { - cache(['rm', 'pkg'], err => { + cache.exec(['rm', 'pkg'], err => { t.match(err.message, 'does not accept arguments', 'should throw error') t.end() }) @@ -110,7 +109,7 @@ t.test('cache add no arg', t => { logOutput = [] }) - cache(['add'], err => { + cache.exec(['add'], err => { t.strictSame(logOutput, [ ['silly', 'cache add', 'args', []], ], 'logs correctly') @@ -126,7 +125,7 @@ t.test('cache add pkg only', t => { tarballStreamOpts = {} }) - cache(['add', 'mypkg'], err => { + cache.exec(['add', 'mypkg'], err => { t.ifError(err) t.strictSame(logOutput, [ ['silly', 'cache add', 'args', ['mypkg']], @@ -145,7 +144,7 @@ t.test('cache add pkg w/ spec modifier', t => { tarballStreamOpts = {} }) - cache(['add', 'mypkg', 'latest'], err => { + cache.exec(['add', 'mypkg', 'latest'], err => { t.ifError(err) t.strictSame(logOutput, [ ['silly', 'cache add', 'args', ['mypkg', 'latest']], @@ -162,7 +161,7 @@ t.test('cache verify', t => { outputOutput = [] }) - cache(['verify'], err => { + cache.exec(['verify'], err => { t.ifError(err) t.match(outputOutput, [ `Cache verified and compressed (${path.join(npm.cache, '_cacache')})`, @@ -189,7 +188,7 @@ t.test('cache verify w/ extra output', t => { delete cacacheVerifyStats.missingContent }) - cache(['check'], err => { + cache.exec(['check'], err => { t.ifError(err) t.match(outputOutput, [ `Cache verified and compressed (~${path.join('/fake/path', '_cacache')})`, |