File tree Expand file tree Collapse file tree 5 files changed +47
-2
lines changed Expand file tree Collapse file tree 5 files changed +47
-2
lines changed Original file line number Diff line number Diff line change @@ -30,3 +30,4 @@ export { useCSSModule } from './useCssModule'
30
30
export { createApp } from './createApp'
31
31
export { nextTick } from './nextTick'
32
32
export { createElement as h } from './createElement'
33
+ export { warn } from './warn'
Original file line number Diff line number Diff line change
1
+ import { getCurrentInstance } from '../runtimeContext'
2
+ import { warn as vueWarn } from '../utils'
3
+
4
+ /**
5
+ * Displays a warning message (using console.error) with a stack trace if the
6
+ * function is called inside of active component.
7
+ *
8
+ * @param message warning message to be displayed
9
+ */
10
+ export function warn ( message : string ) {
11
+ vueWarn ( message , getCurrentInstance ( ) )
12
+ }
Original file line number Diff line number Diff line change @@ -19,7 +19,7 @@ declare module 'vue/types/vue' {
19
19
interface VueConstructor {
20
20
observable < T > ( x : any ) : T
21
21
util : {
22
- warn ( msg : string , vm ?: Vue )
22
+ warn ( msg : string , vm ?: Vue | null )
23
23
defineReactive (
24
24
obj : Object ,
25
25
key : string ,
Original file line number Diff line number Diff line change @@ -83,7 +83,7 @@ export function isUndef(v: any): boolean {
83
83
return v === undefined || v === null
84
84
}
85
85
86
- export function warn ( msg : string , vm ?: Vue ) {
86
+ export function warn ( msg : string , vm ?: Vue | null ) {
87
87
Vue . util . warn ( msg , vm )
88
88
}
89
89
Original file line number Diff line number Diff line change
1
+ const Vue = require ( 'vue/dist/vue.common.js' )
2
+ const { warn : apiWarn } = require ( '../../src' )
3
+
4
+ describe ( 'api/warn' , ( ) => {
5
+ beforeEach ( ( ) => {
6
+ warn = jest . spyOn ( global . console , 'error' ) . mockImplementation ( ( ) => null )
7
+ } )
8
+ afterEach ( ( ) => {
9
+ warn . mockRestore ( )
10
+ } )
11
+
12
+ it ( 'can be called inside a component' , ( ) => {
13
+ new Vue ( {
14
+ setup ( ) {
15
+ apiWarn ( 'warned' )
16
+ } ,
17
+ template : `<div></div>` ,
18
+ } ) . $mount ( )
19
+
20
+ expect ( warn ) . toHaveBeenCalledTimes ( 1 )
21
+ expect ( warn . mock . calls [ 0 ] [ 0 ] ) . toMatch (
22
+ / \[ V u e w a r n \] : w a r n e d [ \s \S ] * \( f o u n d i n < R o o t > \) /
23
+ )
24
+ } )
25
+
26
+ it ( 'can be called outside a component' , ( ) => {
27
+ apiWarn ( 'warned' )
28
+
29
+ expect ( warn ) . toHaveBeenCalledTimes ( 1 )
30
+ expect ( warn ) . toHaveBeenCalledWith ( '[Vue warn]: warned' )
31
+ } )
32
+ } )
You can’t perform that action at this time.
0 commit comments