警告:在 Jest 中运行 Axios 测试时“从浏览器构建中排除后续重定向”

问题描述 投票:0回答:1

在 Jest 中使用 Axios 运行单元测试时,我遇到与

follow-redirects
相关的持续警告:

Warning: exclude follow-redirects from browser builds

重现步骤:

  • 我正在使用 Axios 在 Node.js 项目中发出 HTTP 请求。

  • 运行涉及 Axios 的 Jest 测试(例如 axios.get())时,我始终在控制台输出中看到此警告。

  • 测试本身正在通过,但每次都会出现警告。

我尝试过的:

  • 我已确保 Jest 配置为使用 testEnvironment: 'node'。

  • 我尝试模拟 Axios 和 follow-redirects 模块来避免这个问题。

  • 我检查了 Jest 设置,以确保不会触发特定于浏览器的构建,但警告在所有测试文件中仍然存在。

  • 运行 npm ls follow-redirects 显示 Axios 是使用此包的唯一直接依赖项。

我的环境:

  • Axios版本:1.7.7

  • 笑话版本:29.7.0

axios jestjs
1个回答
0
投票

我在 Nx monorepo 上运行 Vitest 时遇到了类似的问题。我注意到我的 pnpm 锁定文件中的 axios 依赖解析如下所示:

 - [email protected]
   - follow-redirects: 1.15.8([email protected])

然后查看跟随重定向

1.15.8
1.15.9
之间的差异,似乎负责错误日志记录的
nope.js
文件已被删除,取而代之的是更优雅的浏览器排除处理:

我尝试安装

1.15.9
,最新版本中不再出现错误日志记录。

因此,也许可以考虑以下几点:

  • 执行后续重定向的包依赖项更新,在我的 pnpm 例子中是
    pnpm update follow-redirects
  • 如果这不起作用,请考虑安装 follow-redirects 作为直接依赖项,但强制它至少为
    1.15.9
    ,在我的情况下,这将是
    pnpm add follow-redirects@^1.15.9
© www.soinside.com 2019 - 2024. All rights reserved.