Javascript 拦截真实数据响应

Javascript 拦截真实数据响应,javascript,automated-tests,cypress,Javascript,Automated Tests,Cypress,我对Cypress 6中的数据存根有问题。我试图实现从服务器到自定义数据的真实数据交换。我读了文件,得出结论 描述(“测试”,()=>{ 它(“截取”,()=>{ cy.intercept(“http://localhost:3000/spoons“,(请求)=>{ 请求回复((res)=>{ 设{body}=res; body.newProperty=“new”; 控制台日志(res.body); 返回体; }); }); }); });它用于拦截外部网络请求 // 常数测试={ html:

我对Cypress 6中的数据存根有问题。我试图实现从服务器到自定义数据的真实数据交换。我读了文件,得出结论

描述(“测试”,()=>{
它(“截取”,()=>{
cy.intercept(“http://localhost:3000/spoons“,(请求)=>{
请求回复((res)=>{
设{body}=res;
body.newProperty=“new”;
控制台日志(res.body);
返回体;
});
});
});

});它用于拦截外部网络请求

//
常数测试={
html:`
取('https://jsonplaceholder.typicode.com/todos/1')
`,
测试:`
赛义德https://jsonplaceholder.typicode.com/todos/1,req=>{
请求回复((res)=>{
设{body}=res;
body.newProperty=“new”;
控制台日志(res.body);
返回体;
});
})
`
}
它('test',()=>{
cy.runExample(测试)
})
这个日志

{
  completed: false,
  id: 1,
  newProperty: "new",           // added by intercept
  title: "delectus aut autem",
  userId: 1
}
你能更详细地解释一下api服务器和客户端端口吗


我设置了你的服务器,发现它工作正常

我唯一更改的是在服务器响应中添加一个no-store头(停止浏览器查看状态代码“304”)

没有它,Cypress测试的每秒刷新
cy.intercept()
不会触发。在测试中,可以通过向
cy.intercept()
添加一个完整的路由匹配器,而不仅仅是url来解决这个问题

app.use((请求、恢复、下一步)=>{
res.set('Cache-Control','no-store')
下一个()
})
app.get(“/”,(req,res)=>{。。。
我还将应用程序中的脚本修改为console.log in
.then()
,否则您只会得到promise对象


取('http://localhost:3000/spoons')
.then(res=>res.json())
.然后(res=>console.log('app',res))
这是我使用的规格

it('test',()=>{
赛义德http://localhost:3000/spoons,req=>{
请求回复((res)=>{
设{body}=res;
body.newProperty=“new”;
console.log('intercept',res.body);
返回体;
});
})
cy.visit(“../app/intercept-mod-response-local-server-2.html”)
})

编辑为包含服务器信息,但很高兴知道它只适用于“外部”端点:/I我在您的服务器上尝试过,发现结果到处都是。有时主体有值,有时没有,有时截取甚至没有捕获。好的,在Cypress.fiddle中运行时,它很脆弱,但每次切换到html文件都很好。