如何让 jest 在 ESM 下正常工作

in #cn18 days ago

最近在弄 faucet 的重构工作,因为要全面使用新的依赖和模式,遇到了很多问题。

比如最近一周一直被 jest 无法在 ESM 模式下工作的问题卡住。

搜索引擎 + chatgpt 多方面的尝试都没有找到有效方案。

直到昨天看到 jest 官方文档里有专门的一页说这个,才搞定问题。

解决方案就是两步。

第一步是用空配置 {} 替换掉之前的 jest.config.jstransform,即关闭 transform

第二步增加环境变量 --experimental-vm-modules,以启用 node 的实验 API,因为 jest 使用的 node 的实验 API 实现的 ESM 支持,这也就意味着,可以卸载掉 babel-jest 插件了。

启用实验 API 这里, jest 官方文档只给了两个 CLI 下的使用例子,而我们的项目使用的是 package.json 中的 scripts 方式。

因此要想在 scripts 里启用,可以按照下面的格式:

"scripts": {
    "jest": "NODE_OPTIONS=--experimental-vm-modules jest"
  },

ET碎碎念,每周更新,欢迎订阅,点赞,转发!


好用不贵的VPS推荐

https://1hour.win


感谢你的阅读,我是中文区见证人之一,欢迎通过 AuthSteem 来给我投票,或者打开 https://steemitwallet.com/~witnesses 页面,输入 ety001 进行投票。

2.gif

中文区的见证人目前有:
支持一下他们(按字母顺序),一人可以有30票


Thank you for reading. I'm a witness. I would really appreciate your witness vote! You can vote by SteemLogin. Or open https://steemitwallet.com/~witnesses page, input ety001 to vote.

2.gif

Sort:  

"😊 Congrats on figuring out the jest ESM problem! 🎉 That's always a frustrating issue, but it's great that you persisted until finding the solution. 👍 I love how you shared the exact steps to enable experimental-vm-modules in Jest. 💡 It's super helpful for others who might encounter this same problem. 😊 Would love to hear more about your faucet refactor project and any other projects you're working on! 🤔 Don't forget to keep sharing your experiences with us, and thanks for the GIFs - always a plus! 😄 By the way, I just voted for xpilar.witness at https://steemitwallet.com/~witnesses. Hope you'll consider doing the same! 💕"

Coin Marketplace

STEEM 0.17
TRX 0.15
JST 0.028
BTC 58119.05
ETH 2357.18
USDT 1.00
SBD 2.36