Skip to content
Open
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
65 changes: 65 additions & 0 deletions packages/runtime-core/__tests__/debug.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { debug } from '../src/debug'
import {
type ComponentInternalInstance,
defineComponent,
getCurrentInstance,
h,
serializeInner as inner,
nodeOps,
ref,
render,
} from '@vue/runtime-test'

describe('debug', () => {
test('watching states', () => {
const root = nodeOps.createElement('div')
let instance: ComponentInternalInstance
const Comp = defineComponent({
setup() {
const name = ref('foo')
debug({
name,
})
return () => h('div', name.value)
},
mounted() {
instance = getCurrentInstance()!
},
})
render(h(Comp), root)
expect(inner(root)).toBe('<div>foo</div>')
expect(instance!.setupState).toEqual({
name: 'foo',
})
})
test('watching states with calling the debug function multiple times', () => {
const root = nodeOps.createElement('div')
let instance: ComponentInternalInstance
const Comp = defineComponent({
setup() {
const name = ref('foo')
const age = ref(100)
debug({
name,
})
debug({
age,
name,
})
debug({
name,
})
return () => h('div', name.value)
},
mounted() {
instance = getCurrentInstance()!
},
})
render(h(Comp), root)
expect(inner(root)).toBe('<div>foo</div>')
expect(instance!.setupState).toEqual({
name: 'foo',
age: 100,
})
})
})
30 changes: 30 additions & 0 deletions packages/runtime-core/src/debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { reactive } from '@vue/reactivity'
import { getCurrentInstance } from './component'

/**
* this debug function is a helper for watching states in the vue devtool (it runs only in dev mode)
* @example
* const Component = defineComponent({
* setup() {
* const name = ref('foo')
* debug({
* // watch states in the vue devtool
* name,
* })
* return h('div', name.value)
* },
* })
* @param states any states you want to see in the vue devtool
*/
export const debug = __DEV__
? (states: Record<string, any>) => {
const instance = getCurrentInstance()
if (instance) {
instance.setupState = reactive(
Object.assign({}, states, instance.setupState),
)
}
}
: (states: Record<string, any>) => {
// empty
}
1 change: 1 addition & 0 deletions packages/runtime-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ export type {
AsyncComponentOptions,
AsyncComponentLoader,
} from './apiAsyncComponent'
export { debug } from './debug'
export type {
HydrationStrategy,
HydrationStrategyFactory,
Expand Down