@@ -3,39 +3,65 @@ import { expect } from 'chai';
3
3
import { createError , formatError } from '../dist' ;
4
4
5
5
describe ( 'createError' , ( ) => {
6
- it ( 'returns an error that serializes properly' , ( ) => {
7
- const FooError = createError ( 'FooError' , {
8
- message : 'A foo error has occurred' ,
9
- data : {
10
- hello : 'world'
11
- } ,
12
- options : {
13
- showLocations : false ,
14
- showPath : true ,
15
- } ,
16
- } ) ;
6
+ context ( 'when properly used' , ( ) => {
7
+ it ( 'returns an error that serializes properly' , ( ) => {
8
+ const FooError = createError ( 'FooError' , {
9
+ message : 'A foo error has occurred' ,
10
+ data : {
11
+ hello : 'world'
12
+ } ,
13
+ options : {
14
+ showLocations : false ,
15
+ showPath : true ,
16
+ } ,
17
+ } ) ;
17
18
18
- const iso = new Date ( ) . toISOString ( ) ;
19
+ const iso = new Date ( ) . toISOString ( ) ;
19
20
20
- const e = new FooError ( {
21
- message : 'A foo 2.0 error has occurred' ,
22
- data : {
23
- foo : 'bar'
24
- } ,
25
- options : {
26
- showLocations : true ,
27
- showPath : false ,
28
- } ,
29
- } ) ;
21
+ const e = new FooError ( {
22
+ message : 'A foo 2.0 error has occurred' ,
23
+ data : {
24
+ foo : 'bar'
25
+ } ,
26
+ options : {
27
+ showLocations : true ,
28
+ showPath : false ,
29
+ } ,
30
+ } ) ;
30
31
31
- const { message, name, time_thrown, data } = e . serialize ( ) ;
32
+ const { message, name, time_thrown, data } = e . serialize ( ) ;
32
33
33
- expect ( message ) . to . equal ( 'A foo 2.0 error has occurred' ) ;
34
- expect ( name ) . to . equal ( 'FooError' ) ;
35
- expect ( time_thrown ) . to . equal ( e . time_thrown ) ;
36
- expect ( data ) . to . eql ( {
37
- hello : 'world' ,
38
- foo : 'bar'
34
+ expect ( message ) . to . equal ( 'A foo 2.0 error has occurred' ) ;
35
+ expect ( name ) . to . equal ( 'FooError' ) ;
36
+ expect ( time_thrown ) . to . equal ( e . time_thrown ) ;
37
+ expect ( data ) . to . eql ( {
38
+ hello : 'world' ,
39
+ foo : 'bar'
40
+ } ) ;
41
+ } ) ;
42
+ } ) ;
43
+ context ( 'when missing a config as the second parameter' , ( ) => {
44
+ it ( 'throws an assertion error with a useful message' , ( ) => {
45
+ try {
46
+ createError ( 'FooError' ) ;
47
+ throw new Error ( 'did not throw as expected' ) ;
48
+ } catch ( err ) {
49
+ expect ( err . name ) . to . equal ( 'AssertionError [ERR_ASSERTION]' ) ;
50
+ expect ( err . message ) . to . equal ( 'createError requires a config object as the second parameter' ) ;
51
+ }
52
+ } ) ;
53
+ } ) ;
54
+ context ( 'when missing a message from the config object passed as the second parameter' , ( ) => {
55
+ it ( 'throws an assertion error with a useful message' , ( ) => {
56
+ try {
57
+ createError ( 'FooError' , {
58
+
59
+ } ) ;
60
+ throw new Error ( 'did not throw as expected' ) ;
61
+ } catch ( err ) {
62
+ expect ( err . name ) . to . equal ( 'AssertionError [ERR_ASSERTION]' ) ;
63
+ expect ( err . message ) . to . equal ( 'createError requires a "message" property on the config object passed as the second parameter' )
64
+ }
39
65
} ) ;
40
66
} ) ;
41
67
} ) ;
0 commit comments