Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vue-test-utils + typescript type for wrapper.vm

sitting on a issue here. ever used typescript + vue-test-utils and tried to manipulate a value for the test like: wrapper.vm.aCoolRefValueToManipulate = 'something much cooler'?
well i tried. and it worked but ts linter goes crazy on this one because it don't know what aCoolRefValueToManipulate inside vm is.

anyone a idea how to fix this?

linter error

linter tells me:

Property 'showTopDown' does not exist on type '{ $: ComponentInternalInstance; $data: {}; $props: Partial<{}> & Omit<Readonly<ExtractPropTypes<{}>> & VNodeProps & AllowedComponentProps & ComponentCustomProps, never>; ... 10 more ...; $watch(source: string | Function, cb: Function, options?: WatchOptions<...> | undefined): WatchStopHandle; } & Readonly<...> & Sha...'.ts(2339)

Solution

some cool dude helped me on the official Vue Discord Server.

(wrapper.vm as any).aCoolRefValueToManipulate 
like image 336
Deniz Avatar asked Oct 28 '25 05:10

Deniz


2 Answers

Do we have any other ways without using "any" to access into methods of wrapper.vm?

I just found this thing that can try:

type TestWrapper<T> = VueWrapper<ComponentPublicInstance & T>
let wrapper: TestWrapper<Partial<{ myMethod: () => void }>>

wrapper.vm.myMethod?.()
like image 106
Kevin Avatar answered Oct 29 '25 18:10

Kevin


How about defineExpose - this will make the methods public for test purposes.

defineExpose({ showTopDown })

therefore in your test:

wrapper.vm.showTopDown // will be boolean

https://vuejs.org/api/sfc-script-setup#defineexpose

like image 44
Simon Avatar answered Oct 29 '25 18:10

Simon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!