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

时间:2021-07-21 09:24:20

标签: vue.js jestjs babeljs echarts

我在使用 'vue-echarts' 时遇到组件测试问题

InfoBoard.spec.ts

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

<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

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"]
}

出现错误:

 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)

我认为问题出在“transformIgnorePatterns”上,也许我写错了模式。

我搜索了很多天,我尝试了很多 anwser,例如更改 'testEnviroment' 或在 .babelrc 中添加插件,但没有找到解决方案。

0 个答案:

没有答案