11const  t  =  require ( 'tap' ) 
22
3- // NOTE lib/npm.js is wrapped in t.mock() every time because it's currently a 
4- // singleton.  In the next semver major we will export the class and lib/cli.js 
5- // can call `new` on it and then we won't have to do that anymore 
3+ const  {  real : mockNpm  }  =  require ( '../fixtures/mock-npm.js' ) 
64
75const  unsupportedMock  =  { 
86  checkForBrokenNode : ( )  =>  { } , 
@@ -36,13 +34,6 @@ const cliMock = (npm) => t.mock('../../lib/cli.js', {
3634  npmlog : npmlogMock , 
3735} ) 
3836
39- const  npmOutputs  =  [ ] 
40- const  npmMock  =  ( )  =>  { 
41-   const  npm  =  t . mock ( '../../lib/npm.js' ) 
42-   npm . output  =  ( ...msg )  =>  npmOutputs . push ( msg ) 
43-   return  npm 
44- } 
45- 
4637const  processMock  =  ( proc )  =>  { 
4738  const  mocked  =  { 
4839    ...process , 
@@ -59,7 +50,6 @@ const { argv } = process
5950t . afterEach ( ( )  =>  { 
6051  logs . length  =  0 
6152  process . argv  =  argv 
62-   npmOutputs . length  =  0 
6353  exitHandlerCalled  =  null 
6454  exitHandlerNpm  =  null 
6555} ) 
@@ -70,7 +60,7 @@ t.test('print the version, and treat npm_g as npm -g', async t => {
7060    version : process . version , 
7161  } ) 
7262
73-   const  npm   =   npmMock ( ) 
63+   const  {   npm,  outputs  }   =   mockNpm ( t ) 
7464  const  cli  =  cliMock ( npm ) 
7565  await  cli ( proc ) 
7666
@@ -82,15 +72,15 @@ t.test('print the version, and treat npm_g as npm -g', async t => {
8272    [ 'info' ,  'using' ,  'npm@%s' ,  npm . version ] , 
8373    [ 'info' ,  'using' ,  'node@%s' ,  process . version ] , 
8474  ] ) 
85-   t . strictSame ( npmOutputs ,  [ [ npm . version ] ] ) 
75+   t . strictSame ( outputs ,  [ [ npm . version ] ] ) 
8676  t . strictSame ( exitHandlerCalled ,  [ ] ) 
8777} ) 
8878
8979t . test ( 'calling with --versions calls npm version with no args' ,  async  t  =>  { 
9080  const  proc  =  processMock ( { 
9181    argv : [ 'node' ,  'npm' ,  'install' ,  'or' ,  'whatever' ,  '--versions' ] , 
9282  } ) 
93-   const  npm   =   npmMock ( ) 
83+   const  {   npm,  outputs  }   =   mockNpm ( t ) 
9484  const  cli  =  cliMock ( npm ) 
9585
9686  let  versionArgs 
@@ -110,7 +100,7 @@ t.test('calling with --versions calls npm version with no args', async t => {
110100    [ 'info' ,  'using' ,  'node@%s' ,  process . version ] , 
111101  ] ) 
112102
113-   t . strictSame ( npmOutputs ,  [ ] ) 
103+   t . strictSame ( outputs ,  [ ] ) 
114104  t . strictSame ( exitHandlerCalled ,  [ ] ) 
115105} ) 
116106
@@ -119,10 +109,10 @@ t.test('print usage if no params provided', async t => {
119109    argv : [ 'node' ,  'npm' ] , 
120110  } ) 
121111
122-   const  npm   =   npmMock ( ) 
112+   const  {   npm,  outputs  }   =   mockNpm ( t ) 
123113  const  cli  =  cliMock ( npm ) 
124114  await  cli ( proc ) 
125-   t . match ( npmOutputs [ 0 ] [ 0 ] ,  'Usage:' ,  'outputs npm usage' ) 
115+   t . match ( outputs [ 0 ] [ 0 ] ,  'Usage:' ,  'outputs npm usage' ) 
126116  t . match ( exitHandlerCalled ,  [ ] ,  'should call exitHandler with no args' ) 
127117  t . ok ( exitHandlerNpm ,  'exitHandler npm is set' ) 
128118  t . match ( proc . exitCode ,  1 ) 
@@ -133,11 +123,11 @@ t.test('print usage if non-command param provided', async t => {
133123    argv : [ 'node' ,  'npm' ,  'tset' ] , 
134124  } ) 
135125
136-   const  npm   =   npmMock ( ) 
126+   const  {   npm,  outputs  }   =   mockNpm ( t ) 
137127  const  cli  =  cliMock ( npm ) 
138128  await  cli ( proc ) 
139-   t . match ( npmOutputs [ 0 ] [ 0 ] ,  'Unknown command: "tset"' ) 
140-   t . match ( npmOutputs [ 0 ] [ 0 ] ,  'Did you mean this?' ) 
129+   t . match ( outputs [ 0 ] [ 0 ] ,  'Unknown command: "tset"' ) 
130+   t . match ( outputs [ 0 ] [ 0 ] ,  'Did you mean this?' ) 
141131  t . match ( exitHandlerCalled ,  [ ] ,  'should call exitHandler with no args' ) 
142132  t . ok ( exitHandlerNpm ,  'exitHandler npm is set' ) 
143133  t . match ( proc . exitCode ,  1 ) 
@@ -148,7 +138,7 @@ t.test('load error calls error handler', async t => {
148138    argv : [ 'node' ,  'npm' ,  'asdf' ] , 
149139  } ) 
150140
151-   const  npm  =   npmMock ( ) 
141+   const  {   npm }   =   mockNpm ( t ) 
152142  const  cli  =  cliMock ( npm ) 
153143  const  er  =  new  Error ( 'test load error' ) 
154144  npm . load  =  ( )  =>  Promise . reject ( er ) 
0 commit comments