首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Jest测试vue-echarts组件时出错

使用Jest测试vue-echarts组件时出错
EN

Stack Overflow用户
提问于 2021-07-21 09:24:20
回答 1查看 458关注 0票数 3

当使用“vue-echarts”时,我对组件测试有问题。

InfoBoard.spec.ts

代码语言:javascript
复制
import { render } from '@testing-library/vue'

import InfoBoard from '@/components/InfoBoard.vue'

describe('InfoBoard', () => {
  test('Should be truthy', () => {
    const wrapper = render(InfoBoard, {
      stubs: {
        'v-charts': true
      },
    })
    expect(wrapper).toBeTruthy()
  })
})

InfoBoard.vue

代码语言:javascript
复制
<template>
  <div>
    <v-chart></v-chart>
  </div>
</template>

<script>
import { defineComponent } from '@nuxtjs/composition-api'
import VChart from 'vue-echarts'

export default defineComponent({
  components: {
    VChart
  },
  setup() {}
})
</script>

jest.config.js

代码语言:javascript
复制
module.exports = {
  moduleNameMapper: {
    '^@/(.*)$': '<rootDir>/$1',
    '^~/(.*)$': '<rootDir>/$1',
    '^vue$': 'vue/dist/vue.common.js'
  },
  moduleFileExtensions: [
    'ts',
    'js',
    'vue',
    'json'
  ],
  transform: {
    "^.+\\.ts$": "ts-jest",
    '^.+\\.js$': 'babel-jest',
    '.*\\.(vue)$': 'vue-jest'
  },
  collectCoverage: true,
  collectCoverageFrom: [
    '<rootDir>/components/**/*.vue',
    '<rootDir>/pages/**/*.vue'
  ],
  transformIgnorePatterns: [
    "<rootDir>/node_modules/(?!echarts)",
    "<rootDir>/node_modules/(?!echarts\/core)",
    "<rootDir>/node_modules/(?!vue-echarts)"
  ],
  testEnvironment: 'jsdom',
  setupFilesAfterEnv: ["./jest-setup.js"]
}

有错误:

代码语言:javascript
复制
 FAIL  components/InfoBoard.spec.ts
  ● Test suite failed to run

    Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

    Details:

    /Users/admin/Documents/THIP/node_modules/echarts/core.js:20
    export * from './lib/export/core';
    ^^^^^^

    SyntaxError: Unexpected token 'export'

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1479:14)
      at Object.<anonymous> (node_modules/vue-echarts/dist/index.cjs.min.js:1:179)

我认为问题在于“转换in”,也许我写错了模式。

我搜索了很多天,我尝试了很多类似于改变'testEnviroment‘或者在.babelrc中添加插件的程序,但是没有找到解决方案。

EN

回答 1

Stack Overflow用户

发布于 2022-02-28 14:27:53

我遇到了同样的问题,通过将下面的设置添加到我的jest.config.js中,使它能够正常工作

代码语言:javascript
复制
module.exports = {
  transformIgnorePatterns: ["/node_modules/(?!(echarts|zrender)/)"],
}

如果您按照这里提供的方式编写了配置,我假设您不必将根目录包含在忽略模式中。当有一个以上的忽略模式时,它也不起作用;但这可能是因为我编写忽略模式的方式。

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

https://stackoverflow.com/questions/68467058

复制
相关文章

相似问题

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