使用Cypress.io对我自己的Node模块进行单元测试时出错 [英] Getting error while unit testing my own Node modules with Cypress.io

查看:115
本文介绍了使用Cypress.io对我自己的Node模块进行单元测试时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遵循先前的问题&答案.我能够开始使用

Following this previous question & answer. I was able to start using Cypress to unit test my Node's module by using the task() method. However, I'm getting an error which I have been trying to debug or find a reference online but I haven't been able to.

cy:command ✘  assert    expected **{ Object (userInvocationStack, specWindow, ...) }** to equal **[]**

这是什么 {对象(userInvocationStack,specWindow,...)} ?我该如何获取对象的实际 array ?

What's this { Object (userInvocationStack, specWindow, ...) }? How can I get the actual array of objects instead?

编辑:包含的断言& task 代码.

Included assertions & task code.

文件: utils.spec.js

describe('Unit Tests for utils.js methods', () => {
  /**
   * Array of objects mocking Tickets Object response
   */
  const mockedTickets = {
    data: {
      issues: [
        {
          id: 1,
          key: 'ticket-key-1',
          fields: {
            summary: 'This is ticket number 1',
          },
        },
        {
          id: 2,
          key: 'ticket-key-2',
          fields: {
            summary: 'This is ticket number 2',
          },
        },
      ],
    },
  };

  const mockedEmptyTicketsArray = [];

  it('returns an array containing a found ticket summary', () => {
    expect(
      cy.task('getTicketBySummary', {
        issues: mockedTickets,
        summaryTitle: 'This is ticket number 1',
      })
    ).eq(mockedTickets.data.issues[0]);
  });
  it('returns an empty array, when no ticket summary was found', () => {
    expect(
      cy.task('getTicketBySummary', {
        issues: mockedTickets,
        summaryTitle: 'This is ticket number 3',
      })
    ).eq(mockedEmptyTicketsArray);
  });
});

文件:plugins/index.js

File:`plugins/index.js

on('task', {
    getTicketBySummary({ issues, summaryTitle }) {
      issues.data.issues.filter(issueData => {
        return issueData.fields.summary === summaryTitle ? issueData : null;
      });
    },
  });

推荐答案

我能够通过返回 task 的值来解决此问题,如下所示:

I was able to solve the issue by returning the task's value like so:

文件: plugins/index,js

on('task', {
    getTicketBySummary({ issues, summaryTitle }) {
      return issues.data.issues.filter(issueData => {
        return issueData.fields.summary === summaryTitle ? issueData : null;
      });
    },
  });

更新代码以使用 then()方法&断言所传递的 param 值,就像这样:

After I updated my code to use the then() method & assert the passed param value instead like so:

文件: utils.spec.js

it('returns an empty array, when no ticket summary is found', () => {
    cy.task('getTicketBySummary', {
        issues: mockedTickets,
        summaryTitle: 'This is ticket number 3',
     }).then(result => expect(result).to.deep.equal(emptyArray));
});

这篇关于使用Cypress.io对我自己的Node模块进行单元测试时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆