Unit Test Vue Composition Functions

An Xie
Vue.js Developers
Published in
1 min readDec 14, 2020

--

At my current work, we are trying to migrate the vue2 project to use composition API. One question that came up from my co-worker is can we still mock the composition function easily like what we did for services classes. That is a very good question, but I don't see anything that could block us to mock it. First of all, the class is just syntax sugar for functions in JS. Secondly, the Vue composition function is just a regular JS factory function that returns a bunch of Vue specifically stuff like refs, etc.

From what I research on the internet/discord/github, I didn’t find any example to test a composition function. Even in some Vue composition functions library (my favorite VueUse for example) tests for functions are missing. Write your unit test guys!!!

TLDR, here is the code of how you do it:

Leave a comment if you got any questions on this.

--

--