javascript - 使用 Quasar-Framework 0.15 进行 Jest 单元测试配置

标签 javascript vue.js jestjs babel-jest quasar-framework

我在 Quasar 0.14 版本下进行了 Jest 测试。目前,一些简单的测试和所有快照测试都通过了,但对于一些测试,我不断得到: 1.

console.error node_modules/vue/dist/vue.common.js:593
      [Vue warn]: Error in config.errorHandler: "TypeError: Cannot read property 'form' of undefined"

和2:

console.error node_modules/vue/dist/vue.common.js:1743
      TypeError: Cannot read property 'getters' of undefined

和3:

console.error node_modules/vue/dist/vue.common.js:593
  [Vue warn]: Unknown custom element: <q-page-sticky> - did you register the component correctly? For recursive components, make sure to provide the "name" option.

1 和 2 似乎与 Jest 无法识别组件中的 $v.form 和 vuex 存储有关。

有什么建议/最佳实践可以让它发挥作用吗?我跟着this ,并进行以下设置: .babelrc:

{
  "presets": [
    [ "env", {"modules": false} ],
    "stage-2"
  ],
  "plugins": ["transform-runtime"],
  "comments": false,
  "env": {
    "test": {
      "presets": [
        [
          "env",
          {
            "targets": {
              "node": "current"
            }
          }
        ]
      ],
      "plugins": [
        [
          "module-resolver",
          {
            "root": [
              "./src"
            ],
            "alias": {
              "quasar": "quasar-framework/dist/quasar.mat.esm.js",
              "^vue$": "vue/dist/vue.common.js"
            }
          }
        ]
      ]
    }
  }
}

在package.json中:

  "jest": {
    "testMatch": [
      "<rootDir>/src/**/?(*.)(spec).js?(x)"
    ],
    "testPathIgnorePatterns": [
      "<rootDir>/src/e2e/"
    ],
    "moduleNameMapper": {
      "src/components/([^\\.]*).vue$": "<rootDir>/src/components/$1.vue",
      "src/components/([^\\.]*)$": "<rootDir>/src/components/$1.js",
      "^vue$": "vue/dist/vue.common.js",
      "src/([^\\.]*)$": "<rootDir>/src/$1.js",
      "src/([^\\.]*).vue$": "<rootDir>/src/$1.vue",
      "(.*)/(.*).vue$": "$1/$2.vue",
      "(.*)/(.*)/(.*).vue$": "$1/$2/$3.vue"
    },
    "moduleFileExtensions": [
      "js",
      "json",
      "vue"
    ],
    "collectCoverageFrom": [
      "**/*.{vue}"
    ],
    "coverageDirectory": "<rootDir>/src/components/coverage",
    "transformIgnorePatterns": [
      "node_modules/core-js",
      "node_modules/babel-runtime",
      "node_modules/lodash",
      "node_modules/vue"
    ],
    "transform": {
      "^.+\\.js$": "<rootDir>/node_modules/babel-jest",
      ".*\\.(vue)$": "<rootDir>/node_modules/vue-jest"
    },
    "snapshotSerializers": [
      "<rootDir>/node_modules/jest-serializer-vue"
    ]
  },

最佳答案

1。问题

发生第三个错误是因为 Jest 不知道 <q-page-sticky> 是什么。是。你必须明确地告诉它。

[Vue warn]: Unknown custom element: <q-page-sticky> - did you register the component correctly? For recursive components, make sure to provide the "name" option.

2。解决方案

就像告诉 Vue 什么是“Vuex”或者什么是“vue-router”一样简单。您可能已经熟悉这一点。唯一的区别是我们必须使用 localVue这里是测试环境。

import { shallowMount, createLocalVue } from "@vue/test-utils";
import MyComponent from "@/components/MyComponent";

// I see that you already alias "quasar" in your .babelrc,
// otherwise replace "quasar" with "quasar-framework/dist/quasar.mat.esm.js"
import Quasar, { q-page-sticky } from "quasar";
// or if you are using a lot of Quasar components then do
// import Quasar, * as All from "quasar";

describe("Something Something", () => {
  const localVue = createLocalVue();
  localVue.use(Quasar, { components: ["q-page-sticky"]});
  // or if you are using a lot of Quasar components then do
  // localVue.use(Quasar, { components: All, directives: All, plugins: All });  

  const wrapper = shallowMount(MyComponent, {
    localVue,
  });

  it("works", () => {
    expect(wrapper.isVueInstance()).toBe(true);
  });
})

3。引用

以上是一个通用的解决方案,不仅可以与Quasar框架一起使用。您可以查看以下官方vue-test-util文档以获取更多信息。

关于javascript - 使用 Quasar-Framework 0.15 进行 Jest 单元测试配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49616642/

相关文章:

javascript - 如何正确测试 React Dropzone onDrop 方法

javascript - 在 PHP Laravel 中将变量从 foreach 循环传递到我的 Controller

javascript - 禁用缓存后,Chrome CORS 请求会更快吗?

javascript - VueJS 加载更多博客文章

javascript - 如何等待所有请求完成?

react-native - 类型错误 : Cannot read property 'ReactCurrentOwner' of undefined

javascript - React enzyme 无法测试 onclick 功能

JavaScript:链接 URL,但从显示的链接文本中删除 'http://'

javascript - 未在无序列表中显示 json 解析的数据

javascript - Prerender vue.js 2.0组件(类似于vue 1中的this.$compile)