@@ -181,69 +181,73 @@ t.test('global', async t => {
181181 t . matchSnapshot ( joinedOutput ( ) , 'should return global package' )
182182} )
183183
184- t . test ( 'expect entries' , t => {
185- const prefixDir = {
186- node_modules : {
187- a : { name : 'a' , version : '1.0.0' } ,
188- } ,
189- 'package.json' : JSON . stringify ( {
190- name : 'project' ,
191- dependencies : { a : '^1.0.0' } ,
192- } ) ,
184+ t . test ( 'expect entries' , async t => {
185+ const mockExpect = async ( t , query , expectEntries ) => {
186+ const { joinedOutput } = await loadMockNpm ( t , {
187+ command : 'query' ,
188+ exec : [ query ] ,
189+ config : { 'expect-entries' : expectEntries } ,
190+ prefixDir : {
191+ node_modules : {
192+ a : { name : 'a' , version : '1.0.0' } ,
193+ b : { name : 'b' , version : '1.0.0' } ,
194+ c : { name : 'c' , version : '1.0.0' } ,
195+ } ,
196+ 'package.json' : JSON . stringify ( {
197+ name : 'project' ,
198+ dependencies : { a : '^1.0.0' , b : '^1.0.0' , c : '^1.0.0' } ,
199+ } ) ,
200+ } ,
201+ } )
202+ return {
203+ exitCode : process . exitCode ,
204+ entries : JSON . parse ( joinedOutput ( ) ) ,
205+ }
193206 }
207+
194208 t . test ( 'false, has entries' , async t => {
195- const { npm, joinedOutput } = await loadMockNpm ( t , {
196- prefixDir,
197- config : { 'expect-entries' : false } ,
198- } )
199- await npm . exec ( 'query' , [ '#a' ] )
200- t . not ( joinedOutput ( ) , '[]' , 'has entries' )
201- t . ok ( process . exitCode , 'exits with code' )
209+ const { exitCode, entries } = await mockExpect ( t , ':root > .prod' , false )
210+ t . equal ( entries . length , 3 , 'has entries' )
211+ t . ok ( exitCode , 'exits with code' )
202212 } )
203213 t . test ( 'false, no entries' , async t => {
204- const { npm, joinedOutput } = await loadMockNpm ( t , {
205- prefixDir,
206- config : { 'expect-entries' : false } ,
207- } )
208- await npm . exec ( 'query' , [ '#b' ] )
209- t . equal ( joinedOutput ( ) , '[]' , 'does not have entries' )
210- t . notOk ( process . exitCode , 'exits without code' )
214+ const { exitCode, entries } = await mockExpect ( t , ':root > .dev' , false )
215+ t . equal ( entries . length , 0 , 'does not have entries' )
216+ t . notOk ( exitCode , 'exits without code' )
211217 } )
212218 t . test ( 'true, has entries' , async t => {
213- const { npm, joinedOutput } = await loadMockNpm ( t , {
214- prefixDir,
215- config : { 'expect-entries' : true } ,
216- } )
217- await npm . exec ( 'query' , [ '#a' ] )
218- t . not ( joinedOutput ( ) , '[]' , 'has entries' )
219- t . notOk ( process . exitCode , 'exits without code' )
219+ const { exitCode, entries } = await mockExpect ( t , ':root > .prod' , true )
220+ t . equal ( entries . length , 3 , 'has entries' )
221+ t . notOk ( exitCode , 'exits without code' )
220222 } )
221223 t . test ( 'true, no entries' , async t => {
222- const { npm, joinedOutput } = await loadMockNpm ( t , {
223- prefixDir,
224- config : { 'expect-entries' : true } ,
225- } )
226- await npm . exec ( 'query' , [ '#b' ] )
227- t . equal ( joinedOutput ( ) , '[]' , 'does not have entries' )
228- t . ok ( process . exitCode , 'exits with code' )
224+ const { exitCode, entries } = await mockExpect ( t , ':root > .dev' , true )
225+ t . equal ( entries . length , 0 , 'does not have entries' )
226+ t . ok ( exitCode , 'exits with code' )
229227 } )
230228 t . test ( 'number, matches' , async t => {
231- const { npm, joinedOutput } = await loadMockNpm ( t , {
232- prefixDir,
233- config : { 'expect-entries' : 1 } ,
234- } )
235- await npm . exec ( 'query' , [ '#a' ] )
236- t . not ( joinedOutput ( ) , '[]' , 'has entries' )
237- t . notOk ( process . exitCode , 'exits without code' )
229+ const { exitCode, entries } = await mockExpect ( t , ':root > .prod' , 3 )
230+ t . equal ( entries . length , 3 , 'has entries' )
231+ t . notOk ( exitCode , 'exits without code' )
238232 } )
239233 t . test ( 'number, does not match' , async t => {
240- const { npm, joinedOutput } = await loadMockNpm ( t , {
241- prefixDir,
242- config : { 'expect-entries' : 1 } ,
243- } )
244- await npm . exec ( 'query' , [ '#b' ] )
245- t . equal ( joinedOutput ( ) , '[]' , 'does not have entries' )
246- t . ok ( process . exitCode , 'exits with code' )
234+ const { exitCode, entries } = await mockExpect ( t , ':root > .prod' , 2 )
235+ t . equal ( entries . length , 3 , 'has no entries' )
236+ t . ok ( exitCode , 'exits with code' )
237+ } )
238+ t . test ( 'number, no entries' , async t => {
239+ const { exitCode, entries } = await mockExpect ( t , ':root > .dev' , 2 )
240+ t . equal ( entries . length , 0 , 'has no entries' )
241+ t . ok ( exitCode , 'exits with code' )
242+ } )
243+ t . test ( 'zero, no entries' , async t => {
244+ const { exitCode, entries } = await mockExpect ( t , ':root > .dev' , 0 )
245+ t . equal ( entries . length , 0 , 'has no entries' )
246+ t . notOk ( exitCode , 'exits without code' )
247+ } )
248+ t . test ( 'zero, has entries' , async t => {
249+ const { exitCode, entries } = await mockExpect ( t , ':root > .prod' , 0 )
250+ t . equal ( entries . length , 3 , 'has no entries' )
251+ t . ok ( exitCode , 'exits with code' )
247252 } )
248- t . end ( )
249253} )
0 commit comments