【问题标题】:How to access redux store in Cypress if the application is running outside of Cypress tests如果应用程序在赛普拉斯测试之外运行,如何访问赛普拉斯中的 redux 存储
【发布时间】:2021-06-13 05:05:21
【问题描述】:

在我的 E2E Cypress 测试中,我想访问被测应用程序的 Redux 存储并调度一些操作来更改一些 redux 存储值。我已经阅读了一些关于从 Cypress (https://www.cypress.io/blog/2018/11/14/testing-redux-store/) 访问 Redux 存储的博客和教程,我需要在 src/index.js 中公开应用程序存储,它应该位于应用程序包内。

供您参考,我们的“被测应用程序”和 Cypress 测试位于不同的项目结构(不同的包名称)中。

const store = createStore(reducer)

render(
  <Provider store={store}>
    <App />
  </Provider>,
  document.getElementById('root')
)

// expose store when run in Cypress
if (window.Cypress) {
  window.store = store
}

赛普拉斯是否可以访问在不同包中运行的应用程序的 redux 存储?

【问题讨论】:

  • 应该可以,因为您只是将商店添加到应用程序窗口。为什么不试试看呢?

标签: redux cypress


【解决方案1】:

你可以使用cy.window首先访问窗口,然后像这样发送一个动作:

cy.window().then(win => {
  if (win.store) {
    win.store.dispatch(...));
  }
})

并确保在此之前使用 cy.visit() 没有这个没有窗口对象可以获取。

【讨论】:

    猜你喜欢
    • 2020-12-03
    • 2020-10-26
    • 1970-01-01
    • 2019-12-27
    • 1970-01-01
    • 2023-02-14
    • 1970-01-01
    • 1970-01-01
    • 2023-01-10
    相关资源
    最近更新 更多