node modules中包含ES6的语法,怎么进行配置让jest正常测试不报错

来源:4-2 Vue 环境中配置 Jest

夏目鲸鱼

2020-04-28

Test suite failed to run

Jest encountered an unexpected token

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:

/Users/francis/Projects/Ikea/2D-Product-Collage-New-FE/node_modules/vuex-class-modules/lib/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export { Action } from "./actions";
                                                                                         ^^^^^^

SyntaxError: Unexpected token 'export'

  1 | import store from '../store.index'
> 2 | import { VuexModule, Module, Mutation, Action } from 'vuex-class-modules'
    | ^
  3 | 
  4 | const LOCAL_KEY_SELECTED_ROOMID = 'LOCAL_KEY_SELECTED_ROOMID'
  5 | 

  at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
  at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:579:25)
  at Object.<anonymous> (src/store/module/roomInfoStore.ts:2:1)
  at Object.<anonymous> (src/xhr/ajax.ts:5:1)

Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 4.204s
Ran all test suites.

写回答

1回答

EdonPlus

2020-05-26

设置transformIgnorePatterns,忽视node_modules试试

0
0

前端要学的测试课 从Jest入门到 TDD/BDD双实战

自动化测试实战 Vue / React 项目,让技术水平和架构思维双提升

1434 学习 · 350 问题

查看课程