I use Vue 2 and @vue/composition-api
plugin.
I created a Jest test, but the test failed with errors:
Test suite failed to run
[vue-composition-api] must call Vue.use(VueCompositionAPI) before using any function.
1 | import { ref } from '@vue/composition-api';
2 |
> 3 | const showSidebar = ref(false);
| ^
4 | export const breakPointSize = 1250;
5 | export const classLink = 'SidebarSectionItemRow';
6 | export const idBtnNavbar = 'sidebarBtnNavbar';
test.spec.ts
import VueCompositionApi from '@vue/composition-api'
import { createLocalVue, mount } from '@vue/test-utils';
import MainMenuContent from '@/components/layouts/main/sidebar/menu/MainMenuContent.vue';
// create an extended `Vue` constructor
const localVue = createLocalVue()
// install plugins as normal
localVue.use(VueCompositionApi)
describe('MainMenuContent', () => {
it('expect AdminSection ', () => {
const wrapper = mount(MainMenuContent, {
localVue,
});
....
});
});
I think the errors are caused by ref
being outside a setup()
function.
sidebarControl.ts
import { ref } from '@vue/composition-api';
const showSidebar = ref(false);
export function useControlSidebar() {
const toggleSidebar = () => {
showSidebar.value = !showSidebar.value;
};
return {
showSidebar,
toggleSidebar,
};
}
Is it possible to solve this somehow?