Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add warn #596

Merged
merged 2 commits into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/apis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ export { useCSSModule } from './useCssModule'
export { createApp } from './createApp'
export { nextTick } from './nextTick'
export { createElement as h } from './createElement'
export { warn } from './warn'
12 changes: 12 additions & 0 deletions src/apis/warn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { getCurrentInstance } from '../runtimeContext'
import { warn as vueWarn } from '../utils'

/**
* Displays a warning message (using console.error) with a stack trace if the
* function is called inside of active component.
*
* @param message warning message to be displayed
*/
export function warn(message: string) {
vueWarn(message, getCurrentInstance())
}
2 changes: 1 addition & 1 deletion src/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ declare module 'vue/types/vue' {
interface VueConstructor {
observable<T>(x: any): T
util: {
warn(msg: string, vm?: Vue)
warn(msg: string, vm?: Vue | null)
defineReactive(
obj: Object,
key: string,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export function isUndef(v: any): boolean {
return v === undefined || v === null
}

export function warn(msg: string, vm?: Vue) {
export function warn(msg: string, vm?: Vue | null) {
Vue.util.warn(msg, vm)
}

Expand Down
32 changes: 32 additions & 0 deletions test/apis/warn.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const Vue = require('vue/dist/vue.common.js')
const { warn: apiWarn } = require('../../src')

describe('api/warn', () => {
beforeEach(() => {
warn = jest.spyOn(global.console, 'error').mockImplementation(() => null)
})
afterEach(() => {
warn.mockRestore()
})

it('can be called inside a component', () => {
new Vue({
setup() {
apiWarn('warned')
},
template: `<div></div>`,
}).$mount()

expect(warn).toHaveBeenCalledTimes(1)
expect(warn.mock.calls[0][0]).toMatch(
/\[Vue warn\]: warned[\s\S]*\(found in <Root>\)/
)
})

it('can be called outside a component', () => {
apiWarn('warned')

expect(warn).toHaveBeenCalledTimes(1)
expect(warn).toHaveBeenCalledWith('[Vue warn]: warned')
})
})