简体   繁体   English

如何使用 Cypress.io 运行单元测试?

[英]How to run unit tests with Cypress.io?

I have been using Cypress.io to run end-to-end tests .我一直在使用Cypress.io运行端到端测试 Recently, I have been using it to run unit tests as well.最近,我也一直在使用它来运行单元测试 However, I have some issues with some small helper functions that I have built with NodeJs .但是,我使用NodeJs构建的一些小辅助functions存在一些问题。

I have a create file called utils.spec.js in the following path <my-project-name>/cypress/integration/unit/utils.spec.js & I have written the following tests:我在以下路径<my-project-name>/cypress/integration/unit/utils.spec.js中有一个名为utils.spec.js的创建文件,并且我编写了以下测试:

File: utils.spec.js文件: utils.spec.js

// Path to utils.js which holds the regular helper javascript functions
import { getTicketBySummary } from '../../path/to/utils';

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(
      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(
      getTicketBySummary({
        issues: mockedTickets,
        summaryTitle: 'This is ticket number 3',
      })
    ).eq(mockedEmptyTicketsArray);
  });
});

File: utils.js文件: utils.js

const fs = require('fs');

/**
 * Method to search for an issue by its title
 * Saving the search result into an array
 *
 * @param {array} - issues - an array containing all existing issues.
 * @param {string} - summaryTitle - used to search an issue by title
 */
const getTicketBySummary = ({ issues, summaryTitle }) =>
  issues.data.issues.filter(issueData => {
    return issueData.fields.summary === summaryTitle ? issueData : null;
  });

/**
 * Method to read a file's content
 * Returns another string representing the file's content
 *
 * @param {str} - file - a passed string representing the file's path
 */
const readFileContent = file => {
  return new Promise((resolve, reject) => {
    fs.readFile(file, 'utf8', (err, data) => {
      if (err) return reject(err);
      return resolve(data);
    });
  });
};

module.exports = { getTicketBySummary, readFileContent };

However, when I run the command: npx cypress run --spec=\"cypress/integration/unit/utils.spec.js\" --env mode=terminal , I get the error: Module not found: Error: Can't resolve 'fs' .但是,当我运行命令时: npx cypress run --spec=\"cypress/integration/unit/utils.spec.js\" --env mode=terminal ,我收到错误: Module not found: Error: Can't resolve 'fs'

Also if I commented out the fs import & its function I get another error:此外,如果我注释掉fs导入及其function我会收到另一个错误:

1) An uncaught error was detected outside of a test:
     TypeError: The following error originated from your test code, not from Cypress.

  > Cannot assign to read only property 'exports' of object '#<Object>'

When Cypress detects uncaught errors originating from your test code it will automatically fail the current test.

Cypress could not associate this error to any specific test.

We dynamically generated a new test to display this failure

I did some digging on the second error & it seems the describe method is defined .我对第二个错误进行了一些挖掘,并且似乎defineddescribe方法。 How can I fix both issues?如何解决这两个问题? What am I doing wrong?我究竟做错了什么?

You can use tasks to execute node code.您可以使用任务来执行节点代码。 In your plugins.js create the task with the arguments you need, returning the calculated value:在您的plugins.js中,使用您需要的 arguments 创建任务,返回计算值:

  on('task', {
    // you can define and require this elsewhere
    getTicketBySummary('getTicketBySummary', { issues, summaryTitle }) {
      return issues.data.issues.filter(...);
    }
  })
}

In your test, execute the task via cy.task :在您的测试中,通过cy.task执行任务:

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

That being said, getTicketBySummary looks like a pure function that doesn't depend on fs .话虽如此, getTicketBySummary看起来像一个不依赖于fs的纯 function 。 Perhaps separate helper functions that actually need node as that could avoid need cy.task .也许单独的辅助函数实际上需要node ,因为这样可以避免需要cy.task If you want to be able to import commonjs (require) via ES6 import/export you would usually need to setup build tools (babel/rollup/etc) to be able to resolve that effectively.如果您希望能够通过 ES6 导入/导出导入 commonjs(需要),您通常需要设置构建工具(babel/rollup/etc)才能有效地解决这个问题。

Hopefully that helps!希望这会有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM