我正在try 用cypress e2e测试来测试我的fullstack angular nestjs应用程序.

来自angular to not的服务器调用到达我运行在localhost:443上的后端(我用0.0.0.0127.0.0.1测试了它,就像其他一些请求的答案一样——没有成功).

另一方面:cy.request('http://localhost:443/...'发送的请求实际上到达了我的后端.我能够在beforeEach中发送请求,保存响应,拦截real请求,并将保存的响应数据提供给它.

cy.login()进行登录调用,为我的后端构建有效会话.

    describe('test', () => {
        let data: any;

        beforeEach(() => {
            cy.login();
            cy.request('http://localhost:443/load').then(response => {
                data = response;
                console.log('BeforeEach Response: ', data);
            });
        });

        it('load data', () => {
            cy.visit('/');
        });
    });

enter image description here enter image description here

但以下beforeEach行代码确实有效:

cy.request('http://localhost:443/load').then(response => {
    data = response;
    console.log('BeforeEach Response: ', data);
});

enter image description here

因此,以下测试确实完全有效:

    describe('test', () => {
        let data: any;

        beforeEach(() => {
            cy.login();
            cy.request('http://localhost:443/load').then(response => {
                data = response;
                console.log('BeforeEach Response: ', data);
            });
        });

        it('load data', () => {
            cy.intercept('/load', data);
            cy.visit('/');
        });
    });

enter image description here

那么,要用真正的服务器请求成功地测试我的应用程序,而不是手动发送相同的请求并删除真正的请求,我缺少什么呢?

推荐答案

我猜你的基本网址是cypress.json不是localhost:443.如果是这样,那么对于基于chrome的浏览器,可以将chromeWebSecurity设置为false.见https://docs.cypress.io/guides/guides/web-security#Set-chromeWebSecurity-to-false.

Node.js相关问答推荐

NX无法使用缓存运行根级脚本

Mongoose-如何从父文档填充到子文档

对于具有重叠列的组合键,在冲突&q;上没有唯一或排除约束匹配错误

JEST模拟由http服务器控制器导入的ES模块

Next.js 路由不起作用 - 页面未正确加载

当API返回400状态代码时,使用Reactjs fetch获取错误消息

在构建完成后,将AddedFiles文件夹的内容复制到dist文件夹

与诗乃一起嘲笑奈克斯

无法通过 NextJS 访问 HTTP 帖子中的正文

Indexeddb 获取所有不同于特定值的记录

在路由上使用中间件时,Nodejs Express 应用程序失败

ResponseError:键空间ks1不存在

在将用作 nodejs/expressjs 中的中间件的函数中使用 keycloak.protect()

将已保存的卡片从条带显示到前端

等到文件上传完成的有效方法(mongoose )

使用服务帐户将 Firebase 应用程序部署到 Heroku(使用 dotenv 的环境变量)

Express.js中的bodyParser.urlencoded({extended: true }))和bodyParser.json()是什么意思?

如何从 Node.js 应用程序Ping?

AWS Lambda:如何将秘密存储到外部 API?

node/nodemon 中是否有对 typescript 的源映射支持?