如何在Next.js中使用Cypress来使用和使用下一个
我试图分别与Cypress固执,并分别与useession()
和getsession()
分别对后端调用。
固执函数似乎无法替换函数或被调用:
const client = require("next-auth/react")
....
....
cy.stub(client, "getSession").returns({
user: {
name: "xxx",
email: "xxx",
image: "xxx",
},
expires: "2022-07-08T09:49:47.602Z",
})
cy.stub(client, "useSession").returns({
user: {
name: "xxx",
email: "xxx",
image: "xxx",
},
expires: "2022-07-08T09:49:47.602Z",
})
cy.visit(`/draft/cl45ip2d600379as17epvu6ti`)
而且似乎没有很多文档,我不确定如何继续。
我不确定这是否相关,但是客户http请求对API触发了呼吁。
I'm trying to stub with Cypress both the frontend and backend calls to useSession()
and getSession()
respectively.
The stubbed functions don't seem to replace the functions or get called:
I'm trying to do it this way:
const client = require("next-auth/react")
....
....
cy.stub(client, "getSession").returns({
user: {
name: "xxx",
email: "xxx",
image: "xxx",
},
expires: "2022-07-08T09:49:47.602Z",
})
cy.stub(client, "useSession").returns({
user: {
name: "xxx",
email: "xxx",
image: "xxx",
},
expires: "2022-07-08T09:49:47.602Z",
})
cy.visit(`/draft/cl45ip2d600379as17epvu6ti`)
I've tried googling it but most people seem to use jest for this and not cypress and there doesn't seem to be a lot of documentation and I'm not sure how to continue.
I'm not sure if this is relevant or not, but the call to getSession is being triggered by a client http request to the api.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用
cy.stub(用途,'Prototype')。返回('my_value')
以下测试可能会在执行时在我的chrome console中打印控制台警告:
读取<< a href =“ https://docs.cypress.io/api/commands/stub” rel =“ nofollow noreferrer”> https://docs.cypress.io/api/api/commands/stub ,然后分析如何分析我可以操纵一个函数,这是通过执行
// console.log('useSession:',object.getownpropertydescriptors(useessesse))
帮助我找出答案的一种函数。You need to use
cy.stub(useSession, 'prototype').returns('my_value')
The following test could print the console warn in my chrome console as it executed :
A bit of reading of https://docs.cypress.io/api/commands/stub and then analyzing how I could manipulate a function, which is a type of object by doing
// console.log('useSession : ', Object.getOwnPropertyDescriptors(useSession))
helped me to find out.