Abolfazl Panbehkar
Abolfazl Panbehkar

Reputation: 708

How to unit testing with jest in vue composition api component?

I'm writing a unit test with jest, for my composition API component in vue.js.

But I can't access to functions in composition API's setup().

Indicator.vue

<template>
  <div class="d-flex flex-column justify-content-center align-content-center">
    <ul class="indicator-menu d-flex justify-content-center">
      <li v-for="step in steps" :key="step">
        <a href="#" @click="updateValue(step)" :class="activeClass(step, current)"> </a>
      </li>
    </ul>
    <div class="indicator-caption d-flex justify-content-center">
      step
      <span> {{ current }}</span>
      from
      <span> {{ steps }}</span>
    </div>
  </div>
</template>

<script lang="ts">
import {createComponent} from '@vue/composition-api';

export default createComponent({
  name: 'Indicator',
  props: {
    steps: {
      type: Number,
      required: true
    },
    current: {
      type: Number,
      required: true
    }
  },
  setup(props, context) {
    const updateValue = (step: number) => {
      context.emit('clicked', step);
    };
    const activeClass = (step: number, current: number) =>
      step < current ? 'passed' : step === current ? 'current' : '';
    return {
      updateValue,
      activeClass
    };
  }
});
</script>

<style></style>

Indicator.test.ts

import Indicator from '@/views/components/Indicator.vue';
import { shallowMount } from '@vue/test-utils';

describe('@/views/components/Indicator.vue', () => {  
  let wrapper: any;
  beforeEach(() => {
    wrapper = shallowMount(Indicator, {
      propsData: {
        steps: 4,
        current: 2
      }
    });
  });
  it('should return "current" for values (2,2)', () => {
    expect(wrapper.vm.activeClass(2, 2)).toBe('current');
  });
});

And I got this Error, in running test command:

TypeError: Cannot read property 'vm' of undefined

Upvotes: 15

Views: 10426

Answers (3)

Andrej Gaspar
Andrej Gaspar

Reputation: 349

I would also suggest to use jest.config.js file to initialize it by default:

setupFiles: ['<rootDir>/tests/helpers/foo.js']

then in your tests/ folder create helpers/ with file foo.js

and in file:

import Vue from 'vue'
import CompositionApi from '@vue/composition-api'
Vue.use(CompositionApi)

by this way it's available for each test file :)

Upvotes: 1

Mohammad
Mohammad

Reputation: 641

In nuxt.js version 2 @andrej-gaspar solution almost worked but it throws Cannot read property install of undefined error, so to fix that error do like below:

jest.config.js

setupFiles: ['<rootDir>/tests/helpers/foo.js']

foo.js

import Vue from 'vue'
import * as CompositionApi from '@vue/composition-api'
Vue.use(CompositionApi)

Or if you're using @nuxtjs/composition-api:

import Vue from 'vue'
import * as CompositionApi from '@nuxtjs/composition-api'
Vue.use(CompositionApi)

Upvotes: 0

Rob Axelsen
Rob Axelsen

Reputation: 1034

I think simply importing CompositionApi should solve your issue.

import CompositionApi from '@vue/composition-api'

Vue.use(CompositionApi)

Upvotes: 15

Related Questions