【问题标题】:can not get vuex state using Cypress无法使用赛普拉斯获得 vuex 状态
【发布时间】:2018-04-26 07:53:00
【问题描述】:

我关注this blog。按描述添加代码后,window.app 属性确实出现在控制台中,但是我无法引用博客中提到的app.$store ans(这类似于在组件中引用商店的方式)。我可以引用app.store,它确实包含我的vuex 存储中的属性,但它不包含app.store.state,只是getter(和调度等)。

例如使用const getStore = () => cy.window().its('app.store') 它给出:

我希望能够直接使用状态变量,而不仅仅是在我为它创建一个 getter 时。怎么了?这是因为我在我的 vuex 商店中使用了命名空间吗?

编辑: 这也可能与我将商店分配到窗口的方式有关。我使用 Quasar Framework,在当前版本 (>0.15) 中,它根据它使用的 quasar.conf.js 文件中的设置进行了大量配置。我通过将此代码添加为quasar plugin 来分配商店:

export default ({ app, store, Vue }) => {
  if (window.Cypress) { // should only be available during E2E tests
    window.app = app
  }
}

这样做确实给了我我提到的app.store,但不是app.$store(包括state)。 Quasar 配置生成并显示在一个文件中,添加此插件后该文件包含:

/**
 * THIS FILE IS GENERATED AUTOMATICALLY.
 * DO NOT EDIT.
 *
 * You are probably looking on adding initialization code.
 * Use "quasar new plugin <name>" and add it there.
 * One plugin per concern. Then reference the file(s) in quasar.conf.js > plugins:
 * plugins: ['file', ...] // do not add ".js" extension to it.
 **/
import './quasar'
import Vue from 'vue'
Vue.config.productionTip = false
import 'quasar-app-styl'
import 'src/css/app.styl'
import App from 'src/App'
import router from 'src/router'
import store from 'src/store'

const app = {
  el: '#q-app',
  router,
store,
  ...App
}

const plugins = []

import pluginCypress_vuex_store from 'src/plugins/cypress_vuex_store'
plugins.push(pluginCypress_vuex_store)

import pluginVuelidate from 'src/plugins/vuelidate'
plugins.push(pluginVuelidate)

plugins.forEach(plugin => plugin({ app, router, store, Vue }))

new Vue(app)

正如您所见,插件是初始化 Vue 应用程序的一部分,而博客和您建议的方式是在 Vue 初始化之后附加的。由于 Quasar 自动生成配置,我不知道如何在初始化后执行“window.app = app”,看看这是否会导致我的问题......

【问题讨论】:

    标签: javascript vue.js vuex cypress quasar-framework


    【解决方案1】:

    在我的 Vue 应用程序上也是如此。看到这个section

    在我们编写测试之前,我们需要决定如何获取存储引用。为了允许通过 Vue 实例测试和控制应用程序,我更喜欢在窗口对象上保留对组件的引用。在 app.js 中,设置 window.app 的可测试性。

    const app = new Vue({
      store,
      el: '.todoapp'
      //
    })
    window.app = app
    

    最后一行是关键,当我添加测试代码按宣传的那样工作时。

    注意,要调整的文件可能是 ma​​in.js 而不是 app.js


    Quasar - 特殊应用插件:Boot

    Ref this doc,您可以通过生成此特殊插件,然后将其更改为来实现正确的 Cypress window.app 参考

    export default ({ app, Vue }) => {
      // do some logic here...
    
      // ... then, kick off our Quasar website/app:
      window.app = new Vue(app)
      // "app" has everything cooked in by Quasar CLI,
      // you don't need to inject it with anything at this point
    }
    

    注意,这不是标准的 quasar 应用插件。

    【讨论】:

    • 我已经尝试过这样做。当我使用 Quasar Framework 时,这需要以不同的方式完成。也许这导致了我的问题。我在我的问题中添加了一个 EDIT 以提供更多详细信息。
    • 啊,特殊应用程序插件:我认为 Boot 是后来添加的!它有效,非常好!谢谢!
    • 在 Dev 中拥有我所做的属性:export default ({app, Vue}) =&gt; { if (window.Cypress) { window.app = new Vue(app) } else { const vue = new Vue(app) vue() } } 可以正常工作但在正常(非赛普拉斯)浏览器中出错:'boot.js?5fa4:7 Uncaught TypeError: vue is not a function在 webpack_exports.a (boot.js?5fa4:7)';当我只是做new Vue(app) 时,我收到 linting 错误说“不要使用 'new' 来产生副作用 no-new”。
    • 这使它完全按照我的意愿工作,没有任何错误:export default ({app, Vue}) =&gt; { if (window.Cypress) { window.app = new Vue(app) } else { (() =&gt; new Vue(app))() } }
    • 不管怎样,这种 linting 错误也发生在非类星体 Vue 应用程序中。我使用/* eslint-disable no-new */ 来清理它。
    猜你喜欢
    • 2020-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多