4

I have problem with component testing when using '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"]
}

Got Error:

 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)

I think the problem is in 'transformIgnorePatterns', maybe I write wrong patterns.

I searching for many day, I tried many anwser like change 'testEnviroment' or add plugin in .babelrc but don't found the solution.

Mr.Brown
  • 41
  • 1
  • 2

1 Answers1

3

I ran into the same problem and was just able to get it working by adding the following setting to my jest.config.js:

module.exports = {
  transformIgnorePatterns: ["/node_modules/(?!(echarts|zrender)/)"],
}

If you wrote your configuration as provided here, I assume you wouldn't have to include the root directory in the ignore-pattern. It didn't work either when having more than one ignore-pattern; but this might be because of the way I wrote the ignore-pattern.

SimonSimCity
  • 6,415
  • 3
  • 39
  • 52