首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用类星体框架0.15的Jest单元测试配置

使用类星体框架0.15的Jest单元测试配置
EN

Stack Overflow用户
提问于 2018-04-02 18:25:51
回答 2查看 2K关注 0票数 2

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

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

和2:

代码语言:javascript
复制
console.error node_modules/vue/dist/vue.common.js:1743
      TypeError: Cannot read property 'getters' of undefined

和3:

代码语言:javascript
复制
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存储有关。

有什么建议/最佳实践,如何使这一工作?我遵循了,并具有以下设置:.babelrc:

代码语言:javascript
复制
{
  "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内部:

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

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-05-04 16:12:46

我有同样的警告(1和2)。对我来说,它使用了错误的mount。我使用的是Vue的mount函数,而不是@vue/test-utils中的函数。我不知道为什么现在起作用了,但对我来说是这样的。

票数 1
EN

Stack Overflow用户

发布于 2018-05-13 07:39:05

1.问题

第三个错误发生是因为Jest不知道<q-page-sticky>是什么。你得直截了当地说出来。

代码语言:javascript
复制
[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-路由器‘一样简单。你可能已经对此很熟悉了。唯一的区别是我们必须在测试环境中使用localVue

代码语言:javascript
复制
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.参考资料

以上是一个通用的解决方案,不仅适用于类星体框架。您可以查看以下官方vue-test-util文档以获得更多信息。

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

https://stackoverflow.com/questions/49616642

复制
相关文章

相似问题

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