@@ -15,32 +15,32 @@ it('throws an exception if the predicate is falsy', () => {
15
15
} ) . toThrow ( new InvariantError ( 'Error message' ) )
16
16
17
17
expect ( ( ) => invariant ( null , 'Error message' ) ) . toThrow (
18
- new InvariantError ( 'Error message' )
18
+ new InvariantError ( 'Error message' ) ,
19
19
)
20
20
expect ( ( ) => invariant ( undefined , 'Error message' ) ) . toThrow (
21
- new InvariantError ( 'Error message' )
21
+ new InvariantError ( 'Error message' ) ,
22
22
)
23
23
} )
24
24
25
25
it ( 'supports positional values in the error message' , ( ) => {
26
26
// Strings.
27
27
expect ( ( ) => invariant ( false , 'Cannot %s the %s' , 'fetch' , 'user' ) ) . toThrow (
28
- new InvariantError ( 'Cannot fetch the user' )
28
+ new InvariantError ( 'Cannot fetch the user' ) ,
29
29
)
30
30
31
31
// Numbers.
32
32
expect ( ( ) => invariant ( false , 'Expected %d apples' , 3 ) ) . toThrow (
33
- new InvariantError ( 'Expected 3 apples' )
33
+ new InvariantError ( 'Expected 3 apples' ) ,
34
34
)
35
35
36
36
// Booleans.
37
37
expect ( ( ) => invariant ( false , 'Expected %s but got %s' , true , false ) ) . toThrow (
38
- new InvariantError ( 'Expected true but got false' )
38
+ new InvariantError ( 'Expected true but got false' ) ,
39
39
)
40
40
41
41
// Objects.
42
42
expect ( ( ) =>
43
- invariant ( false , 'Cannot create user: %o' , { name : 'John' } )
43
+ invariant ( false , 'Cannot create user: %o' , { name : 'John' } ) ,
44
44
) . toThrow ( new InvariantError ( `Cannot create user: {"name":"John"}` ) )
45
45
} )
46
46
@@ -69,7 +69,7 @@ it('supports polymorphic error class with additional arguments', () => {
69
69
class NetworkError extends Error {
70
70
constructor (
71
71
public readonly errorCode : number ,
72
- public readonly message : string
72
+ public readonly message : string ,
73
73
) {
74
74
super ( message )
75
75
Object . setPrototypeOf ( this , NetworkError . prototype )
@@ -81,11 +81,28 @@ it('supports polymorphic error class with additional arguments', () => {
81
81
( message ) => new NetworkError ( 230 , message ) ,
82
82
false ,
83
83
'Failed to handle %s' ,
84
- 'http://localhost:3000'
84
+ 'http://localhost:3000' ,
85
85
)
86
86
} catch ( error ) {
87
87
expect ( error ) . toBeInstanceOf ( NetworkError )
88
88
expect ( error ) . toBeInstanceOf ( Error )
89
89
expect ( error . message ) . toEqual ( 'Failed to handle http://localhost:3000' )
90
90
}
91
91
} )
92
+
93
+ it ( 'supports polymorphic error class with multiple positionals' , ( ) => {
94
+ class MyError extends Error {
95
+ constructor ( message ?: string ) {
96
+ super ( message )
97
+ Object . setPrototypeOf ( this , MyError . prototype )
98
+ }
99
+ }
100
+
101
+ try {
102
+ invariant . as ( MyError , false , 'Cannot %s the %s' , 'fetch' , 'user' )
103
+ } catch ( error ) {
104
+ expect ( error ) . toBeInstanceOf ( MyError )
105
+ expect ( error ) . toBeInstanceOf ( Error )
106
+ expect ( error . message ) . toBe ( 'Cannot fetch the user' )
107
+ }
108
+ } )
0 commit comments