How can i test custom hooks like this? #1229
Answered
by
cexbrayat
liujiapeng
asked this question in
Q&A
-
I read some tutorials for testing the composition api, but it looks like they all use the html output to test correctness. Sometimes I use custom hooks to write some logic, but I don't know how to test it, is there a demo for it? import { ref } from 'vue'
const useService = () => {
const count = ref(0)
const addCount = () => count.value++
return {
count,
addCount
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
cexbrayat
Jan 10, 2022
Replies: 1 comment 3 replies
-
Hi @liujiapeng You don't need Vue Test Utils to test this: it's a simple function test. I would write something like this (I did not test it): test('addCount should increment count', () => {
const service = useService();
expect(service.count.value).toBe(0);
service.addCoun();
expect(service.count.value).toBe(1);
}); |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
cexbrayat
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @liujiapeng
You don't need Vue Test Utils to test this: it's a simple function test.
I would write something like this (I did not test it):