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

TestCafe Docker运行时全局请求钩子类型不匹配报错求助

TestCafe Docker环境全局RequestMock钩子报错解决

问题现象

本地通过npm run b运行TestCafe测试时,.testcaferc.js中配置的全局RequestMock请求钩子可正常生效;但使用TestCafe Docker镜像执行以下命令时:

docker run -v //c/testproject:/mytests -it testcafe/testcafe "chromium:headless:emulation:device=iPhone X" /mytests/testone.js --config-file /mytests/.testcaferc.js

出现报错:

ERROR Cannot prepare tests due to the following error: The hook (object) is not of expected type (RequestHook subclass).

本地项目依赖testcafe@^2.0.1,仅Docker环境触发该问题。

相关配置代码

.testcaferc.js配置:

const { RequestMock} = require("testcafe");

const mock = RequestMock()
.onRequestTo('https://a360cdn.azureedge.net/javascript/a360.consent.default.js')
    .respond(null, 200)

module.exports = {
    hooks: {
        request: [mock]
    },
    browsers:  "chromium:headless:emulation:device=iPhone X",
    skipJsErrors: true,
    skipUncaughtErrors: true,
};

测试文件testone.js:

import {Selector} from 'testcafe'

fixture `fixture 1`

test.page(`https://www.chileautos.cl/`)(`First test`, async t => {
    await t.expect(Selector(`div.element-id`).exists).ok()
});

原因分析

Docker镜像默认使用latest标签的TestCafe版本,该版本与本地项目依赖的testcafe@2.0.1不一致。不同版本的TestCafe中,RequestMock实例对应的RequestHook类并非同一构造函数,导致Docker环境无法识别本地配置中创建的钩子对象类型。

解决步骤

  1. 指定Docker镜像版本与本地一致:
    修改Docker命令,使用与本地项目匹配的TestCafe镜像版本(即2.0.1):
    docker run -v //c/testproject:/mytests -it testcafe/testcafe:2.0.1 "chromium:headless:emulation:device=iPhone X" /mytests/testone.js --config-file /mytests/.testcaferc.js
    
  2. 验证版本匹配:
    执行docker run -it testcafe/testcafe:2.0.1 --version确认镜像内的TestCafe版本与本地项目一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 15:35:35