@@ -14,80 +14,79 @@ import dependencyTree from '../index.js';
14
14
const require = createRequire ( import . meta. url ) ;
15
15
const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
16
16
17
- describe ( 'dependencyTree' , ( ) => {
18
- function testTreesForFormat ( format , ext = '.js' ) {
19
- it ( 'returns an object form of the dependency tree for a file' , ( ) => {
20
- const root = path . join ( __dirname , `/fixtures/${ format } ` ) ;
21
- const filename = path . normalize ( `${ root } /a${ ext } ` ) ;
22
-
23
- const tree = dependencyTree ( { filename, root } ) ;
17
+ let _directory ;
24
18
25
- assert . ok ( tree instanceof Object ) ;
19
+ function testTreesForFormat ( format , ext = '.js' ) {
20
+ it ( 'returns an object form of the dependency tree for a file' , ( ) => {
21
+ const root = path . join ( __dirname , `/fixtures/${ format } ` ) ;
22
+ const filename = path . normalize ( `${ root } /a${ ext } ` ) ;
26
23
27
- const aSubTree = tree [ filename ] ;
28
-
29
- assert . ok ( aSubTree instanceof Object ) ;
30
- const filesInSubTree = Object . keys ( aSubTree ) ;
24
+ const tree = dependencyTree ( { filename, root } ) ;
25
+ const aSubTree = tree [ filename ] ;
26
+ const filesInSubTree = Object . keys ( aSubTree ) ;
31
27
32
- assert . equal ( filesInSubTree . length , 2 ) ;
33
- } ) ;
34
- }
28
+ assert . ok ( tree instanceof Object ) ;
29
+ assert . ok ( aSubTree instanceof Object ) ;
30
+ assert . equal ( filesInSubTree . length , 2 ) ;
31
+ } ) ;
32
+ }
35
33
36
- function mockStylus ( ) {
37
- mockfs ( {
38
- [ path . join ( __dirname , '/fixtures/stylus' ) ] : {
39
- 'a.styl' : `
34
+ function mockStylus ( ) {
35
+ mockfs ( {
36
+ [ path . join ( __dirname , '/fixtures/stylus' ) ] : {
37
+ 'a.styl' : `
40
38
@import "b"
41
39
@require "c.styl"
42
40
` ,
43
- 'b.styl' : '@import "c"' ,
44
- 'c.styl' : ''
45
- }
46
- } ) ;
47
- }
41
+ 'b.styl' : '@import "c"' ,
42
+ 'c.styl' : ''
43
+ }
44
+ } ) ;
45
+ }
48
46
49
- function mockSass ( ) {
50
- mockfs ( {
51
- [ path . join ( __dirname , '/fixtures/sass' ) ] : {
52
- 'a.scss' : `
47
+ function mockSass ( ) {
48
+ mockfs ( {
49
+ [ path . join ( __dirname , '/fixtures/sass' ) ] : {
50
+ 'a.scss' : `
53
51
@import "_b";
54
52
@import "_c.scss";
55
53
` ,
56
- '_b.scss' : 'body { color: blue; }' ,
57
- '_c.scss' : 'body { color: pink; }'
58
- }
59
- } ) ;
60
- }
54
+ '_b.scss' : 'body { color: blue; }' ,
55
+ '_c.scss' : 'body { color: pink; }'
56
+ }
57
+ } ) ;
58
+ }
61
59
62
- function mockLess ( ) {
63
- mockfs ( {
64
- [ path . join ( __dirname , '/fixtures/less' ) ] : {
65
- 'a.less' : `
60
+ function mockLess ( ) {
61
+ mockfs ( {
62
+ [ path . join ( __dirname , '/fixtures/less' ) ] : {
63
+ 'a.less' : `
66
64
@import "b.css";
67
65
@import "c.less";
68
66
` ,
69
- 'b.css' : 'body { color: blue; }' ,
70
- 'c.less' : 'body { color: pink; }'
71
- }
72
- } ) ;
73
- }
67
+ 'b.css' : 'body { color: blue; }' ,
68
+ 'c.less' : 'body { color: pink; }'
69
+ }
70
+ } ) ;
71
+ }
74
72
75
- function mockes6 ( ) {
76
- mockfs ( {
77
- [ path . join ( __dirname , '/fixtures/es6' ) ] : {
78
- 'a.js' : `
73
+ function mockEs6 ( ) {
74
+ mockfs ( {
75
+ [ path . join ( __dirname , '/fixtures/es6' ) ] : {
76
+ 'a.js' : `
79
77
import b from './b';
80
78
import c from './c';
81
79
` ,
82
- 'b.js' : 'export default function() {};' ,
83
- 'c.js' : 'export default function() {};' ,
84
- 'jsx.js' : 'import c from "./c";\n export default <jsx />;' ,
85
- 'foo.jsx' : 'import React from "react";\n import b from "b";\n export default <jsx />;' ,
86
- 'es7.js' : 'import c from "./c";\n export default async function foo() {};'
87
- }
88
- } ) ;
89
- }
80
+ 'b.js' : 'export default function() {};' ,
81
+ 'c.js' : 'export default function() {};' ,
82
+ 'jsx.js' : 'import c from "./c";\n export default <jsx />;' ,
83
+ 'foo.jsx' : 'import React from "react";\n import b from "b";\n export default <jsx />;' ,
84
+ 'es7.js' : 'import c from "./c";\n export default async function foo() {};'
85
+ }
86
+ } ) ;
87
+ }
90
88
89
+ describe ( 'dependencyTree' , ( ) => {
91
90
afterEach ( ( ) => {
92
91
mockfs . restore ( ) ;
93
92
} ) ;
@@ -327,36 +326,42 @@ describe('dependencyTree', () => {
327
326
} ) ;
328
327
329
328
describe ( 'throws' , ( ) => {
330
- beforeEach ( function ( ) {
331
- this . _directory = path . join ( __dirname , '/fixtures/commonjs' ) ;
329
+ beforeEach ( ( ) => {
330
+ _directory = path . join ( __dirname , '/fixtures/commonjs' ) ;
332
331
} ) ;
333
332
334
333
it ( 'throws if the filename is missing' , ( ) => {
335
- assert . throws ( function ( ) {
334
+ assert . throws ( ( ) => {
336
335
dependencyTree ( {
337
336
filename : undefined ,
338
- directory : this . _directory
337
+ directory : _directory
339
338
} ) ;
340
- } ) ;
339
+ } , / ^ E r r o r : f i l e n a m e n o t g i v e n $ / ) ;
341
340
} ) ;
342
341
343
342
it ( 'throws if the root is missing' , ( ) => {
344
343
assert . throws ( ( ) => {
345
344
dependencyTree ( { undefined } ) ;
346
- } ) ;
345
+ } , / ^ E r r o r : f i l e n a m e n o t g i v e n $ / ) ;
347
346
} ) ;
348
347
349
- it ( 'throws if a supplied filter is not a function ' , ( ) => {
348
+ it ( 'throws if the directory is missing ' , ( ) => {
350
349
assert . throws ( ( ) => {
351
- const directory = path . join ( __dirname , '/fixtures/onlyRealDeps' ) ;
352
- const filename = path . normalize ( `${ directory } /a.js` ) ;
350
+ dependencyTree ( { filename : 'foo.js' , directory : undefined } ) ;
351
+ } , / ^ E r r o r : d i r e c t o r y n o t g i v e n $ / ) ;
352
+ } ) ;
353
+
354
+ it ( 'throws if a supplied filter is not a function' , ( ) => {
355
+ const directory = path . join ( __dirname , '/fixtures/onlyRealDeps' ) ;
356
+ const filename = path . normalize ( `${ directory } /a.js` ) ;
353
357
358
+ assert . throws ( ( ) => {
354
359
dependencyTree ( {
355
360
filename,
356
361
directory,
357
362
filter : 'foobar'
358
363
} ) ;
359
- } ) ;
364
+ } , / ^ E r r o r : f i l t e r m u s t b e a f u n c t i o n $ / ) ;
360
365
} ) ;
361
366
362
367
it ( 'does not throw on the legacy `root` option' , ( ) => {
@@ -373,21 +378,21 @@ describe('dependencyTree', () => {
373
378
} ) ;
374
379
375
380
describe ( 'on file error' , ( ) => {
376
- beforeEach ( function ( ) {
377
- this . _directory = path . join ( __dirname , '/fixtures/commonjs' ) ;
381
+ beforeEach ( ( ) => {
382
+ _directory = path . join ( __dirname , '/fixtures/commonjs' ) ;
378
383
} ) ;
379
384
380
- it ( 'does not throw' , function ( ) {
385
+ it ( 'does not throw' , ( ) => {
381
386
assert . doesNotThrow ( ( ) => {
382
387
dependencyTree ( {
383
388
filename : 'foo' ,
384
- directory : this . _directory
389
+ directory : _directory
385
390
} ) ;
386
391
} ) ;
387
392
} ) ;
388
393
389
- it ( 'returns no dependencies' , function ( ) {
390
- const tree = dependencyTree ( { filename : 'foo' , directory : this . _directory } ) ;
394
+ it ( 'returns no dependencies' , ( ) => {
395
+ const tree = dependencyTree ( { filename : 'foo' , directory : _directory } ) ;
391
396
// eslint-disable-next-line unicorn/explicit-length-check
392
397
assert . ok ( ! tree . length ) ;
393
398
} ) ;
@@ -418,15 +423,17 @@ describe('dependencyTree', () => {
418
423
} ) ;
419
424
420
425
describe ( 'memoization (#2)' , ( ) => {
421
- beforeEach ( function ( ) {
422
- this . _spy = sinon . spy ( dependencyTree , '_getDependencies' ) ;
426
+ let _spy ;
427
+
428
+ beforeEach ( ( ) => {
429
+ _spy = sinon . spy ( dependencyTree , '_getDependencies' ) ;
423
430
} ) ;
424
431
425
432
afterEach ( ( ) => {
426
433
dependencyTree . _getDependencies . restore ( ) ;
427
434
} ) ;
428
435
429
- it ( 'accepts a cache object for memoization (#2)' , function ( ) {
436
+ it ( 'accepts a cache object for memoization (#2)' , ( ) => {
430
437
const filename = path . join ( __dirname , '/fixtures/amd/a.js' ) ;
431
438
const directory = path . join ( __dirname , '/fixtures/amd' ) ;
432
439
const cache = { } ;
@@ -443,16 +450,15 @@ describe('dependencyTree', () => {
443
450
} ) ;
444
451
445
452
assert . equal ( Object . keys ( tree [ filename ] ) . length , 2 ) ;
446
- assert . ok ( this . _spy . neverCalledWith ( path . join ( __dirname , '/fixtures/amd/b.js' ) ) ) ;
453
+ assert . ok ( _spy . neverCalledWith ( path . join ( __dirname , '/fixtures/amd/b.js' ) ) ) ;
447
454
} ) ;
448
455
449
456
it ( 'returns the precomputed list of a cached entry point' , ( ) => {
450
457
const filename = path . join ( __dirname , '/fixtures/amd/a.js' ) ;
451
458
const directory = path . join ( __dirname , '/fixtures/amd' ) ;
452
459
453
460
const cache = {
454
- // Shouldn't process the first file's tree
455
- [ filename ] : [ ]
461
+ [ filename ] : [ ] // Shouldn't process the first file's tree
456
462
} ;
457
463
458
464
const tree = dependencyTree ( {
@@ -530,41 +536,41 @@ describe('dependencyTree', () => {
530
536
} ) ;
531
537
532
538
describe ( 'es6' , ( ) => {
533
- beforeEach ( function ( ) {
534
- this . _directory = path . join ( __dirname , '/fixtures/es6' ) ;
535
- mockes6 ( ) ;
539
+ beforeEach ( ( ) => {
540
+ _directory = path . join ( __dirname , '/fixtures/es6' ) ;
541
+ mockEs6 ( ) ;
536
542
} ) ;
537
543
538
544
testTreesForFormat ( 'es6' ) ;
539
545
540
- it ( 'resolves files that have jsx' , function ( ) {
541
- const filename = path . normalize ( `${ this . _directory } /jsx.js` ) ;
546
+ it ( 'resolves files that have jsx' , ( ) => {
547
+ const filename = path . normalize ( `${ _directory } /jsx.js` ) ;
542
548
const { [ filename ] : tree } = dependencyTree ( {
543
549
filename,
544
- directory : this . _directory
550
+ directory : _directory
545
551
} ) ;
546
552
547
- assert . ok ( tree [ path . normalize ( `${ this . _directory } /c.js` ) ] ) ;
553
+ assert . ok ( tree [ path . normalize ( `${ _directory } /c.js` ) ] ) ;
548
554
} ) ;
549
555
550
- it ( 'resolves files with a jsx extension' , function ( ) {
551
- const filename = path . normalize ( `${ this . _directory } /foo.jsx` ) ;
556
+ it ( 'resolves files with a jsx extension' , ( ) => {
557
+ const filename = path . normalize ( `${ _directory } /foo.jsx` ) ;
552
558
const { [ filename ] : tree } = dependencyTree ( {
553
559
filename,
554
- directory : this . _directory
560
+ directory : _directory
555
561
} ) ;
556
562
557
- assert . ok ( tree [ path . normalize ( `${ this . _directory } /b.js` ) ] ) ;
563
+ assert . ok ( tree [ path . normalize ( `${ _directory } /b.js` ) ] ) ;
558
564
} ) ;
559
565
560
- it ( 'resolves files that have es7' , function ( ) {
561
- const filename = path . normalize ( `${ this . _directory } /es7.js` ) ;
566
+ it ( 'resolves files that have es7' , ( ) => {
567
+ const filename = path . normalize ( `${ _directory } /es7.js` ) ;
562
568
const { [ filename ] : tree } = dependencyTree ( {
563
569
filename,
564
- directory : this . _directory
570
+ directory : _directory
565
571
} ) ;
566
572
567
- assert . ok ( tree [ path . normalize ( `${ this . _directory } /c.js` ) ] ) ;
573
+ assert . ok ( tree [ path . normalize ( `${ _directory } /c.js` ) ] ) ;
568
574
} ) ;
569
575
570
576
describe ( 'when given an es6 file using CJS lazy requires' , ( ) => {
@@ -795,7 +801,7 @@ describe('dependencyTree', () => {
795
801
796
802
describe ( 'es6' , ( ) => {
797
803
beforeEach ( ( ) => {
798
- mockes6 ( ) ;
804
+ mockEs6 ( ) ;
799
805
} ) ;
800
806
801
807
testToList ( 'es6' ) ;
@@ -832,30 +838,32 @@ describe('dependencyTree', () => {
832
838
} ) ;
833
839
834
840
describe ( 'webpack' , ( ) => {
835
- beforeEach ( function ( ) {
841
+ let testResolution ;
842
+
843
+ beforeEach ( ( ) => {
836
844
// Note: not mocking because webpack's resolver needs a real project with dependencies;
837
845
// otherwise, we'd have to mock a ton of files.
838
- this . _root = path . join ( __dirname , '../' ) ;
839
- this . _webpackConfig = `${ this . _root } /webpack.config.js` ;
846
+ const root = path . join ( __dirname , '../' ) ;
847
+ const webpackConfig = `${ root } /webpack.config.js` ;
840
848
841
- this . _testResolution = name => {
849
+ testResolution = name => {
842
850
const results = dependencyTree . toList ( {
843
851
filename : path . join ( __dirname , `/fixtures/webpack/${ name } .js` ) ,
844
- directory : this . _root ,
845
- webpackConfig : this . _webpackConfig ,
852
+ directory : root ,
853
+ webpackConfig,
846
854
filter : filename => filename . includes ( 'filing-cabinet' )
847
855
} ) ;
848
856
849
857
assert . ok ( results . some ( filename => filename . includes ( path . normalize ( 'node_modules/filing-cabinet' ) ) ) ) ;
850
858
} ;
851
859
} ) ;
852
860
853
- it ( 'resolves aliased modules' , function ( ) {
854
- this . _testResolution ( 'aliased' ) ;
861
+ it ( 'resolves aliased modules' , ( ) => {
862
+ testResolution ( 'aliased' ) ;
855
863
} ) ;
856
864
857
- it ( 'resolves unaliased modules' , function ( ) {
858
- this . _testResolution ( 'unaliased' ) ;
865
+ it ( 'resolves unaliased modules' , ( ) => {
866
+ testResolution ( 'unaliased' ) ;
859
867
} ) ;
860
868
} ) ;
861
869
0 commit comments