# Tests

> How BareCI keeps JUnit results from every job and run, flags flaky tests, ranks the slowest and balances shards by recorded duration.

Source: https://bareci.com/docs/modules/tests

Tests keeps the results of every job and run that reports JUnit XML: each test's file, name, duration and outcome, for 30 days.

## Reporting results

In a run, pass `--junit`:

```sh
bareci run --junit junit.xml -- bun test --reporter=junit --reporter-outfile=junit.xml
```

In a CI job, report the file after the tests:

```sh
bareci report test-results/junit.xml
```

Flows write JUnit too, so end-to-end results sit beside unit tests.

## Flaky tests

A test is **flaky** when it both failed and passed on the same commit, or passed on retry. `tests_flaky` lists flaky tests with their counts and last failure message.

The [Gate](https://bareci.com/docs/modules/gate) uses this: a failed check whose failing tests are all known to be flaky is re-run before it counts, and new flaky tests in a pull request are named in the verdict.

## Slowest tests

`tests_slowest` lists the slowest files and tests, so an agent can see, for example, which few files take most of the suite's time.

## Sharding

```sh
bareci shard --total 4 --index 1 tests/**/*.test.ts
```

`shard` prints this shard's files, balanced by recorded duration: longest first onto the least-loaded shard. Files with no history count as the median. With `bareci run --shards N`, each machine has `BARECI_SHARD_INDEX` and `BARECI_SHARD_TOTAL` set.

See the [Tests API](https://bareci.com/docs/api/tests).
