# Setup & Reporters

Learn how to set up Unit Tests in Buddy and configure reporters for your testing framework.

## Creating a Suite in Buddy Project

To get started with Unit Tests, you need to create a test suite in your Buddy project:

1. Navigate to project settings
2. Select the **Test Suites** tab
3. Click `+`, to add a new suite

![Project Settings - Empty Test Suites Section](/docs/tests/unit-tests/unittest-config-1.png 640x204)

4. **Add a new suite** form:
   - Enter the **suite name** (e.g., "My Unit Test")
   - **ID** is automatically generated based on the suite name (e.g., `my-unit-test`). The suite ID can be used as a reference in YAML, variables, and scripts.
   - Click **Add suite**

![Project Settings Test Suites](/docs/tests/unit-tests/unittest-config-2.png 640x366)

After creating a suite, you will see a configuration interface with tabs for different testing frameworks: Jest, Jasmine, Mocha, Cypress, Playwright, Vitest, and More (which includes JUnit XML and NUnit options).

Each framework tab contains step-by-step instructions:

1. **Install unit tests package** - the command for installing the package, including information about requirements
2. **Set token** - the command for exporting the environment variable with the suite’s unique token
3. **Run tests with Buddy reporter** - the command for running tests with the Buddy reporter

## Installing the Package

Install Buddy Unit Tests package:

```bash
npm install --save-dev @buddy-works/unit-tests
```
<Hint type="info">
Node.js version 20 or higher is required in your project.
</Hint>

## Setting the Token

Set the `BUDDY_UT_TOKEN` environment variable using your suite’s token:

```bash
export BUDDY_UT_TOKEN=<YOUR_TOKEN>
```

You will find the token in the suite settings

### Running Your First Test

After installing the package and setting the token, you can run the tests from the terminal. For example, with Playwright:

```bash
npm install --save-dev @buddy-works/unit-tests
npx playwright install
export BUDDY_UT_TOKEN=<YOUR_TOKEN>
npx playwright test --reporter=@buddy-works/unit-tests/playwright
```

After running the tests, the interface automatically switches to the **Sessions** tab, where you can see live test results as they execute. The session will complete once all tests have finished.

To use Unit Tests in a Buddy pipeline, see the [Pipeline Integration](/docs/tests/unit-tests/pipeline-integration.md) section. You can find detailed information about viewing results in the [Results & Analytics](/docs/tests/unit-tests/results-and-analytics.md) section.

## Suite Settings

![Unit Tests Settings](/docs/tests/unit-tests/unit-tests-settings.png 640x527)

The **Settings** tab allows you to configure your test suite.

- **SUITE NAME**: Edit the display name of your suite.
- **SUITE ID**: Display or modify the suite ID (used in YAML, variables and scripts).
- **SUITE TOKEN**: View your suite’s unique token. The Regenerate button allows you to create a new token.
- **ERROR PATTERN**: Adjust the error description pattern using the placeholders: 
  ```
  #### Failure Message
  {{testcase.failure.message}}

  #### StackTrace

  {{testcase.failure}}
  ```
- **TIMEOUT**: Set the maximum duration of a test session. Default is 60 seconds.
- **SLOW THRESHOLD**: Define the `slow` tests treshold. Default is 500ms.

## Unit Tests CLI

The **CLI** tab provides quick access to configuration commands for all supported frameworks. You can switch between the framework tabs (Jest, Jasmine, Mocha, Cypress, Playwright, Vitest, JUnit XML, NUnit) to view the appropriate installation commands, token configuration, and test execution instructions.

For frameworks that use JUnit XML or NUnit, the CLI tab also provides options for:
- **Operating system selection**: Windows, macOS, Linux, Linux ARM
- **Installation methods**: Chocolatey, Download or npm
- **CLI install command**: e.g., `sudo npm i -g bdy` 
- **Upload command**: `bdy tests upload --format junit-xml TestResults/TestResult.xml`

![Unit Tests CLI Tab](/docs/tests/unit-tests/unit-tests-cli.png 640x368)

## Built-in Live Reporters

Below you will find detailed configuration instructions for each supported testing framework:

### Jest

```bash
npx jest --reporters=default --reporters=@buddy-works/unit-tests/jest
```

### Jasmine

```bash
npx jasmine --reporter=@buddy-works/unit-tests/jasmine
```

### Mocha

```bash
npx mocha --reporter @buddy-works/unit-tests/mocha
```

### Cypress

First, install both the unit tests package and Mocha (required for Cypress):

```bash
npm install --save-dev @buddy-works/unit-tests mocha
```

Then configure your `cypress.config.js`:

```javascript
const { defineConfig } = require('cypress')
const BuddyCypressReporter = require('@buddy-works/unit-tests/cypress')

module.exports = defineConfig({
  e2e: {
    reporter: '@buddy-works/unit-tests/dist/reporters/cypress/index.js',
    setupNodeEvents(on) {
      on('after:run', BuddyCypressReporter.closeSession)
    },
  },
})
```

And run tests with:

```bash
npx cypress run
```

### Playwright

```bash
npx playwright test --reporter=@buddy-works/unit-tests/playwright
```

![Configure Unit Tests for Playwright](/docs/tests/unit-tests/unit-tests-configuration-5.png 640x311)

### Vitest

```bash
npx vitest --reporter=default --reporter=@buddy-works/unit-tests/vitest
```

## Other Frameworks

For frameworks that don’t have a built-in reporter, you can use the JUnit XML or NUnit format together with the Buddy CLI.

In the **CLI** tab you can specify operating system and installation method.
- **Windows**: Chocolatey, Download or npm
- **macOS**: Homebrew, Download or npm
- **Linux / Linux ARM**: APT, Download or npm

After installing the Buddy CLI and setting the token, you can upload the test results as follows:

### JUnit XML

```bash
bdy tests upload --format junit-xml reports/*.xml
```

![Configure Unit Tests - JUnit XML](/docs/tests/unit-tests/unit-tests-configuration-7.png 640x379)

### NUnit

```bash
bdy tests upload --format junit-xml TestResults/TestResult.xml
```

![Configure Unit Tests - NUnit](/docs/tests/unit-tests/unit-tests-configuration-8.png 640x382)

Alternatively, you can use the Buddy API to build your own live reporter.

## Reporting from Anywhere

You can send test results from: 

- **Buddy pipeline** - add a test action to your pipeline
- **GitHub Actions** - use Buddy reporters or CLI
- **CircleCI and other CI platforms** - integrate via Buddy CLI or API
- **Terminal** - run tests locally and display the results in Buddy


---
Original source: https://buddy.works/docs/tests/unit-tests/setup-and-reporters