> ## Documentation Index
> Fetch the complete documentation index at: https://trunk-4cab4936-test-collections-setup-instructions.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Cypress

> A guide for generating Trunk-compatible test reports for Cypress tests

You can automatically [detect and manage flaky tests](../../detection/) in your Cypress projects by integrating with Trunk. This document explains how to configure Cypress to output JUnit XML reports that can be uploaded to Trunk for analysis.

## Setup steps

Work through the steps below in order. Once you've finished the last one, you'll be ready to move on to [configure uploads in CI](../ci-providers/).

<Steps>
  <Step title={<a href="#generating-reports">Generate a compatible test report</a>} />

  <Step title={<a href="#report-file-path">Configure the report file path or glob</a>} />

  <Step title={<a href="#disable-retries">Disable retries for better detection accuracy</a>} />

  <Step title={<a href="#try-it-locally">Test uploads locally</a>} />
</Steps>

## Generating Reports

Cypress has a built-in Mocha JUnit reporter which outputs XML test reports. However, the built-in reporter does not include file paths in test case elements, which means Trunk cannot match tests to code owners or enable file-based filtering in the dashboard.

### Recommended: Use cypress-junit-plugin for file paths

For full functionality including code owner detection and file-based search, use the [`cypress-junit-plugin`](https://github.com/saucelabs/cypress-junit-plugin) reporter. It outputs test cases with the correct nested structure and file path attributes that Trunk expects.

Install the plugin:

```bash theme={null}
npm install --save-dev @saucelabs/cypress-junit-plugin
```

Update your Cypress config:

```javascript title="cypress.config.js" theme={null}
const { defineConfig } = require('cypress')
const { setupJUnitPlugin } = require('@saucelabs/cypress-junit-plugin')

module.exports = defineConfig({
  e2e: {
    setupNodeEvents(on, config) {
      setupJUnitPlugin(on, config, { filename: './junit.xml' })
      return config
    },
  },
})
```

### Alternative: Built-in Mocha reporter

If you don't need file path matching or code owner detection, you can use the built-in reporter. Uploads will still work, but you will see warnings about missing file paths and won't be able to search by file in the dashboard.

```javascript title="cypress.config.js" theme={null}
const { defineConfig } = require('cypress')

module.exports = defineConfig({
  reporter: 'junit',
  reporterOptions: {
    mochaFile: './junit.xml',
    toConsole: true,
  },
})
```

<Info>
  The built-in Mocha JUnit reporter places the `file` attribute on `<testsuite>` elements but not on individual `<testcase>` elements. Trunk requires file paths on test cases for code owner matching. If you see warnings like "report has test cases with missing file or filepath", switch to the `cypress-junit-plugin` above.
</Info>

### Report File Path

The JUnit report location is specified by the `filename` option passed to `setupJUnitPlugin` (or the `mochaFile` property when using the built-in reporter). In the above examples, the file will be at `./junit.xml`.

### Disable Retries

You need to disable automatic retries if you previously enabled them. Retries compromise the accurate detection of flaky tests.

You can disable retries by setting `retries: 0` in your Cypress config file.

```javascript title="cypress.config.js" theme={null}
module.exports = defineConfig({
  retries: 0,
})
```

## Try It Locally

### The Validate Command

<CodeGroup>
  ```bash Linux (x64) theme={null}
  SKU="trunk-analytics-cli-x86_64-unknown-linux.tar.gz"
  curl -fL --retry 3 \
    "https://github.com/trunk-io/analytics-cli/releases/latest/download/${SKU}" \
    | tar -xz

  chmod +x trunk-analytics-cli
  ./trunk-analytics-cli validate --junit-paths "./junit.xml"
  ```

  ```bash Linux (arm64) theme={null}
  SKU="trunk-analytics-cli-aarch64-unknown-linux.tar.gz"
  curl -fL --retry 3 \
    "https://github.com/trunk-io/analytics-cli/releases/latest/download/${SKU}" \
    | tar -xz

  chmod +x trunk-analytics-cli
  ./trunk-analytics-cli validate --junit-paths "./junit.xml"
  ```

  ```bash macOS (arm64) theme={null}
  SKU="trunk-analytics-cli-aarch64-apple-darwin.tar.gz"
  curl -fL --retry 3 \
    "https://github.com/trunk-io/analytics-cli/releases/latest/download/${SKU}" \
    | tar -xz

  chmod +x trunk-analytics-cli
  ./trunk-analytics-cli validate --junit-paths "./junit.xml"
  ```

  ```bash macOS (x64) theme={null}
  SKU="trunk-analytics-cli-x86_64-apple-darwin.tar.gz"
  curl -fL --retry 3 \
    "https://github.com/trunk-io/analytics-cli/releases/latest/download/${SKU}" \
    | tar -xz

  chmod +x trunk-analytics-cli
  ./trunk-analytics-cli validate --junit-paths "./junit.xml"
  ```
</CodeGroup>

### Test Upload

Before modifying your CI jobs to automatically upload test results to Trunk, try uploading a single test run manually.

You make an upload to Trunk using the following command:

```sh theme={null}
./trunk-analytics-cli upload --junit-paths "./junit.xml" \
    --org-url-slug <TRUNK_ORG_URL_SLUG> \
    --test-collection-id <TRUNK_TEST_COLLECTION_ID> \
    --token <TRUNK_ORG_TOKEN>
```

You can find your Trunk organization slug and token in the settings or by following these [instructions](/flaky-tests/get-started/ci-providers/otherci#id-1.-store-a-trunk_token-secret-in-your-ci-system). The collection ID identifies the [test collection](/flaky-tests/get-started/test-collections) the results belong to, and you copy it from that collection in the Trunk app. After your upload, you can verify that Trunk has received and processed it successfully in the **Uploads** tab. Warnings will be displayed if the report has issues.

<Frame>
  <img className="block dark:hidden" src="https://mintlify.s3.us-west-1.amazonaws.com/trunk-4cab4936-test-collections-setup-instructions/assets/_shared/data-uploads-light.png" alt="" />

  <img className="hidden dark:block" src="https://mintlify.s3.us-west-1.amazonaws.com/trunk-4cab4936-test-collections-setup-instructions/assets/_shared/data-uploads-dark.png" alt="" />
</Frame>

## Next Step

Configure your CI to upload test runs to Trunk. Find the guides for your CI framework below:

<Columns cols={3}>
  <Card title="Azure DevOps Pipelines" href="../ci-providers/azure-devops-pipelines" img="https://mintlify.s3.us-west-1.amazonaws.com/trunk-4cab4936-test-collections-setup-instructions/assets/_shared/azure.png" />

  <Card title="BitBucket Pipelines" href="../ci-providers/bitbucket-pipelines" img="https://mintlify.s3.us-west-1.amazonaws.com/trunk-4cab4936-test-collections-setup-instructions/assets/_shared/bitbucket.png" />

  <Card title="BuildKite" href="../ci-providers/buildkite" img="https://mintlify.s3.us-west-1.amazonaws.com/trunk-4cab4936-test-collections-setup-instructions/assets/_shared/buildkite.png" />

  <Card title="CircleCI" href="../ci-providers/circleci" img="https://mintlify.s3.us-west-1.amazonaws.com/trunk-4cab4936-test-collections-setup-instructions/assets/_shared/circle-ci.png" />

  <Card title="Drone CI" href="../ci-providers/droneci" img="https://mintlify.s3.us-west-1.amazonaws.com/trunk-4cab4936-test-collections-setup-instructions/assets/_shared/drone.png" />

  <Card title="GitHub Actions" href="../ci-providers/github-actions" img="https://mintlify.s3.us-west-1.amazonaws.com/trunk-4cab4936-test-collections-setup-instructions/assets/_shared/github.png" />

  <Card title="GitLab" href="../ci-providers/gitlab" img="https://mintlify.s3.us-west-1.amazonaws.com/trunk-4cab4936-test-collections-setup-instructions/assets/_shared/gitlab.png" />

  <Card title="Jenkins" href="../ci-providers/jenkins" img="https://mintlify.s3.us-west-1.amazonaws.com/trunk-4cab4936-test-collections-setup-instructions/assets/_shared/jenkins.png" />

  <Card title="Semaphore" href="../ci-providers/semaphoreci" img="https://mintlify.s3.us-west-1.amazonaws.com/trunk-4cab4936-test-collections-setup-instructions/assets/_shared/semaphore.png" />

  <Card title="TeamCity" img="https://mintlify.s3.us-west-1.amazonaws.com/trunk-4cab4936-test-collections-setup-instructions/assets/_shared/teamcity.png" />

  <Card title="Travis CI" href="../ci-providers/travisci" img="https://mintlify.s3.us-west-1.amazonaws.com/trunk-4cab4936-test-collections-setup-instructions/assets/_shared/travis.png" />

  <Card title="Other CI Providers" href="../ci-providers/otherci" img="https://mintlify.s3.us-west-1.amazonaws.com/trunk-4cab4936-test-collections-setup-instructions/assets/_shared/other.png" />
</Columns>
