Cypress

April 30, 2024

What is Cypress?

Cypress is a front end testing tool for web applications. Cypress can be used both for unit testing, meaning it tests parts of a codebase in isolation, as well as integration testing where the parts are combined and tested to see how they work together. Cypress makes it easy to write and manage standardized front end tests for your codebase, and works hand in hand with your CI (Continuous Integration) system to ensure tests are reliable and repeatable across team members and environments.

Enabling XML Output

Cypress can be configured to produce JUnit XML output by adding the mocha-junit-reporter package to your codebase and modify the config file to add it as a reporter.

1npm install --save-dev mocha-junit-reporter

cypress.config.js or similar file.

1const { defineConfig } = require('cypress')
2module.exports = defineConfig({
3  reporter: 'junit',
4  reporterOptions: {
5    mochaFile: 'results/my-test-output.xml',
6  },
7})

Now you can run Cypress from the command line with

1cypress run

And from within your CI system the same way

1cypress run

Test Suite naming

The mocha-junit-reporter will automatically fill in values for the <testcase> and <testsuite> name and class attributes. 

1describe('addition', () => {
2  describe('positive numbers', () => {
3    it('should add up', () => {
4      expect(1 + 2).toBe(3);
5    });
6  });
7});

would produce output that looks like this:

1<testsuites name="Mocha Tests">
2  <testsuite name="addition">
3    <testcase classname="addition positive numbers should add up" 
4              name="addition positive numbers should add up">
5    </testcase>
6  </testsuite>
7</testsuites>

The default attributes can be configured with the reporterOptions argument in the Cypress config.

Handling Retries

By default, Cypress will not retry tests. The developer must restart the tests manually.  To have Cypress automatically retry failed tests use the retries setting. Ex:

1{
2   retries: 3
3}

If you need to retry a lot of tests that is a strong indicator that you may have flaky tests. These are tests that sometimes pass and sometimes fail, slowing down your development process. You can get early access to Trunk’s new Flaky Tests tool to manage these types of tests better.

Try it yourself or
request a demo

Get started for free

Try it yourself or
Request a Demo

Free for first 5 users