1
1
const t = require ( 'tap' )
2
2
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' )
6
4
7
5
const unsupportedMock = {
8
6
checkForBrokenNode : ( ) => { } ,
@@ -36,13 +34,6 @@ const cliMock = (npm) => t.mock('../../lib/cli.js', {
36
34
npmlog : npmlogMock ,
37
35
} )
38
36
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
-
46
37
const processMock = ( proc ) => {
47
38
const mocked = {
48
39
...process ,
@@ -59,7 +50,6 @@ const { argv } = process
59
50
t . afterEach ( ( ) => {
60
51
logs . length = 0
61
52
process . argv = argv
62
- npmOutputs . length = 0
63
53
exitHandlerCalled = null
64
54
exitHandlerNpm = null
65
55
} )
@@ -70,7 +60,7 @@ t.test('print the version, and treat npm_g as npm -g', async t => {
70
60
version : process . version ,
71
61
} )
72
62
73
- const npm = npmMock ( )
63
+ const { npm, outputs } = mockNpm ( t )
74
64
const cli = cliMock ( npm )
75
65
await cli ( proc )
76
66
@@ -82,15 +72,15 @@ t.test('print the version, and treat npm_g as npm -g', async t => {
82
72
[ 'info' , 'using' , 'npm@%s' , npm . version ] ,
83
73
[ 'info' , 'using' , 'node@%s' , process . version ] ,
84
74
] )
85
- t . strictSame ( npmOutputs , [ [ npm . version ] ] )
75
+ t . strictSame ( outputs , [ [ npm . version ] ] )
86
76
t . strictSame ( exitHandlerCalled , [ ] )
87
77
} )
88
78
89
79
t . test ( 'calling with --versions calls npm version with no args' , async t => {
90
80
const proc = processMock ( {
91
81
argv : [ 'node' , 'npm' , 'install' , 'or' , 'whatever' , '--versions' ] ,
92
82
} )
93
- const npm = npmMock ( )
83
+ const { npm, outputs } = mockNpm ( t )
94
84
const cli = cliMock ( npm )
95
85
96
86
let versionArgs
@@ -110,7 +100,7 @@ t.test('calling with --versions calls npm version with no args', async t => {
110
100
[ 'info' , 'using' , 'node@%s' , process . version ] ,
111
101
] )
112
102
113
- t . strictSame ( npmOutputs , [ ] )
103
+ t . strictSame ( outputs , [ ] )
114
104
t . strictSame ( exitHandlerCalled , [ ] )
115
105
} )
116
106
@@ -119,10 +109,10 @@ t.test('print usage if no params provided', async t => {
119
109
argv : [ 'node' , 'npm' ] ,
120
110
} )
121
111
122
- const npm = npmMock ( )
112
+ const { npm, outputs } = mockNpm ( t )
123
113
const cli = cliMock ( npm )
124
114
await cli ( proc )
125
- t . match ( npmOutputs [ 0 ] [ 0 ] , 'Usage:' , 'outputs npm usage' )
115
+ t . match ( outputs [ 0 ] [ 0 ] , 'Usage:' , 'outputs npm usage' )
126
116
t . match ( exitHandlerCalled , [ ] , 'should call exitHandler with no args' )
127
117
t . ok ( exitHandlerNpm , 'exitHandler npm is set' )
128
118
t . match ( proc . exitCode , 1 )
@@ -133,11 +123,11 @@ t.test('print usage if non-command param provided', async t => {
133
123
argv : [ 'node' , 'npm' , 'tset' ] ,
134
124
} )
135
125
136
- const npm = npmMock ( )
126
+ const { npm, outputs } = mockNpm ( t )
137
127
const cli = cliMock ( npm )
138
128
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?' )
141
131
t . match ( exitHandlerCalled , [ ] , 'should call exitHandler with no args' )
142
132
t . ok ( exitHandlerNpm , 'exitHandler npm is set' )
143
133
t . match ( proc . exitCode , 1 )
@@ -148,7 +138,7 @@ t.test('load error calls error handler', async t => {
148
138
argv : [ 'node' , 'npm' , 'asdf' ] ,
149
139
} )
150
140
151
- const npm = npmMock ( )
141
+ const { npm } = mockNpm ( t )
152
142
const cli = cliMock ( npm )
153
143
const er = new Error ( 'test load error' )
154
144
npm . load = ( ) => Promise . reject ( er )
0 commit comments