網站首頁 編程語言 正文
問題場景
項目使用jest
進行測試時, 當引入外部庫是es
模塊時,?jest
無法處理導致報錯.
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. Details: /home/xueyou/workspace/projects/node_modules/lodash-es/lodash.js:10 import * as lodash from 'lodash-es' SyntaxError: Unexpected token *
解決方法
查閱issues
發現, 目前jest
不支持em
模塊, 只有通過babel
去處理了
安裝依賴
yarn add --dev babel-jest @babel/core @babel/preset-env babel-plugin-transform-es2015-modules-commonjs
配置babel.config.js
module.exports = { presets: [ [ "@babel/preset-env", { targets: { node: "current" } } ] ], plugins: ["transform-es2015-modules-commonjs"] };
配置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
相關推薦
- 2022-06-22 git工作區暫存區與版本庫基本理解及提交流程全解_其它綜合
- 2022-11-16 Python操作csv文件之csv.writer()和csv.DictWriter()方法的基本使用
- 2022-05-05 C語言的三種條件判斷語句你都了解嗎_C 語言
- 2024-02-26 啟動zabbix服務報“./zabbix_agentd: cannot execute binary
- 2023-01-11 ubuntu如何搭建vsftpd服務器_FTP服務器
- 2022-04-11 Docker部署SonarQube的詳細流程_docker
- 2022-09-26 Python文件相關操作和方法匯總大全_python
- 2022-03-29 Python雙端隊列實現回文檢測_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支