【问题标题】:@quasar/extras/mdi-v4 jest unit testing failing on Unexpected token 'export'@quasar/extras/mdi-v4 开玩笑的单元测试在意外令牌“导出”上失败
【发布时间】:2020-08-19 06:38:50
【问题描述】:

我正在使用新的 SVG 图像。我为包含徽标的标题创建了一个测试,名称为 mdiMenu。

从“@quasar/extras/mdi-v4”导入 { mdiMenu } 但是我正在运行测试的所有内容:我总是收到此错误:

SyntaxError: Unexpected token 'export'

      29 | 
      30 | <script>
    > 31 | import { mdiMenu } from '@quasar/extras/mdi-v4'
Do somebody else have encounter this problem and how can I solve this?

This is the rest of the error message:

This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    Here's what you can do:
     • 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/en/configuration.html

    Details:

    C:\Users\YohnCheung\work\bitBucket\central-station\node_modules\@quasar\extras\mdi-v4\index.js:3
    export const mdiAbTesting = 'M4 2A2 2 0 0 0 2 4V12H4V8H6V12H8V4A2 2 0 0 0 6 2H4M4 4H6V6H4M22 15.5V14A2 2 0 0 0 20 12H16V22H20A2 2 0 0 0 22 20V18.5A1.54 1.54 0 0 0 20.5 17A1.54 1.54 0 0 0 22 15.5M20 20H18V18H20V20M20 16H18V14H20M5.79 21.61L4.21 20.39L18.21 2.39L19.79 3.61Z'
    ^^^^^^

问题来自:

https://forum.quasar-framework.org/topic/5459/quasar-extras-mdi-v4-jest-unit-testing-failing https://forum.vuejs.org/t/quasar-extras-mdi-v4-jest-unit-testing-failing/89973

【问题讨论】:

    标签: jestjs quasar


    【解决方案1】:

    需要设置正确的转换。 所以需要正确配置jest.config.js

     ...
     transform: {
        '^.+\\.jsx?$': require.resolve('babel-jest'),
     },
     ...
    

    我的笑话配置正在扩展预设

    preset: '@vue/cli-plugin-unit-jest/presets/typescript-and-babel',
    

    已经包含了这个转换,但也设置不转换来自 node_modules 的 pacages

    transformIgnorePatterns: ['/node_modules/'],
    

    参考:https://github.com/vuejs/vue-cli/blob/dev/packages/%40vue/cli-plugin-unit-jest/presets/default/jest-preset.js

    因此在我的配置中我将模式更改为

      transformIgnorePatterns: ['node_modules/(?!(quasar|quasar/*|@quasar|@quasar/*))'],
    

    所以它会忽略除类星体之外的所有包

    希望这个答案对某人有所帮助:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-10
      • 1970-01-01
      • 2017-09-13
      • 1970-01-01
      • 1970-01-01
      • 2019-01-30
      • 2021-10-28
      • 2017-07-04
      相关资源
      最近更新 更多