網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
問(wèn)題場(chǎng)景
項(xiàng)目使用jest
進(jìn)行測(cè)試時(shí), 當(dāng)引入外部庫(kù)是es
模塊時(shí),?jest
無(wú)法處理導(dǎo)致報(bào)錯(cuò).
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
發(fā)現(xiàn), 目前jest
不支持em
模塊, 只有通過(guò)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庫(kù), 從而使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
相關(guān)推薦
- 2022-08-11 python使用prettytable內(nèi)置庫(kù)美化輸出表格_python
- 2022-04-10 python?tkinter實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能_python
- 2022-07-06 安裝pytorch報(bào)錯(cuò)torch.cuda.is_available()=false問(wèn)題的解決過(guò)程_
- 2022-10-20 C++淺析虛函數(shù)使用方法_C 語(yǔ)言
- 2022-05-25 python教程之利用pyautogui圖形自動(dòng)化擊敗重復(fù)性辦公任務(wù)_python
- 2022-10-10 AOSP源碼下載示例代碼_Android
- 2023-12-26 Mapper.xml文件中SQL新增數(shù)據(jù)的簡(jiǎn)便寫法
- 2022-06-23 C#使用DLLImport調(diào)用外部DLL的方法_C#教程
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支