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