如何在Next.js中使用Cypress来使用和使用下一个

发布于 2025-02-05 16:20:53 字数 700 浏览 1 评论 0原文

我试图分别与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:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空宴 2025-02-12 16:20:53

您需要使用cy.stub(用途,'Prototype')。返回('my_value')

以下测试可能会在执行时在我的chrome console中打印控制台警告:

import React from 'react'
import FormCreationConcession from './FormCreationConcession'
import TEST_TYPES from '@/Constants/tests-types'
import {useSession, SessionProvider} from 'next-auth/react'
import {testLocators} from '@/Constants/testLocators'

// https://medium.com/geekculture/component-testing-next-js-application-with-cypress-28fa311adda6
describe(`${TEST_TYPES.component.integration} <FormCreationConcession /> works correctly for professionnal site user`, () => {
  beforeEach(() => {
    // see: https://on.cypress.io/mounting-react
    cy.mount(
      <SessionProvider session={{}}>
        <FormCreationConcession />
      </SessionProvider>
    )

    cy.stub(useSession, 'prototype').returns(console.warn('testttt'))
  })

  it('findByTestId', () => {
    cy.findByTestId(testLocators.components.FormCreationConcession.title)
  })
})

读取<< 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 :

import React from 'react'
import FormCreationConcession from './FormCreationConcession'
import TEST_TYPES from '@/Constants/tests-types'
import {useSession, SessionProvider} from 'next-auth/react'
import {testLocators} from '@/Constants/testLocators'

// https://medium.com/geekculture/component-testing-next-js-application-with-cypress-28fa311adda6
describe(`${TEST_TYPES.component.integration} <FormCreationConcession /> works correctly for professionnal site user`, () => {
  beforeEach(() => {
    // see: https://on.cypress.io/mounting-react
    cy.mount(
      <SessionProvider session={{}}>
        <FormCreationConcession />
      </SessionProvider>
    )

    cy.stub(useSession, 'prototype').returns(console.warn('testttt'))
  })

  it('findByTestId', () => {
    cy.findByTestId(testLocators.components.FormCreationConcession.title)
  })
})

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文