如何将 Redux 操作记录到 Cypress 控制台中

huangapple go评论67阅读模式
英文:

How to log redux actions out into cypress console

问题

抱歉,您的文本包含代码和特殊字符,我无法直接提供准确的翻译。如果您需要有关代码和特殊字符的翻译或解释,请提供更多上下文,我将尽力提供帮助。

英文:

I'm having trouble getting cypress to correctly log each redux action being processed by our app as a live feed into the cypress console.

I had assumed that this would entail subscribing to our actions object which gets exposed to cypress through the window object using the following code.

import { Component, OnInit } from '@angular/core';
import { Actions } from '@ngrx/effects';

@Component({
  selector: 'application-initialize-cypress-variables-component',
  templateUrl: './initialize-cypress-variables.component.html',
})
export class InitializeCypressVariablesComponent implements OnInit {
  public constructor(private readonly actions: Actions) { }

  public ngOnInit(): void {
    if (window.top.Cypress) {
      window.actions = this.actions;
    }
  }
}

Then subscribing to this object within cypress would be pretty straightforward. We would just need to cy.log everytime our app dispactched an action which could be done by adding a cy.log inside of the subscribe block on window.action

actions.subscribe((action) => {
      cy.log(action.type);
    });

This works correctly and logs out each redux action being processed as a live feed to the cypress console. The issue is that I am getting this console error which says something along the lines of: "Cypress detected that you returned a promise from a command while also invoking one or more cy commands in that promise" as the actions are being logged out. This makes me think that I am doing something incorrectly. Any ideas?

答案1

得分: 2

这可能根本不可能,因为Cypress命令在它们自己的队列中异步运行。

这意味着当你的JavaScript发出cy.log(some-param)调用时,它是在说“在所有之前的命令都运行之后,添加一个日志条目以记录some-param”,但它实际上不会在那个时间点执行日志记录。

可能可以使用Cypress.log()来实现,因为它会立即运行。

actions.subscribe((action) => {
  Cypress.log({
    displayName: 'action',
    message: action.type
  })
})

但请注意,Cypress命令队列和Redux操作队列都是从测试运行的JavaScript异步执行的。

我认为您需要一些东西来同步这两者,执行测试中的一步,然后等待生成的Redux操作完成。

英文:

This might not be possible at all, since the Cypress commands run asynchronously in their own queue.

That means when you javascript issues a cy.log(some-param) call it's saying "add a log entry for some-param after all the preceding commands have run", but it's not actually executing the log at that point in time.

You may be able to do it with Cypress.log() instead, since it runs immediately.

actions.subscribe((action) => {
  Cypress.log({
    displayName: 'action',
    message: action.type
  })
})

But note, the Cypress command queue and the redux action queue are both asynchronous from the Javascript of the test runner.

I think you will need something to synchronize the two, something that performs an step in the test then waits for the resulting redux action to complete.

huangapple
  • 本文由 发表于 2023年3月7日 23:47:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75664189.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定