@@ -12,18 +12,24 @@ describe('lib/aliases.js', () => {
12
12
13
13
describe ( '#getAliases' , ( ) => {
14
14
it ( 'should read the aliases from package.json config' , ( ) => {
15
- return aliasUtils ( app1Dir ) . getAliases ( ) . then ( aliases => {
16
- aliases . should . eql ( [
17
- { alias : 'myFile' , source : './lib/fileA.js' } ,
18
- { alias : 'other' , source : './lib/other' } ,
19
- ] )
20
- } )
15
+ return aliasUtils ( app1Dir )
16
+ . getAliases ( )
17
+ . then ( aliases => {
18
+ aliases . should . eql ( [
19
+ { alias : 'myFile' , source : 'lib/fileA.js' , isIndex : false } ,
20
+ { alias : 'other/' , source : 'lib/other' , isIndex : true } ,
21
+ { alias : 'nested/cjs/folder/path' , source : 'lib/fileA.js' , isIndex : true } ,
22
+ { alias : 'nested/esm/folder/path' , source : 'lib/fileB.esm.js' , isIndex : false } ,
23
+ ] )
24
+ } )
21
25
} )
22
26
23
27
it ( 'should default to no aliases' , ( ) => {
24
- return aliasUtils ( app2Dir + '/' ) . getAliases ( ) . then ( aliases => {
25
- aliases . should . eql ( [ ] )
26
- } )
28
+ return aliasUtils ( app2Dir + '/' )
29
+ . getAliases ( )
30
+ . then ( aliases => {
31
+ aliases . should . eql ( [ ] )
32
+ } )
27
33
} )
28
34
29
35
it ( 'should fail when no package.json is found' , ( ) => {
@@ -33,52 +39,83 @@ describe('lib/aliases.js', () => {
33
39
34
40
describe ( '#createAlias' , ( ) => {
35
41
it ( 'should create a file in the root dir' , ( ) => {
36
- return aliasUtils ( app3Dir ) . createAlias ( 'file' , 'alias' ) . then ( ( ) => {
37
- return Promise . try ( ( ) => {
38
- fs . statSync ( app3Dir + '/alias.js' )
39
- fs . unlinkSync ( app3Dir + '/alias.js' )
42
+ return aliasUtils ( app3Dir )
43
+ . createAlias ( 'file' , 'alias' )
44
+ . then ( ( ) => {
45
+ return Promise . try ( ( ) => {
46
+ fs . statSync ( app3Dir + '/alias.js' )
47
+ fs . unlinkSync ( app3Dir + '/alias.js' )
48
+ } )
40
49
} )
41
- } )
42
50
} )
43
51
44
52
it ( 'should create .d.ts files when appropriate' , ( ) => {
45
- return aliasUtils ( app1Dir ) . createAlias ( 'lib/other' , 'alias' ) . then ( ( ) => {
46
- return Promise . try ( ( ) => {
47
- const contents = fs . readFileSync ( app1Dir + '/alias.d.ts' , 'utf8' )
48
- contents . should . contain ( 'export * from' )
49
- fs . unlinkSync ( app1Dir + '/alias.js' )
50
- fs . unlinkSync ( app1Dir + '/alias.d.ts' )
53
+ return aliasUtils ( app1Dir )
54
+ . createAlias ( 'lib/other' , 'alias' )
55
+ . then ( ( ) => {
56
+ return Promise . try ( ( ) => {
57
+ const contents = fs . readFileSync ( app1Dir + '/alias.d.ts' , 'utf8' )
58
+ contents . should . contain ( 'export * from' )
59
+ fs . unlinkSync ( app1Dir + '/alias.js' )
60
+ fs . unlinkSync ( app1Dir + '/alias.d.ts' )
61
+ } )
51
62
} )
52
- } )
53
63
} )
54
64
55
65
it ( 'should create an esm file when source looks like esm' , ( ) => {
56
- return aliasUtils ( app1Dir ) . createAlias ( 'lib/fileB.esm.js' , 'alias' ) . then ( ( ) => {
57
- return Promise . try ( ( ) => {
58
- const contents = fs . readFileSync ( app1Dir + '/alias.js' , 'utf8' )
59
- contents . should . match ( / ^ e x p o r t \* f r o m / )
60
- fs . unlinkSync ( app1Dir + '/alias.js' )
66
+ return aliasUtils ( app1Dir )
67
+ . createAlias ( 'lib/fileB.esm.js' , 'alias' )
68
+ . then ( ( ) => {
69
+ return Promise . try ( ( ) => {
70
+ const contents = fs . readFileSync ( app1Dir + '/alias.js' , 'utf8' )
71
+ contents . should . match ( / ^ e x p o r t \* f r o m " .\/ l i b \/ f i l e B " / )
72
+ fs . unlinkSync ( app1Dir + '/alias.js' )
73
+ } )
61
74
} )
62
- } )
63
75
} )
64
76
65
- it ( 'should point the alias to the source file' , ( ) => {
66
- return aliasUtils ( app3Dir ) . createAlias ( './file.js' , 'alias' ) . then ( ( ) => {
67
- return Promise . try ( ( ) => {
68
- const original = require ( app3Dir + '/file.js' )
69
- const aliased = require ( app3Dir + '/alias.js' )
70
- original . should . equal ( aliased )
71
- fs . unlinkSync ( app3Dir + '/alias.js' )
77
+ it ( 'should create an index file when alias ends in /' , ( ) => {
78
+ return aliasUtils ( app1Dir )
79
+ . createAlias ( 'lib/fileB.esm.js' , 'alias' )
80
+ . then ( ( ) => {
81
+ return Promise . try ( ( ) => {
82
+ const contents = fs . readFileSync ( app1Dir + '/alias.js' , 'utf8' )
83
+ contents . should . match ( / ^ e x p o r t \* f r o m " ..\/ l i b \/ f i l e B " / )
84
+ fs . unlinkSync ( app1Dir + '/alias.js' )
85
+ } )
72
86
} )
73
- } )
74
87
} )
75
88
76
- it ( 'should fail for missing files' , ( ) => {
77
- return ( ( ) => aliasUtils ( app3Dir ) . createAlias ( './missing' , 'alias' ) ) . should . throw ( / C a n n o t f i n d / )
89
+ it ( 'should create nested aliases' , ( ) => {
90
+ const expectedPath = app1Dir + '/nested/folder/alias.js'
91
+ return aliasUtils ( app1Dir )
92
+ . createAlias ( 'lib/fileB.esm.js' , 'nested/folder/alias' )
93
+ . then ( ( ) => {
94
+ return Promise . try ( ( ) => {
95
+ const contents = fs . readFileSync ( expectedPath , 'utf8' )
96
+ contents . should . match ( / ^ e x p o r t \* f r o m " ..\/ ..\/ l i b \/ f i l e B " / )
97
+ fs . unlinkSync ( expectedPath )
98
+ } )
99
+ } )
78
100
} )
79
101
80
- it ( 'should fail for nested aliases' , ( ) => {
81
- return aliasUtils ( app3Dir ) . createAlias ( './file' , 'nested/alias' ) . should . eventually . be . rejected
102
+ it ( 'should point the alias to the source file' , ( ) => {
103
+ return aliasUtils ( app3Dir )
104
+ . createAlias ( './file.js' , 'alias' )
105
+ . then ( ( ) => {
106
+ return Promise . try ( ( ) => {
107
+ const original = require ( app3Dir + '/file.js' )
108
+ const aliased = require ( app3Dir + '/alias.js' )
109
+ original . should . equal ( aliased )
110
+ fs . unlinkSync ( app3Dir + '/alias.js' )
111
+ } )
112
+ } )
113
+ } )
114
+
115
+ it ( 'should fail for missing files' , ( ) => {
116
+ return ( ( ) => aliasUtils ( app3Dir ) . createAlias ( './missing' , 'alias' ) ) . should . throw (
117
+ / C a n n o t f i n d / ,
118
+ )
82
119
} )
83
120
84
121
it ( 'should fail for wildcard aliases' , ( ) => {
0 commit comments