Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I unit test a quasar app using Jest?

I have an quasar application that was generated with the quasar-cli.

How do I integrate a unit test into a test runner like Jest for an application like this?

I've added a this to my Jest configuration

"moduleNameMapper": {
    "quasar": "<rootDir>/node_modules/quasar-framework"
}

Unfortunately, Jest reports back

Cannot find module 'quasar' from 'index.vue'

Here is the a snippet of the Vue file

<template>
<div style="padding-top: 20px" v-if="refund.type != null ">
      <q-btn :label="'Issue ' + (  currency(refund.amount)) + ' Refund'" :disable="refund.amount <= 0" @click="issueRefund()" color="green" class="full-width" :loading="noteLoading" />
    </div>
</template>

<script>
import { Notify } from "quasar"; // here is where I am using Quasar
issueRefund() {
  this.noteLoading = true;
  this.$axios
    .post(`${BASE_URL}/issue_refund/?secret=${this.secret}`, {
      refund: this.refund,
      agent_email: this.userEmail,
      order_id: this.selectedOrder.id,
      agent_name: this.$route.query.user_name,
      order_number: this.selectedOrder.order_number,
      ticket_id: this.ticketId
    })
    .then(res => {
        this.noteLoading = false;
      if ((res.data.res === "success")) {
        Notify.create({
          position: "bottom",
          type: "positive",
          message: "Refund Issued."
        });
        this.selectedOrder = res.data.order;
        this.resetRefundObj();
        this.$refs.refundDiag.hide();
      } else {
        Notify.create({
          position: "bottom",
          type: "negative",
          message: res.data.error
        });
      }
    });
},
</script>
like image 626
Kim Merino Avatar asked Jan 28 '23 07:01

Kim Merino


1 Answers

Integrating Jest with Quasar is quite straight-forward. You'll need two packages, babel-jest and jest.

yarn add jest babel-jest -D

After adding those two dependencies, create a jest.config.js file at the root of your project--here's where all the jest configuration goes.

Here's how the jest.config.js file should look like;

module.exports = {
  globals: {
    __DEV__: true,
  },
  verbose: false, // false since we want to see console.logs inside tests
  bail: false,
  testURL: 'http://localhost/',
  testEnvironment: 'jsdom',
  testRegex: './__unit__/.*.js$',
  rootDir: '.',
  testPathIgnorePatterns: [
    '<rootDir>/components/coverage/',
    '<rootDir>/test/cypress/',
    '<rootDir>/test/coverage/',
    '<rootDir>/dist/',
    '<rootDir>/node_modules/',
  ],
  moduleFileExtensions: ['js', 'json', 'vue'],
  moduleNameMapper: {
    '^vue$': 'vue/dist/vue.common.js',
    'quasar': 'quasar-framework/dist/umd/quasar.mat.umd.js',
  },
  resolver: null,
  transformIgnorePatterns: [
    'node_modules/core-js',
    'node_modules/babel-runtime',
    'node_modules/vue',
  ],
  transform: {
    '^.+\\.js$': '<rootDir>/node_modules/babel-jest',
    '.*\\.(vue)$': '<rootDir>/node_modules/vue-jest',
  }
}

Then create a folder inside the root of your project called __unit__

Place a file called MyUnitTest.test.js inside the __unit__ folder. Now Jest picks up files from this folder.

The final touch would be to run the tests, simply add this to the package.json

"unit": "yarn run jest --config jest.config.js"

Boom! -- Now you may run yarn run unit or yarn run unit --watch and it should work.

Here's a sample of a Quasar component and Jest test.

import { createLocalVue, shallowMount } from '@vue/test-utils'
import Vuex from 'vuex'
import Quasar, * as All from 'quasar'

import CookieConsent from '@components/common/CookieConsent.vue'


const localVue = createLocalVue()

localVue.use(Vuex)
localVue.use(Quasar, { components: All, directives: All, plugins: All })

describe('CookieConsent.vue', () => {
  const wrapper = shallowMount(CookieConsent, {
    localVue,
    mocks: {
      $t: () => {},
    },
  })

  test('CookieConsent.vue mock should exist', () => {
    expect(wrapper.exists()).toBe(true)
  })

})

Hope you found this useful

like image 91
Trevor-Indrek Lasn Avatar answered Feb 05 '23 22:02

Trevor-Indrek Lasn