@@ -176,6 +176,7 @@ beforeEach(() => {
176
176
eslintMock . mock . executeOnText . mockClear ( )
177
177
eslintMock . mock . getConfigForFile . mockClear ( )
178
178
prettierMock . format . mockClear ( )
179
+ prettierMock . resolveConfig . mockClear ( )
179
180
fsMock . readFileSync . mockClear ( )
180
181
loglevelMock . mock . clearAll ( )
181
182
global . __PRETTIER_ESLINT_TEST_STATE__ = { }
@@ -186,93 +187,89 @@ tests.forEach(({title, modifier, input, output}) => {
186
187
if ( modifier ) {
187
188
fn = test [ modifier ]
188
189
}
189
- fn ( title , ( ) => {
190
+ fn ( title , async ( ) => {
190
191
input . text = stripIndent ( input . text ) . trim ( )
191
192
const expected = stripIndent ( output ) . trim ( )
192
- const actual = format ( input )
193
+ const actual = await format ( input )
193
194
// adding the newline in the expected because
194
195
// prettier adds a newline to the end of the input
195
196
expect ( actual ) . toBe ( `${ expected } \n` )
196
197
} )
197
198
} )
198
199
199
- test ( 'failure to fix with eslint throws and logs an error' , ( ) => {
200
+ test ( 'failure to fix with eslint throws and logs an error' , async ( ) => {
200
201
const { executeOnText} = eslintMock . mock
201
- const error = 'Something happened'
202
- executeOnText . throwError = new Error ( error )
203
- expect ( ( ) => format ( { text : '' } ) ) . toThrowError ( error )
204
- expect ( logger . error ) . toHaveBeenCalledTimes ( 1 )
202
+ const error = new Error ( 'Something happened' )
203
+ executeOnText . throwError = error
204
+ const errorThrown = await format ( { text : '' } ) . catch ( e => e )
205
205
executeOnText . throwError = null
206
+
207
+ expect ( errorThrown ) . toBe ( error )
208
+ expect ( logger . error ) . toHaveBeenCalledTimes ( 1 )
206
209
} )
207
210
208
- test ( 'logLevel is used to configure the logger' , ( ) => {
211
+ test ( 'logLevel is used to configure the logger' , async ( ) => {
209
212
logger . setLevel = jest . fn ( )
210
- format ( { text : '' , logLevel : 'silent' } )
213
+ await format ( { text : '' , logLevel : 'silent' } )
211
214
expect ( logger . setLevel ) . toHaveBeenCalledTimes ( 1 )
212
215
expect ( logger . setLevel ) . toHaveBeenCalledWith ( 'silent' )
213
216
} )
214
217
215
- test ( `when prettier throws, log to logger.error and throw the error` , ( ) => {
216
- const { format : prettierMockFormat } = prettierMock
217
- const error = 'something bad happened'
218
- prettierMockFormat . throwError = new Error ( error )
218
+ test ( `when prettier throws, log to logger.error and throw the error` , async ( ) => {
219
+ const error = new Error ( 'something bad happened' )
220
+ prettierMock . format . throwError = error
221
+ const errorThrown = await format ( { text : '' } ) . catch ( e => e )
222
+ prettierMock . format . throwError = null
219
223
220
- expect ( ( ) => format ( { text : '' } ) ) . toThrowError ( error )
224
+ expect ( errorThrown ) . toBe ( error )
221
225
expect ( logger . error ) . toHaveBeenCalledTimes ( 1 )
222
-
223
- prettierMockFormat . throwError = null
224
226
} )
225
227
226
- test ( 'can accept a path to an eslint module and uses that instead.' , ( ) => {
228
+ test ( 'can accept a path to an eslint module and uses that instead.' , async ( ) => {
227
229
const eslintPath = path . join ( __dirname , '../__mocks__/eslint' )
228
- const { executeOnText} = eslintMock . mock
229
- format ( { text : '' , eslintPath} )
230
- expect ( executeOnText ) . toHaveBeenCalledTimes ( 1 )
230
+ await format ( { text : '' , eslintPath} )
231
+ expect ( eslintMock . mock . executeOnText ) . toHaveBeenCalledTimes ( 1 )
231
232
} )
232
233
233
- test ( 'fails with an error if the eslint module cannot be resolved.' , ( ) => {
234
+ test ( 'fails with an error if the eslint module cannot be resolved.' , async ( ) => {
234
235
const eslintPath = path . join (
235
236
__dirname ,
236
237
'../__mocks__/non-existant-eslint-module' ,
237
238
)
238
239
239
- expect ( ( ) => format ( { text : '' , eslintPath} ) ) . toThrowError (
240
- / n o n - e x i s t a n t - e s l i n t - m o d u l e / ,
241
- )
240
+ const error = await format ( { text : '' , eslintPath} ) . catch ( e => e )
241
+
242
+ expect ( error . message ) . toMatch ( / n o n - e x i s t a n t - e s l i n t - m o d u l e / )
242
243
expect ( logger . error ) . toHaveBeenCalledTimes ( 1 )
243
244
244
245
const errorString = expect . stringMatching (
245
- / E S L i n t .* ?e s l i n t P a t h .* n o n - e x i s t a n t - e s l i n t - m o d u l e / ,
246
+ / t r o u b l e g e t t i n g .* ?e s l i n t .* n o n - e x i s t a n t - e s l i n t - m o d u l e / ,
246
247
)
247
248
248
249
expect ( logger . error ) . toHaveBeenCalledWith ( errorString )
249
250
} )
250
251
251
- test ( 'can accept a path to a prettier module and uses that instead.' , ( ) => {
252
+ test ( 'can accept a path to a prettier module and uses that instead.' , async ( ) => {
252
253
const prettierPath = path . join ( __dirname , '../__mocks__/prettier' )
253
- const { format : prettierMockFormat } = prettierMock
254
- format ( { text : '' , prettierPath} )
255
- expect ( prettierMockFormat ) . toHaveBeenCalledTimes ( 1 )
254
+ await format ( { text : '' , prettierPath} )
255
+ expect ( prettierMock . format ) . toHaveBeenCalledTimes ( 1 )
256
256
} )
257
257
258
- test ( 'fails with an error if the prettier module cannot be resolved.' , ( ) => {
258
+ test ( 'fails with an error if the prettier module cannot be resolved.' , async ( ) => {
259
259
const prettierPath = path . join (
260
260
__dirname ,
261
261
'../__mocks__/non-existant-prettier-module' ,
262
262
)
263
- expect ( ( ) => format ( { text : '' , prettierPath} ) ) . toThrowError (
264
- / n o n - e x i s t a n t - p r e t t i e r - m o d u l e / ,
265
- )
263
+ const error = await format ( { text : '' , prettierPath} ) . catch ( e => e )
264
+ expect ( error . message ) . toMatch ( / n o n - e x i s t a n t - p r e t t i e r - m o d u l e / )
266
265
expect ( logger . error ) . toHaveBeenCalledTimes ( 1 )
267
- const errorString = expect . stringMatching (
268
- / p r e t t i e r .* ?p r e t t i e r P a t h .* n o n - e x i s t a n t - p r e t t i e r - m o d u l e / ,
269
- )
266
+ const errorString = expect . stringMatching ( / t r o u b l e g e t t i n g .* p r e t t i e r / )
270
267
expect ( logger . error ) . toHaveBeenCalledWith ( errorString )
271
268
} )
272
269
273
- test ( 'resolves to the eslint module relative to the given filePath' , ( ) => {
270
+ test ( 'resolves to the eslint module relative to the given filePath' , async ( ) => {
274
271
const filePath = require . resolve ( '../../tests/fixtures/paths/foo.js' )
275
- format ( { text : '' , filePath} )
272
+ await format ( { text : '' , filePath} )
276
273
const stateObj = {
277
274
eslintPath : require . resolve (
278
275
'../../tests/fixtures/paths/node_modules/eslint/index.js' ,
@@ -284,9 +281,9 @@ test('resolves to the eslint module relative to the given filePath', () => {
284
281
expect ( global . __PRETTIER_ESLINT_TEST_STATE__ ) . toMatchObject ( stateObj )
285
282
} )
286
283
287
- test ( 'resolves to the local eslint module' , ( ) => {
284
+ test ( 'resolves to the local eslint module' , async ( ) => {
288
285
const filePath = '/blah-blah/default-config'
289
- format ( { text : '' , filePath} )
286
+ await format ( { text : '' , filePath} )
290
287
expect ( global . __PRETTIER_ESLINT_TEST_STATE__ ) . toMatchObject ( {
291
288
// without Jest's mocking, these would actually resolve to the
292
289
// project modules :) The fact that jest's mocking is being
@@ -296,28 +293,47 @@ test('resolves to the local eslint module', () => {
296
293
} )
297
294
} )
298
295
299
- test ( 'reads text from fs if filePath is provided but not text' , ( ) => {
296
+ test ( 'reads text from fs if filePath is provided but not text' , async ( ) => {
300
297
const filePath = '/blah-blah/some-file.js'
301
- try {
302
- format ( { filePath} )
303
- } catch ( e ) {
304
- // ignore
305
- }
298
+ await format ( { filePath} ) . catch ( ( ) => { } )
306
299
// one hit to get the file and one for the eslintignore
307
300
expect ( fsMock . readFileSync ) . toHaveBeenCalledTimes ( 2 )
308
301
expect ( fsMock . readFileSync ) . toHaveBeenCalledWith ( filePath , 'utf8' )
309
302
} )
310
303
311
- test ( 'logs error if it cannot read the file from the filePath' , ( ) => {
304
+ test ( 'logs error if it cannot read the file from the filePath' , async ( ) => {
312
305
const originalMock = fsMock . readFileSync
313
306
fsMock . readFileSync = jest . fn ( ( ) => {
314
307
throw new Error ( 'some error' )
315
308
} )
316
- expect ( ( ) => format ( { filePath : '/some-path.js' } ) ) . toThrowError ( / s o m e e r r o r / )
309
+ const error = await format ( { filePath : '/some-path.js' } ) . catch ( e => e )
310
+ expect ( error . message ) . toMatch ( / s o m e e r r o r / )
317
311
expect ( logger . error ) . toHaveBeenCalledTimes ( 1 )
318
312
fsMock . readFileSync = originalMock
319
313
} )
320
314
315
+ test ( 'calls prettier.resolveConfig with the file path' , async ( ) => {
316
+ const filePath = require . resolve ( '../../tests/fixtures/paths/foo.js' )
317
+ await format ( {
318
+ filePath,
319
+ text : defaultInputText ( ) ,
320
+ eslintConfig : getESLintConfigWithDefaultRules ( ) ,
321
+ } )
322
+ expect ( prettierMock . resolveConfig ) . toHaveBeenCalledTimes ( 1 )
323
+ expect ( prettierMock . resolveConfig ) . toHaveBeenCalledWith ( filePath )
324
+ } )
325
+
326
+ test ( 'logs if there is a problem making the CLIEngine' , async ( ) => {
327
+ const error = new Error ( 'fake error' )
328
+ eslintMock . CLIEngine . mockImplementation ( ( ) => {
329
+ throw error
330
+ } )
331
+ const errorThrown = await format ( { text : '' } ) . catch ( e => e )
332
+ eslintMock . CLIEngine . mockReset ( )
333
+ expect ( errorThrown ) . toBe ( error )
334
+ expect ( logger . error ) . toHaveBeenCalledTimes ( 1 )
335
+ } )
336
+
321
337
function getESLintConfigWithDefaultRules ( overrides ) {
322
338
return {
323
339
parserOptions : { ecmaVersion : 7 } ,
0 commit comments