@quasar/extras/mdi-v4 开玩笑单元测试在意外令牌'export'失败



我正在使用新的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:UsersYohnCheungworkbitBucketcentral-stationnode_modules@quasarextrasmdi-v4index.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-failinghttps://forum.vuejs.org/t/quasar-extras-mdi-v4-jest-unit-testing-failing/89973

需要设置正确的转换。因此,您需要正确配置jest.config.js

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

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

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

它已经包括了这个转换,但也设置为不转换来自node_modules 的包

transformIgnorePatterns: ['/node_modules/'],

参考:https://github.com/vuejs/vue-cli/blob/dev/packages/%40vue/cli-插件单元jest/presets/default/jest-preset.js

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

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

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

希望这个答案能帮助到别人:(

最新更新