From a8b846469390dfe18b0f552159f935ad6c85aa9a Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Mon, 23 Nov 2020 13:18:09 +0100 Subject: [PATCH 1/2] feat: add warn --- src/apis/index.ts | 1 + src/apis/warn.ts | 12 ++++++++++++ src/env.d.ts | 2 +- src/utils/utils.ts | 2 +- test/apis/warn.spec.js | 25 +++++++++++++++++++++++++ 5 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 src/apis/warn.ts create mode 100644 test/apis/warn.spec.js diff --git a/src/apis/index.ts b/src/apis/index.ts index bf2e31e2..d1821026 100644 --- a/src/apis/index.ts +++ b/src/apis/index.ts @@ -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' diff --git a/src/apis/warn.ts b/src/apis/warn.ts new file mode 100644 index 00000000..2bb5236f --- /dev/null +++ b/src/apis/warn.ts @@ -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()) +} diff --git a/src/env.d.ts b/src/env.d.ts index 161e9a67..8e362480 100644 --- a/src/env.d.ts +++ b/src/env.d.ts @@ -19,7 +19,7 @@ declare module 'vue/types/vue' { interface VueConstructor { observable(x: any): T util: { - warn(msg: string, vm?: Vue) + warn(msg: string, vm?: Vue | null) defineReactive( obj: Object, key: string, diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 0bffd6d7..2c4f4fc1 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -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) } diff --git a/test/apis/warn.spec.js b/test/apis/warn.spec.js new file mode 100644 index 00000000..ab9401fb --- /dev/null +++ b/test/apis/warn.spec.js @@ -0,0 +1,25 @@ +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('should work', () => { + new Vue({ + setup() { + apiWarn('warned') + }, + template: `
`, + }).$mount() + + expect(warn).toHaveBeenCalled() + expect(warn.mock.calls[0][0]).toMatch( + /\[Vue warn\]: warned.?[\s\S]*\(found in \)/ + ) + }) +}) From eaf03df12d4973fad0401b85f5cb4e818d03ef92 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Mon, 23 Nov 2020 13:20:19 +0100 Subject: [PATCH 2/2] test: add test for warn --- test/apis/warn.spec.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/test/apis/warn.spec.js b/test/apis/warn.spec.js index ab9401fb..9ceb5fa9 100644 --- a/test/apis/warn.spec.js +++ b/test/apis/warn.spec.js @@ -9,7 +9,7 @@ describe('api/warn', () => { warn.mockRestore() }) - it('should work', () => { + it('can be called inside a component', () => { new Vue({ setup() { apiWarn('warned') @@ -17,9 +17,16 @@ describe('api/warn', () => { template: `
`, }).$mount() - expect(warn).toHaveBeenCalled() + expect(warn).toHaveBeenCalledTimes(1) expect(warn.mock.calls[0][0]).toMatch( - /\[Vue warn\]: warned.?[\s\S]*\(found in \)/ + /\[Vue warn\]: warned[\s\S]*\(found in \)/ ) }) + + it('can be called outside a component', () => { + apiWarn('warned') + + expect(warn).toHaveBeenCalledTimes(1) + expect(warn).toHaveBeenCalledWith('[Vue warn]: warned') + }) })