Cypress.io setUpNodeEvents: `cy.task` 返回的对象缺少函数属性。

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

Cypress,io setUpNodeEvents: cy.task is missing function properties from returned object

问题

我有一个返回对象字面量的节点函数。该对象将具有各种属性,其中包括一个函数属性。当我通过cypress的setUpNodeEvents设置一个任务来调用此函数并在cypress测试中使用它时,返回的对象不再具有该函数属性;它现在是未定义的。

有人能看出为什么函数属性丢失吗?

节点函数

module.exports = build = () => {
 return {
  myFunc() {}
 };
}

cypress.config

const build = require('./builder');

module.exports = defineConfig({
  env,
  e2e: {
    setupNodeEvents(on, config) {
      on('task', {
        buildObject: () => build(); 
      })
    },
  },
})

cypress测试文件

describe('transform', () => {
    it('should return object with a function property', () => {
        cy.task('buildObject')  
        .then(result => assert.isFunction(result.myFunc)); 
    });
});

我期望result.myFunc被定义并且是一个函数,但结果是'expected undefined to be a function'。

英文:

I have a node function that returns an object literal. That object will have various properties, among them a function property. When I set up a task via cypress setUpNodeEvents to call this function and use that in a cypress test, the returned object no longer has the function property; it is now undefined.

Can anyone see why the function property is lost?

node function

module.exports = build = () => {
 return {
  myFunc() {}
 };
}

cypress.config

const build = require('./builder');

module.exports = defineConfig({
  env,
  e2e: {
    setupNodeEvents(on, config) {
      on('task', {
        buildObject: () => build(); 
      })
    },
  },
})

cypress test file

describe('transform', () => {
    it('should return object with a function property', () => {
        cy.task('buildObject')  
        .then(result => assert.isFunction(result.myFunc)); 
    });
});

I expect result,myFunc to be defined and be a Function but the result is 'expected undefined to be a function'.

答案1

得分: 3

请参考Cypress任务 - 参数

arg (对象)

与事件一起发送的参数。这可以是任何可以通过 JSON.stringify() 进行序列化的值。无法序列化的类型,如函数、正则表达式或符号,将被省略为 null。

这适用于输入参数,但相同的规则也适用于输出结果。

这是因为 task 边界是一个进程间边界,你不能在其间进行调用。同样适用于 cy.origin() 沙盒边界。

为了解决问题,调用函数内部的任务并返回其结果。

从我所看来,逻辑上没有理由传回函数本身,因为 cy.task('buildObject') 就是它的简单包装。

英文:

Please refer to this Cypress task - arguments

> arg (Object)
>
> An argument to send along with the event. This can be any value that can be serialized by JSON.stringify(). Unserializable types such as functions, regular expressions, or symbols will be omitted to null.

That refers to the input arguments, but the same rule applies to output results.

It's because the task boundary is an inter-process boundary and you cannot make calls across it. Same applies to the cy.origin() sandbox boundary.

To resolve the problem, call the function inside the task and return it's result.

There is logically no reason (as far as I can see) to pass back the function itself, as cy.task('buildObject') is a straight-forward wrapper for it.

huangapple
  • 本文由 发表于 2023年3月3日 22:32:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75628359.html
匿名

发表评论

匿名网友

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

确定