File tree 5 files changed +54
-5
lines changed
5 files changed +54
-5
lines changed Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ export type Config = {
16
16
performance: boolean ;
17
17
devtools: boolean ;
18
18
errorHandler: ?( err : Error , vm : Component , info : string ) => void ;
19
+ warnHandler: ?( msg : string , vm : Component , trace : string ) => void ;
19
20
ignoredElements: Array < string > ;
20
21
keyCodes: { [ key : string ] : number | Array < number > } ;
21
22
@@ -62,6 +63,11 @@ export default ({
62
63
*/
63
64
errorHandler : null ,
64
65
66
+ /**
67
+ * Warn handler for watcher warns
68
+ */
69
+ warnHandler : null ,
70
+
65
71
/**
66
72
* Ignore certain custom elements
67
73
*/
Original file line number Diff line number Diff line change @@ -15,10 +15,12 @@ if (process.env.NODE_ENV !== 'production') {
15
15
. replace ( / [ - _ ] / g, '' )
16
16
17
17
warn = ( msg , vm ) => {
18
- if ( hasConsole && ( ! config . silent ) ) {
19
- console . error ( `[Vue warn]: ${ msg } ` + (
20
- vm ? generateComponentTrace ( vm ) : ''
21
- ) )
18
+ const trace = vm ? generateComponentTrace ( vm ) : ''
19
+
20
+ if ( config . warnHandler ) {
21
+ config . warnHandler . call ( null , msg , vm , trace )
22
+ } else if ( hasConsole && ( ! config . silent ) ) {
23
+ console . error ( `[Vue warn]: ${ msg } ${ trace } ` )
22
24
}
23
25
}
24
26
Original file line number Diff line number Diff line change 1
1
import Vue from 'vue'
2
- import { formatComponentName } from 'core/util/debug'
2
+ import { formatComponentName , warn } from 'core/util/debug'
3
3
4
4
describe ( 'Debug utilities' , ( ) => {
5
5
it ( 'properly format component names' , ( ) => {
@@ -80,4 +80,38 @@ found in
80
80
<Root>`
81
81
) . toHaveBeenWarned ( )
82
82
} )
83
+
84
+ describe ( 'warn' , ( ) => {
85
+ const msg = 'message'
86
+ const vm = new Vue ( )
87
+
88
+ it ( 'calls warnHandler if warnHandler is set' , ( ) => {
89
+ Vue . config . warnHandler = jasmine . createSpy ( )
90
+
91
+ warn ( msg , vm )
92
+
93
+ expect ( Vue . config . warnHandler ) . toHaveBeenCalledWith ( msg , vm , jasmine . any ( String ) )
94
+
95
+ Vue . config . warnHandler = null
96
+ } )
97
+
98
+ it ( 'calls console.error if silent is false' , ( ) => {
99
+ Vue . config . silent = false
100
+
101
+ warn ( msg , vm )
102
+
103
+ expect ( msg ) . toHaveBeenWarned ( )
104
+ expect ( console . error ) . toHaveBeenCalled ( )
105
+ } )
106
+
107
+ it ( 'does not call console.error if silent is true' , ( ) => {
108
+ Vue . config . silent = true
109
+
110
+ warn ( msg , vm )
111
+
112
+ expect ( console . error ) . not . toHaveBeenCalled ( )
113
+
114
+ Vue . config . silent = false
115
+ } )
116
+ } )
83
117
} )
Original file line number Diff line number Diff line change @@ -63,6 +63,12 @@ class Test extends Vue {
63
63
vm . testMethods ( ) ;
64
64
}
65
65
} ;
66
+ config . warnHandler = ( msg , vm ) => {
67
+ if ( vm instanceof Test ) {
68
+ vm . testProperties ( ) ;
69
+ vm . testMethods ( ) ;
70
+ }
71
+ } ;
66
72
config . keyCodes = { esc : 27 } ;
67
73
}
68
74
Original file line number Diff line number Diff line change @@ -75,6 +75,7 @@ export declare class Vue {
75
75
productionTip : boolean ;
76
76
performance : boolean ;
77
77
errorHandler ( err : Error , vm : Vue , info : string ) : void ;
78
+ warnHandler ( msg : string , vm : Vue , trace : string ) : void ;
78
79
ignoredElements : string [ ] ;
79
80
keyCodes : { [ key : string ] : number } ;
80
81
}
You can’t perform that action at this time.
0 commit comments