@@ -46,6 +46,8 @@ describe('config', () => {
46
46
'/conf/invalid.js' : ( ) => {
47
47
throw new SyntaxError ( 'Unexpected token =' )
48
48
} ,
49
+ '/conf/export-not-function.js' : 'not-a-function' ,
50
+ // '/conf/export-null.js': null, // Same as `/conf/not-exist.js`?
49
51
'/conf/exclude.js' : wrapCfg ( { exclude : [ 'one.js' , 'sub/two.js' ] } ) ,
50
52
'/conf/absolute.js' : wrapCfg ( { files : [ 'http://some.com' , 'https://more.org/file.js' ] } ) ,
51
53
'/conf/both.js' : wrapCfg ( { files : [ 'one.js' , 'two.js' ] , exclude : [ 'third.js' ] } ) ,
@@ -57,6 +59,7 @@ describe('config', () => {
57
59
m = loadFile ( path . join ( __dirname , '/../../lib/config.js' ) , mocks , {
58
60
global : { } ,
59
61
process : mocks . process ,
62
+ Error : Error , // Without this, chai's `.throw()` assertion won't correctly check against constructors.
60
63
require ( path ) {
61
64
if ( mockConfigs [ path ] ) {
62
65
return mockConfigs [ path ]
@@ -123,7 +126,20 @@ describe('config', () => {
123
126
expect ( mocks . process . exit ) . to . have . been . calledWith ( 1 )
124
127
} )
125
128
126
- it ( 'should throw and log error if invalid file' , ( ) => {
129
+ it ( 'should log error and throw if file does not exist AND throwErrors is true' , ( ) => {
130
+ function parseConfig ( ) {
131
+ e . parseConfig ( '/conf/not-exist.js' , { } , { throwErrors : true } )
132
+ }
133
+
134
+ expect ( parseConfig ) . to . throw ( Error , 'Error in config file!\n Error: Cannot find module \'/conf/not-exist.js\'' )
135
+ expect ( logSpy ) . to . have . been . called
136
+ const event = logSpy . lastCall . args
137
+ expect ( event . toString ( ) . split ( '\n' ) . slice ( 0 , 2 ) ) . to . be . deep . equal (
138
+ [ 'Error in config file!' , ' Error: Cannot find module \'/conf/not-exist.js\'' ] )
139
+ expect ( mocks . process . exit ) . not . to . have . been . called
140
+ } )
141
+
142
+ it ( 'should log an error and exit if invalid file' , ( ) => {
127
143
e . parseConfig ( '/conf/invalid.js' , { } )
128
144
129
145
expect ( logSpy ) . to . have . been . called
@@ -133,6 +149,32 @@ describe('config', () => {
133
149
expect ( mocks . process . exit ) . to . have . been . calledWith ( 1 )
134
150
} )
135
151
152
+ it ( 'should log an error and throw if invalid file AND throwErrors is true' , ( ) => {
153
+ function parseConfig ( ) {
154
+ e . parseConfig ( '/conf/invalid.js' , { } , { throwErrors : true } )
155
+ }
156
+
157
+ expect ( parseConfig ) . to . throw ( Error , 'Error in config file!\n SyntaxError: Unexpected token =' )
158
+ expect ( logSpy ) . to . have . been . called
159
+ const event = logSpy . lastCall . args
160
+ expect ( event [ 0 ] ) . to . eql ( 'Error in config file!\n' )
161
+ expect ( event [ 1 ] . message ) . to . eql ( 'Unexpected token =' )
162
+ expect ( mocks . process . exit ) . not . to . have . been . called
163
+ } )
164
+
165
+ it ( 'should log error and throw if file does not export a function AND throwErrors is true' , ( ) => {
166
+ function parseConfig ( ) {
167
+ e . parseConfig ( '/conf/export-not-function.js' , { } , { throwErrors : true } )
168
+ }
169
+
170
+ expect ( parseConfig ) . to . throw ( Error , 'Config file must export a function!\n' )
171
+ expect ( logSpy ) . to . have . been . called
172
+ const event = logSpy . lastCall . args
173
+ expect ( event . toString ( ) . split ( '\n' ) . slice ( 0 , 1 ) ) . to . be . deep . equal (
174
+ [ 'Config file must export a function!' ] )
175
+ expect ( mocks . process . exit ) . not . to . have . been . called
176
+ } )
177
+
136
178
it ( 'should override config with given cli options' , ( ) => {
137
179
const config = e . parseConfig ( '/home/config4.js' , { port : 456 , autoWatch : false } )
138
180
0 commit comments