You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用jest-codemods转换AVA测试时因实验性语法报错

解决jest-codemods解析import断言的错误

以下是几个可行的解决办法,按优先级尝试:

1. 确保Babel插件正确安装并配置

先确认@babel/plugin-syntax-import-assertions已经安装到开发依赖:

npm install --save-dev @babel/plugin-syntax-import-assertions
# 或者用yarn
yarn add -D @babel/plugin-syntax-import-assertions

然后把Babel配置从package.json移到单独的.babelrc.js文件(jest-codemods对单独的配置文件识别度更高):

module.exports = {
  presets: ['@ava/stage-4'],
  plugins: [
    'transform-object-rest-spread',
    '@babel/plugin-syntax-import-assertions'
  ]
};

2. 运行jest-codemods时指定Babel配置

执行codemods命令时,显式指定解析器和配置文件路径:

npx jest-codemods --parser babel --babel-config-path ./.babelrc.js

3. 临时替换JSON导入语法(最快临时方案)

Jest默认支持直接导入JSON文件,不需要assert断言。你可以先把所有类似的导入:

import sample from '../sample.json' assert { type: 'json' };

替换成:

import sample from '../sample.json';

等jest-codemods完成转换后,再根据项目需求恢复原有语法(如果其他场景需要断言的话)。

4. 检查AVA预设的冲突

@ava/stage-4预设可能包含过时的语法配置,和import断言插件存在冲突。可以临时替换成@babel/preset-env测试:

// package.json中的babel配置
"babel": {
  "presets": [
    ["@babel/preset-env", { "targets": { "node": "current" } }]
  ],
  "plugins": [
    "transform-object-rest-spread",
    "@babel/plugin-syntax-import-assertions"
  ]
}

内容的提问来源于stack exchange,提问作者Mickers

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 00:28:19