11import assert from 'node:assert' ;
2+ import { esmocha , expect } from 'esmocha' ;
23import _ from 'lodash' ;
3- import inquirer from 'inquirer' ;
44import nock from 'nock' ;
5- import * as td from 'testdouble' ;
6- import sinon from 'sinon' ;
75import registryUrlFactory from 'registry-url' ;
86import Router from '../lib/router.js' ;
97import * as helpers from './helpers.js' ;
108
9+ const spawn = await esmocha . mock ( 'cross-spawn' , {
10+ default : esmocha . fn ( ) . mockReturnValue ( {
11+ on : esmocha . fn ( ) . mockImplementation ( function ( name , callback ) {
12+ if ( name === 'close' ) {
13+ callback ( ) ;
14+ }
15+
16+ return this ;
17+ } ) ,
18+ } ) ,
19+ } ) ;
20+
21+ esmocha . spyOn ( _ , 'memoize' ) . mockImplementation ( function_ => function_ ) ;
22+ const { default : inquirer } = await esmocha . mock ( 'inquirer' ) ;
23+ const { install} = await import ( '../lib/routes/install.js' ) ;
24+ esmocha . reset ( ) ;
25+ _ . memoize . mockRestore ( ) ;
26+
1127const registryUrl = registryUrlFactory ( ) ;
1228
1329describe ( 'install route' , ( ) => {
1430 beforeEach ( async function ( ) {
15- this . sandbox = sinon . createSandbox ( ) ;
1631 this . env = await helpers . fakeEnv ( ) ;
17- this . homeRoute = sinon . stub ( ) . returns ( Promise . resolve ( ) ) ;
32+ this . homeRoute = esmocha . fn ( ) . mockResolvedValue ( ) ;
1833 this . router = new Router ( this . env ) ;
1934 this . router . registerRoute ( 'home' , this . homeRoute ) ;
20- this . spawn = helpers . fakeCrossSpawn ( 'close' ) ;
21- await td . replaceEsm ( 'cross-spawn' , undefined , this . spawn ) ;
22-
23- const { install} = await import ( '../lib/routes/install.js' ) ;
2435
2536 this . router . registerRoute ( 'install' , install ) ;
2637 this . env . registerStub ( _ . noop , 'generator-unicorn' ) ;
2738 } ) ;
2839
29- afterEach ( function ( ) {
30- this . sandbox . restore ( ) ;
31- td . reset ( ) ;
40+ afterEach ( ( ) => {
41+ esmocha . clearAllMocks ( ) ;
42+ nock . cleanAll ( ) ;
3243 } ) ;
3344
3445 describe ( 'npm success with results' , ( ) => {
@@ -83,6 +94,7 @@ describe('install route', () => {
8394 nock ( registryUrl )
8495 . get ( '/-/v1/search' )
8596 . query ( true )
97+ . times ( 4 )
8698 . reply ( 200 , { objects : this . packages . map ( data => ( { package : data } ) ) } )
8799 . filteringPath ( / \/ [ ^ ? ] + $ / g, '/pkg' )
88100 . get ( '/pkg' )
@@ -95,13 +107,9 @@ describe('install route', () => {
95107 . reply ( 200 , this . blacklist ) ;
96108 } ) ;
97109
98- afterEach ( ( ) => {
99- nock . cleanAll ( ) ;
100- } ) ;
101-
102110 it ( 'filters already installed generators and match search term' , function ( done ) {
103111 let call = 0 ;
104- this . sandbox . stub ( inquirer , ' prompt' ) . callsFake ( argument => {
112+ inquirer . prompt . mockImplementation ( argument => {
105113 call ++ ;
106114 if ( call === 1 ) {
107115 return Promise . resolve ( { searchTerm : 'unicorn' } ) ;
@@ -124,7 +132,7 @@ describe('install route', () => {
124132
125133 it ( 'filters blacklisted generators and match search term' , function ( done ) {
126134 let call = 0 ;
127- this . sandbox . stub ( inquirer , ' prompt' ) . callsFake ( argument => {
135+ inquirer . prompt . mockImplementation ( argument => {
128136 call ++ ;
129137 if ( call === 1 ) {
130138 return Promise . resolve ( { searchTerm : 'blacklist' } ) ;
@@ -144,33 +152,32 @@ describe('install route', () => {
144152 this . router . navigate ( 'install' ) ;
145153 } ) ;
146154
147- it ( 'allow redo the search' , function ( done ) {
155+ it ( 'allow redo the search' , async function ( ) {
148156 let call = 0 ;
149- this . sandbox . stub ( inquirer , ' prompt' ) . callsFake ( argument => {
157+ inquirer . prompt . mockImplementation ( async argument => {
150158 call ++ ;
151159 if ( call === 1 ) {
152- return Promise . resolve ( { searchTerm : 'unicorn' } ) ;
160+ return { searchTerm : 'unicorn' } ;
153161 }
154162
155163 if ( call === 2 ) {
156- return Promise . resolve ( { toInstall : 'install' } ) ;
164+ return { toInstall : 'install' } ;
157165 }
158166
159167 if ( call === 3 ) {
160168 assert . strictEqual ( argument [ 0 ] . name , 'searchTerm' ) ;
161- return Promise . resolve ( { searchTerm : 'unicorn' } ) ;
169+ return { searchTerm : 'unicorn' } ;
162170 }
163171
164- done ( ) ;
165- return Promise . resolve ( { toInstall : 'home' } ) ;
172+ return { toInstall : 'home' } ;
166173 } ) ;
167174
168- this . router . navigate ( 'install' ) ;
175+ await this . router . navigate ( 'install' ) ;
169176 } ) ;
170177
171178 it ( 'allow going back home' , function ( ) {
172179 let call = 0 ;
173- this . sandbox . stub ( inquirer , ' prompt' ) . callsFake ( ( ) => {
180+ inquirer . prompt . mockImplementation ( ( ) => {
174181 call ++ ;
175182 if ( call === 1 ) {
176183 return Promise . resolve ( { searchTerm : 'unicorn' } ) ;
@@ -180,13 +187,13 @@ describe('install route', () => {
180187 } ) ;
181188
182189 return this . router . navigate ( 'install' ) . then ( ( ) => {
183- sinon . assert . calledOnce ( this . homeRoute ) ;
190+ expect ( this . homeRoute ) . toHaveBeenCalledTimes ( 1 ) ;
184191 } ) ;
185192 } ) ;
186193
187194 it ( 'install a generator' , function ( ) {
188195 let call = 0 ;
189- this . sandbox . stub ( inquirer , ' prompt' ) . callsFake ( ( ) => {
196+ inquirer . prompt . mockImplementation ( ( ) => {
190197 call ++ ;
191198 if ( call === 1 ) {
192199 return Promise . resolve ( { searchTerm : 'unicorn' } ) ;
@@ -200,9 +207,9 @@ describe('install route', () => {
200207 } ) ;
201208
202209 return this . router . navigate ( 'install' ) . then ( ( ) => {
203- sinon . assert . calledWith ( this . spawn , 'npm' , [ 'install' , '--global' , 'generator-unicorn' ] , { stdio : 'inherit' } ) ;
204- sinon . assert . calledOnce ( this . spawn ) ;
205- sinon . assert . calledOnce ( this . homeRoute ) ;
210+ expect ( spawn . default ) . toHaveBeenCalledTimes ( 1 ) ;
211+ expect ( spawn . default ) . toHaveBeenCalledWith ( 'npm' , [ 'install' , '--global' , 'generator-unicorn' ] , { stdio : 'inherit' } ) ;
212+ expect ( this . homeRoute ) . toHaveBeenCalledTimes ( 1 ) ;
206213 } ) ;
207214 } ) ;
208215 } ) ;
@@ -230,10 +237,10 @@ describe('install route', () => {
230237 } ) ;
231238 } ) ;
232239
233- it ( 'list options if search have no results' , function ( done ) {
240+ it ( 'list options if search have no results' , async function ( ) {
234241 let call = 0 ;
235242
236- this . sandbox . stub ( inquirer , ' prompt' ) . callsFake ( argument => {
243+ inquirer . prompt . mockImplementation ( argument => {
237244 call ++ ;
238245
239246 if ( call === 1 ) {
@@ -243,13 +250,12 @@ describe('install route', () => {
243250 if ( call === 2 ) {
244251 const { choices} = argument [ 0 ] ;
245252 assert . deepStrictEqual ( _ . map ( choices , 'value' ) , [ 'install' , 'home' ] ) ;
246- done ( ) ;
247253 }
248254
249255 return Promise . resolve ( { toInstall : 'home' } ) ;
250256 } ) ;
251257
252- this . router . navigate ( 'install' ) ;
258+ await this . router . navigate ( 'install' ) ;
253259 } ) ;
254260 } ) ;
255261} ) ;
0 commit comments