@@ -175,28 +175,99 @@ describe('cli', () => {
175175 }
176176 } )
177177
178- test ( 'supports --prefer-local to load modules' , async ( ) => {
179- const cwd = tmpdir ( )
180- const external = tmpdir ( )
181- await fs . outputJson ( path . join ( external , 'node_modules/a/package.json' ) , {
178+ describe ( '`--prefer-local`' , ( ) => {
179+ const pkgIndex = `export const a = 'AAA'`
180+ const pkgJson = {
182181 name : 'a' ,
183182 version : '1.0.0' ,
184183 type : 'module' ,
185184 exports : './index.js' ,
186- } )
187- await fs . outputFile (
188- path . join ( external , 'node_modules/a/index.js' ) ,
189- `
190- export const a = 'AAA'
191- `
192- )
185+ }
193186 const script = `
194187import {a} from 'a'
195188console.log(a);
196189`
197- const out =
198- await $ `node build/cli.js --cwd=${ cwd } --prefer-local=${ external } --test <<< ${ script } `
199- assert . equal ( out . stdout , 'AAA\n' )
190+
191+ test ( 'true' , async ( ) => {
192+ const cwd = tmpdir ( )
193+ await fs . outputFile ( path . join ( cwd , 'node_modules/a/index.js' ) , pkgIndex )
194+ await fs . outputJson (
195+ path . join ( cwd , 'node_modules/a/package.json' ) ,
196+ pkgJson
197+ )
198+
199+ const out =
200+ await $ `node build/cli.js --cwd=${ cwd } --prefer-local=true --test <<< ${ script } `
201+ assert . equal ( out . stdout , 'AAA\n' )
202+ assert . ok ( await fs . exists ( path . join ( cwd , 'node_modules/a/index.js' ) ) )
203+ } )
204+
205+ test ( 'external dir' , async ( ) => {
206+ const cwd = tmpdir ( )
207+ const external = tmpdir ( )
208+ await fs . outputFile (
209+ path . join ( external , 'node_modules/a/index.js' ) ,
210+ pkgIndex
211+ )
212+ await fs . outputJson (
213+ path . join ( external , 'node_modules/a/package.json' ) ,
214+ pkgJson
215+ )
216+
217+ const out =
218+ await $ `node build/cli.js --cwd=${ cwd } --prefer-local=${ external } --test <<< ${ script } `
219+ assert . equal ( out . stdout , 'AAA\n' )
220+ assert . ok ( await fs . exists ( path . join ( external , 'node_modules/a/index.js' ) ) )
221+ } )
222+
223+ test ( 'external alias' , async ( ) => {
224+ const cwd = tmpdir ( )
225+ const external = tmpdir ( )
226+ await fs . outputFile (
227+ path . join ( external , 'node_modules/a/index.js' ) ,
228+ pkgIndex
229+ )
230+ await fs . outputJson (
231+ path . join ( external , 'node_modules/a/package.json' ) ,
232+ pkgJson
233+ )
234+ await fs . symlinkSync (
235+ path . join ( external , 'node_modules' ) ,
236+ path . join ( cwd , 'node_modules' ) ,
237+ 'junction'
238+ )
239+
240+ const out =
241+ await $ `node build/cli.js --cwd=${ cwd } --prefer-local=true --test <<< ${ script } `
242+ assert . equal ( out . stdout , 'AAA\n' )
243+ assert . ok ( await fs . exists ( path . join ( cwd , 'node_modules' ) ) )
244+ } )
245+
246+ test ( 'throws if exists' , async ( ) => {
247+ const cwd = tmpdir ( )
248+ const external = tmpdir ( )
249+ await fs . outputFile ( path . join ( cwd , 'node_modules/a/index.js' ) , pkgIndex )
250+ await fs . outputFile (
251+ path . join ( external , 'node_modules/a/index.js' ) ,
252+ pkgIndex
253+ )
254+ assert . rejects (
255+ ( ) =>
256+ $ `node build/cli.js --cwd=${ cwd } --prefer-local=${ external } --test <<< ${ script } ` ,
257+ / n o d e _ m o d u l e s a l r e a d y e x i s t s /
258+ )
259+ } )
260+
261+ test ( 'throws if not dir' , async ( ) => {
262+ const cwd = tmpdir ( )
263+ const external = tmpdir ( )
264+ await fs . outputFile ( path . join ( external , 'node_modules' ) , pkgIndex )
265+ assert . rejects (
266+ ( ) =>
267+ $ `node build/cli.js --cwd=${ cwd } --prefer-local=${ external } --test <<< ${ script } ` ,
268+ / n o d e _ m o d u l e s d o e s n ' t e x i s t o r i s n o t a d i r e c t o r y /
269+ )
270+ } )
200271 } )
201272
202273 test ( 'scripts from https 200' , async ( ) => {
0 commit comments