@@ -38,6 +38,17 @@ function disconnect(err) {
38
38
throw err || new Error ( 'Unknown disconnection' )
39
39
}
40
40
41
+ function shouldReject ( promise , errType = Error , message = 'Should have rejected' ) {
42
+ return promise . then (
43
+ ( ) => {
44
+ throw new Error ( message )
45
+ } ,
46
+ err => {
47
+ assert . instanceOf ( err , errType )
48
+ }
49
+ )
50
+ }
51
+
41
52
// All bindings are required to work with an "echo" firmware
42
53
// The echo firmware should respond with this data when it's
43
54
// ready to echo. This allows for remote device bootup.
@@ -156,22 +167,12 @@ function testBinding(bindingName, Binding, testPort) {
156
167
} )
157
168
} )
158
169
159
- it ( 'throws when not given a path' , done => {
160
- try {
161
- binding . open ( '' )
162
- } catch ( e ) {
163
- assert . instanceOf ( e , TypeError )
164
- done ( )
165
- }
170
+ it ( 'throws when not given a path' , async ( ) => {
171
+ await shouldReject ( binding . open ( '' ) , TypeError )
166
172
} )
167
173
168
- it ( 'throws when not given options' , done => {
169
- try {
170
- binding . open ( 'COMBAD' )
171
- } catch ( e ) {
172
- assert . instanceOf ( e , TypeError )
173
- done ( )
174
- }
174
+ it ( 'throws when not given options' , async ( ) => {
175
+ await shouldReject ( binding . open ( 'COMBAD' ) , TypeError )
175
176
} )
176
177
177
178
if ( ! testPort ) {
@@ -278,15 +279,9 @@ function testBinding(bindingName, Binding, testPort) {
278
279
} )
279
280
280
281
describe ( '#update' , ( ) => {
281
- it ( 'throws when not given an object' , done => {
282
+ it ( 'throws when not given an object' , async ( ) => {
282
283
const binding = new Binding ( { disconnect } )
283
-
284
- try {
285
- binding . update ( )
286
- } catch ( e ) {
287
- assert . instanceOf ( e , TypeError )
288
- done ( )
289
- }
284
+ await shouldReject ( binding . update ( ) , TypeError )
290
285
} )
291
286
292
287
it ( 'errors asynchronously when not open' , done => {
@@ -315,53 +310,47 @@ function testBinding(bindingName, Binding, testPort) {
315
310
316
311
afterEach ( ( ) => binding . close ( ) )
317
312
318
- it ( 'throws errors when updating nothing' , done => {
319
- try {
320
- binding . update ( { } )
321
- } catch ( err ) {
322
- assert . instanceOf ( err , Error )
323
- done ( )
324
- }
313
+ it ( 'throws errors when updating nothing' , async ( ) => {
314
+ await shouldReject ( binding . update ( { } ) , Error )
325
315
} )
326
316
327
- it ( 'errors when not called with options' , done => {
328
- try {
329
- binding . set ( ( ) => { } )
330
- } catch ( e ) {
331
- assert . instanceOf ( e , Error )
332
- done ( )
333
- }
317
+ it ( 'errors when not called with options' , async ( ) => {
318
+ await shouldReject ( binding . set ( ( ) => { } ) , Error )
334
319
} )
335
320
336
321
it ( 'updates baudRate' , ( ) => {
337
322
return binding . update ( { baudRate : 57600 } )
338
323
} )
339
324
} )
340
325
341
- describe ( '#write' , ( ) => {
326
+ describe . only ( '#write' , ( ) => {
342
327
it ( 'errors asynchronously when not open' , done => {
343
328
const binding = new Binding ( {
344
329
disconnect,
345
330
} )
346
331
let noZalgo = false
347
- binding . write ( Buffer . from ( [ ] ) ) . catch ( err => {
348
- assert . instanceOf ( err , Error )
349
- assert ( noZalgo )
350
- done ( )
351
- } )
332
+ binding
333
+ . write ( Buffer . from ( [ ] ) )
334
+ . then (
335
+ data => {
336
+ console . log ( { data } )
337
+ throw new Error ( 'Should have errored' )
338
+ } ,
339
+ err => {
340
+ assert . instanceOf ( err , Error )
341
+ assert ( noZalgo )
342
+ done ( )
343
+ }
344
+ )
345
+ . catch ( done )
352
346
noZalgo = true
353
347
} )
354
348
355
- it ( 'throws when not given a buffer' , done => {
349
+ it ( 'throws when not given a buffer' , async ( ) => {
356
350
const binding = new Binding ( {
357
351
disconnect,
358
352
} )
359
- try {
360
- binding . write ( null )
361
- } catch ( e ) {
362
- assert . instanceOf ( e , TypeError )
363
- done ( )
364
- }
353
+ await shouldReject ( binding . write ( null ) , TypeError )
365
354
} )
366
355
367
356
if ( ! testPort ) {
@@ -494,16 +483,11 @@ function testBinding(bindingName, Binding, testPort) {
494
483
noZalgo = true
495
484
} )
496
485
497
- it ( 'throws when not called with options' , done => {
486
+ it ( 'throws when not called with options' , async ( ) => {
498
487
const binding = new Binding ( {
499
488
disconnect,
500
489
} )
501
- try {
502
- binding . set ( ( ) => { } )
503
- } catch ( e ) {
504
- assert . instanceOf ( e , TypeError )
505
- done ( )
506
- }
490
+ await shouldReject ( binding . set ( ( ) => { } ) , TypeError )
507
491
} )
508
492
509
493
if ( ! testPort ) {
0 commit comments