首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Jest对类星体应用程序进行单元测试?

如何使用Jest对类星体应用程序进行单元测试?
EN

Stack Overflow用户
提问于 2018-10-12 17:18:20
回答 1查看 4.6K关注 0票数 4

我有一个用quasar-cli生成的类星体应用程序。

如何将单元测试集成到像Jest这样的应用程序的测试运行程序中?

我在我的Jest配置中添加了一个这个

代码语言:javascript
复制
"moduleNameMapper": {
    "quasar": "<rootDir>/node_modules/quasar-framework"
}

不幸的是,Jest报告了

代码语言:javascript
复制
Cannot find module 'quasar' from 'index.vue'

下面是Vue文件的片段

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-25 17:05:37

把Jest和类星体结合起来是相当直截了当的.您需要两个包,babel-jestjest

yarn add jest babel-jest -D

在添加了这两个依赖项之后,在项目的根处创建一个jest.config.js文件--这是所有jest配置的去处。

下面是jest.config.js文件的样子;

代码语言:javascript
复制
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了,它应该能工作。

这是类星体组件和Jest测试的示例。

代码语言:javascript
复制
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)
  })

})

希望你觉得这个有用

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52784267

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档