@@ -51,3 +51,54 @@ import PluginError = require("plugin-error");
51
51
}
52
52
}
53
53
54
+ describe ( 'PluginError()' , function ( ) {
55
+ it ( 'support error type define with "Error | string"' , ( done ) => {
56
+ const PLUGIN_NAME = 'test' ;
57
+ function createPluginError ( err : Error | string ) {
58
+ return new PluginError ( PLUGIN_NAME , err ) ;
59
+ }
60
+ expect ( createPluginError ( 'something broke' ) . message ) . to . equal ( 'something broke' ) ;
61
+ expect ( createPluginError ( new Error ( 'something broke' ) ) . message ) . to . equal ( 'something broke' ) ;
62
+ done ( ) ;
63
+ } ) ;
64
+
65
+ it ( 'Inference with union type on second parameter' , ( done ) => {
66
+ const PLUGIN_NAME = 'test' ;
67
+
68
+ interface IFooError extends Error {
69
+ foo : number ;
70
+ }
71
+
72
+ function createPluginError ( err : IFooError | string ) {
73
+ return new PluginError ( PLUGIN_NAME , err ) ;
74
+ }
75
+
76
+ const fooError : IFooError = Object . assign ( new Error ( 'something broke' ) , { foo : 1 } ) ;
77
+ const pluginError = createPluginError ( fooError ) ;
78
+ const foo : number = pluginError . foo ;
79
+ expect ( foo ) . to . be ( 1 ) ;
80
+ done ( ) ;
81
+ } ) ;
82
+
83
+ it ( 'Inference with union type on second parameter and dependent properties' , ( done ) => {
84
+ // Inference with union type on second parameter and dependent properties
85
+ const PLUGIN_NAME = 'test' ;
86
+
87
+ interface IFooBarError extends Error {
88
+ foo : number ;
89
+ bar : number ;
90
+ }
91
+
92
+ function createPluginError ( err : IFooBarError | string ) {
93
+ return new PluginError ( PLUGIN_NAME , err ) ;
94
+ }
95
+
96
+ const fooError : IFooBarError = Object . assign ( new Error ( 'something broke' ) , { foo : 1 , bar : 2 } ) ;
97
+ const pluginError = createPluginError ( fooError ) ;
98
+ const foo : number = pluginError . foo ;
99
+ const bar : number = pluginError . bar ;
100
+ expect ( foo ) . to . be ( 1 ) ;
101
+ expect ( bar ) . to . be ( 2 ) ;
102
+ done ( ) ;
103
+ } ) ;
104
+ } ) ;
0 commit comments