Jest 在 es2015 模块时出错
jest 在处理 es2015 模块的时候, 如果是 node_modules
里的,会报错,以 lodash-es/cloneDeep
为例:
/node_modules/lodash-es/cloneDeep.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import baseClone from './_baseClone.js';
^^^^^^
SyntaxError: Unexpected token import
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/ScriptTransformer.js:290:17)
at Object.<anonymous> (src/fetch.ts:46:70)
at Object.<anonymous> (src/index.ts:6:10)
参照设置(https://facebook.github.io/jest/docs/configuration.html#transformignorepatterns-array-string),添加
"transformIgnorePatterns": ["<rootDir>/node_modules/(?!lodash-es)/"]
然而并不生效,各位大神知道怎么做么?
具体 repo(lodash 分支): https://github.com/doxiaodong/intercept-fetch/tree/lodash
执行: yarn install, npm test 即可出现
4 回复
不要被淹没呀,很急
npm 包实际上是 js, 需要用 babel-preset-es2015
处理
“transformIgnorePatterns”: ["<rootDir>/node_modules/(?!lodash-es)"]