我想用 Cypress 创建一个测试,它有一个 React 组件,该组件使用一个身份验证库 ( @okta/okta-react
) 和一个 HOC ( withOktaAuth
)。
我的组件如下所示:
// Welcome.js
import { withOktaAuth } from '@okta/okta-react'
const Welcome = ({authState}) => {
return <div>{authState.isAuthenticated ? 'stubbed' : 'not working'}</div>
}
export default withOktaAuth(Welcome)
我试着做一个这样的测试:
// test.js
import * as OktaReact from '@okta/okta-react'
const withOktaAuthStub = Component => {
Component.defaultProps = {
...Component.defaultProps,
authState: {
isAuthenticated: true,
isPending: false
},
authService: {
accessToken: '123'
}
}
return Component
}
describe('Test auth', () => {
before(() => {
cy.stub(OktaReact, 'withOktaAuth').callsFake(withOktaAuthStub)
})
it('Stubs auth', () => {
cy.visit('/welcome')
cy.contains('stubbed')
})
})
当我运行测试时,组件仍然不使用 stub 函数。很感谢任何形式的帮助!
最佳答案
提交这个问题已经 2 年了,但是对于那些仍然遇到该错误的人,Cypress 提供了有关 Okta e2e 测试的指南:https://docs.cypress.io/guides/end-to-end-testing/okta-authentication#Adapting-the-back-end
关于javascript - 如何使用 Cypress stub 模块功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61944433/