Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
34 changes: 34 additions & 0 deletions packages/runtime-core/__tests__/debug.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {debug} from '../src/debug'
import {
h,
render,
defineComponent,
ref, getCurrentInstance, ComponentInternalInstance, nodeOps,
serializeInner as inner
} 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',
})
})
})
26 changes: 26 additions & 0 deletions packages/runtime-core/src/debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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))
Comment thread
bichikim marked this conversation as resolved.
Outdated
}
} : (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 @@ -261,6 +261,7 @@ export {
AsyncComponentLoader
} from './apiAsyncComponent'
export { HMRRuntime } from './hmr'
export { debug } from './debug'

// Internal API ----------------------------------------------------------------

Expand Down