解决jest处理es模块

问题场景

项目使用jest进行测试时, 当引入外部库是es模块时, jest无法处理导致报错.

Test suite failed to runJest encountered an unexpected tokenThis 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.Details:/home/xueyou/workspace/projects/node_modules/lodash-es/lodash.js:10import * as lodash from 'lodash-es'SyntaxError: Unexpected token *

解决方法

查阅issues发现, 目前jest不支持em模块, 只有通过babel去处理了

  1. 安装依赖

    yarn add --dev babel-jest @babel/core @babel/preset-env babel-plugin-transform-es2015-modules-commonjs

  2. 配置babel.config.js

    module.exports = {presets: [["@babel/preset-env",{targets: {node: "current"}}]],plugins: ["transform-es2015-modules-commonjs"]
    };
    
  3. 配置jest.config.js

    module.exports = {preset: "ts-jest",testMatch: ["<rootDir>/tests/**/*.(spec|test).ts?(x)"],transform: {// 将.js后缀的文件使用babel-jest处理"^.+\\.js$": "babel-jest","^.+\\.(ts|tsx)$": "ts-jest"},// 下面非要从重要, 将不忽略 lodash-es, other-es-lib 这些es库, 从而使babel-jest去处理它们transformIgnorePatterns: ["<rootDir>/node_modules/(?!(lodash-es|other-es-lib))"]
    };
    

    脚注

    • Unexpected import inside nodejs module
    • Unexpected Token Import for ES6 modules

转载于:https://www.cnblogs.com/xueyoucd/p/10495922.html

解决jest处理es模块相关推荐

  1. 漫画:深入浅出 ES 模块

    翻译自:ES modules: A cartoon deep-dive (https://hacks.mozilla.org/2018/03/es-modules-a-cartoon-deep-div ...

  2. [译] 漫画:深入浅出 ES 模块

    原文地址:ES modules: A cartoon deep-dive 原文作者:Lin Clark 译文出自:掘金翻译计划 本文永久链接:github.com/xitu/gold-m- 译者:st ...

  3. 为什么ES模块比CommonJS更好?

    ES6 模块与 CommonJS 模块的差异 1.CommonJS 模块输出的是一个值的拷贝,ES6 模块输出的是值的引用.2.CommonJS 模块是运行时加载,ES6 模块是编译时输出接口. Co ...

  4. 解决IDEA创建多模块项目找不到创建class类的问题

    解决IDEA创建多模块项目找不到创建class类的问题 参考文章: (1)解决IDEA创建多模块项目找不到创建class类的问题 (2)https://www.cnblogs.com/memoa/p/ ...

  5. 解决PIL没有ImageOps模块问题,以及Windows成功安装torchvision

    解决PIL没有ImageOps模块问题,以及Windows成功安装torchvision 其实更新Pillow就好了.首先说一下我的问题,我是安装了torchvision后再导入torchvision ...

  6. RPA解决:python GSM 模块 - 电脑接收短信

    学Python,用RPA 艺赛旗RPA2020.1版本 正在免费下载使用中,欢迎下载使用 www.i-search.com.cn/index.html?from=line1 前言: 最近做了一个需求, ...

  7. SpringBoot高级-检索-SpringBoot整合Jest操作ES

    接下来就用SpringBoot来整合ElasticSearch进行测试,pom文件引入了spring-boot-starter-data-elasticsearch,其实加了data都是用spring ...

  8. 解决 Gradle 进行多模块开发时,模块相互依赖的问题

    问题背景 在使用多模块开发时,通常一个模块(A)可能会需要其它模块(B)中的类或方法,这时就产生了依赖关系,也就是 A 依赖 B. 要解决上述依赖,有两种方案 1.将B发布到 repo 仓库(可以为本 ...

  9. Jest 只MOCK模块中的某个功能实现

    单元测试某些场景下只想模拟模块中的某个功能,并且保留模块原有的功能.这时候我可以用 jest.requireActual 配合 jest.mock 进行实现. jest.requireActual 该 ...

最新文章

  1. 微信小程序页面间的对象传递
  2. Ubuntu11.04更新源(转)
  3. 国科大高级人工智能7-命题逻辑
  4. 远程开启admin$共享(远程管理需开启如dameware软件)
  5. 部署war包后,新增tomcat服务器,启动tomcat服务器报错解决方法
  6. 利用ACS实现AAA服务的搭建
  7. 工具推荐:2016年最佳的15款Android黑客工具
  8. linux和windows精简版,win7 64/86 超级精简版877MB -三蛋作品
  9. Android实现连线题效果
  10. Office2010的故事 1、从精简版无法升级
  11. 系统架构设计师:分布式系统(中间件技术)
  12. AspNetPager分页
  13. 华为--NAT技术easy IP 原理配置or实验详解
  14. [UE4]Viewport中摄像机镜头缩放速度修改
  15. c++将文件保存至txt文件的方法
  16. Vue中过滤器和自定义指令详解
  17. 【IoT】14.Identify Customer Need 拿捏住客户的想法
  18. windows 远程连接 快捷键
  19. 为论文建立索引的一种方法
  20. windows升级新版本mysql

热门文章

  1. 2018 新浪校招 PHP实习生 电话面试总结
  2. 关于移动硬盘突然变成RAW格式数据该怎么恢复
  3. css自动换行(转载)
  4. 推荐10个免费在线测试网页性能工具
  5. 关于Spring中的ClassPath
  6. 【好工具】安利一款优秀的图片浏览器
  7. 【hbase问题】org.apache.hadoop.hbase.ipc.ServerNotRunningYetException: Server is not run
  8. 认证 (authentication) 和授权 (authorization)小记
  9. 几何光学学习笔记(2)- 1.2 费马原理、马吕斯定律和成像
  10. 修改linux系统iqn,linux iscsi initiator 安装配置