如何使用Jest对quasar应用程序进行单元测试?

4
我有一个使用quasar-cli生成的Quasar应用程序。
我该如何将单元测试集成到类似于Jest这样的测试运行器中?
我已经在Jest配置文件中添加了this。
"moduleNameMapper": {
    "quasar": "<rootDir>/node_modules/quasar-framework"
}

很不幸,Jest 反馈如下:

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

这是Vue文件的一部分代码。
<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>

你能分享一下 index.vue 的内容吗? - Boussadjra Brahim
当然可以分享一些,因为代码是专有的。我会编辑原始问题 @boussadjrabrahim - Kim Merino
1个回答

9

将Jest与Quasar集成非常简单。您需要两个软件包:babel-jestjest

yarn add jest babel-jest -D

在添加这两个依赖项之后,在项目根目录中创建一个jest.config.js文件--这里是所有jest配置的地方。

以下是jest.config.js文件应如何编写:

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',
  }
}

然后在项目的根目录下创建一个名为__unit__的文件夹。

将名为MyUnitTest.test.js的文件放置在__unit__文件夹中。现在Jest会从该文件夹中获取文件。

最后一步是运行测试,只需将以下代码添加到package.json中即可:

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

完成!现在您可以运行yarn run unityarn run unit --watch来执行测试。

以下是Quasar组件和Jest测试的示例。

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)
  })

})

希望您会发现这篇文章对您有所帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接