如何解决docker 中的 Cypress 代码覆盖率:无法获取错误
我使用安装了 https://github.com/cypress-io/code-coverage 的 Cypress v7.5.0。配置和设置没有什么特别之处,就像描述中一样。
我有一个在本地运行的 Vue3 测试应用程序,位于 docker 容器内。当我在主机上运行 Cypress 时,一切正常——覆盖、测试,没有问题。 但是,如果我从另一个 docker 容器中运行 Cypress,则测试会由于代码覆盖而失败,请参见屏幕截图:
我知道错误来自代码覆盖率不仅是因为 sceenshot,还因为当我通过使用 cypress run --env coverage=false
运行测试省略覆盖率时测试运行良好。
这是我的 docker-compose 设置:
version: '3.1'
services:
phoenix:
build:
context: .
container_name: phoenix
ports:
- "4000:4000"
- "4001:4001"
networks:
- mapp
volumes:
- ../:/app
command: bash -c "
cd /app && make run_phoenix_inside_docker
"
cypress:
container_name: cypress
# the Docker image to use from https://github.com/cypress-io/cypress-docker-images
image: "cypress/included:7.5.0"
depends_on:
- phoenix
entrypoint: ["/bin/bash","./cypress_entrypoint.sh"]
environment:
# pass base url to test pointing at the web application
- CYPRESS_baseUrl=http://phoenix:4000
# share the current folder as volume to avoid copying
working_dir: /
volumes:
- ../E2E:/e2e
- ./cypress_entrypoint.sh:/cypress_entrypoint.sh
networks:
- mapp
networks:
mapp:
phoenix 是 vue3 应用程序,当我将 phoenix 添加到我的 /etc/hosts
时,它在主机上运行良好,但当我从 Cypress 容器中运行时则不然。
有人知道 Failed to fetch
错误是什么意思吗?文件是顺便写的,所以对coverage文件夹有写权限。
cypress 入口点只是
#!/bin/bash
echo "Init cypress..."
tail -F /dev/null
避免在启动容器后立即运行测试。我猛击容器并从那里运行 cd e2e && cypress run
。
解决方法
看起来它可能来自获取后端覆盖的 cy.request()
,here
代码覆盖率/support.js
if (runningEndToEndTests && isIntegrationSpec) {
// we can only request server-side code coverage
// if we are running end-to-end tests,// otherwise where do we send the request?
const url = Cypress._.get(
Cypress.env('codeCoverage'),'url','/__coverage__'
)
cy.request({
url,log: false,failOnStatusCode: false
})
您要检查 cypress.json 中指定的 url 在 Docker 中是否可用
cypress.json
{
"fixturesFolder": false,"baseUrl": "http://localhost:3003","env": {
"codeCoverage": {
"url": "http://localhost:3003/__coverage__","expectBackendCoverageOnly": true
}
}
}
,
我发现了一个不能真正解决问题的“修复”,但至少测试不再失败。在 support/index.js 中,我只是添加
Cypress.on('uncaught:exception',(err,runnable) => {
// returning false here prevents Cypress from
// failing the test
return false
})
现在测试也在 docker 中运行。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。