# Getting started

Mootant makes small changes to your code, called mutants, and runs the tests
that should notice them. **A mutant that survives points to a test that is
missing or too weak.**

## Requirements

Mootant needs Node.js and tests on [Vitest](https://vitest.dev),
[Jest](https://jestjs.io) or [Node Test](https://nodejs.org/api/test.html), the
test runner built into Node.js. The tests have to pass as they are: Mootant runs
them without mutants first.

- **Node.js**: 20.19, 22.12 or later
- **Vitest**: Version 5
- **Jest**: Version 29.7+
- **Node Test**: Built-in

See [compatibility](https://mootant.org/guide/compatibility.md) for test runner and framework limits.
Vitest 5 does not support Node.js 20.

## Installation

Add Mootant to the development dependencies of your project:

```sh
npm install -D mootant
```

## First run

Point Mootant at your source code:

```sh
npx mootant run src
```

Mootant finds Vitest, Jest or Node Test and uses your existing configuration.
The mutants are compiled into your code in memory, so your files stay untouched,
and each mutant runs only the test files that reach it.

```text
● mootant run src                                 vitest 5.0.4

dry run         11 tests in 0.2s
mutants         26 in 3 files
time            1.5s (start 0.1s, dry run 0.2s, isolated 1.2s)

killed          25
survived        1
timeout         0
no coverage     0
score           96.15%

survived 1

src/discount.ts
  2:7  comparison-operator  q31p
    - if (total >= 100 || isMember) {
    + if (total > 100 || isMember) {
    test: test/discount.test.ts

re-check one: mootant run src --mutant q31p
```

The report lists the mutants that survived, the ones no test reached and the
ones that caused an error, file by file. Each mutant comes with:

- `comparison-operator`: the [mutator](https://mootant.org/mutators/comparison-operator.md) that made
  it
- [`q31p`](https://mootant.org/reports/html/demo#/mutant/q31p): its short id
- The `-` and `+` lines: the change
- `test:`: the test file to extend

`re-check one:` at the end of the report is the command that runs the first
survivor again.

## Fix a surviving mutant

This mutant is `total >= 100` → `total > 100`. The two versions differ only for
an order of exactly 100, and no test uses one. Move the slider to find it:

Results of `getDiscount(total, false)`:

| Total | `total >= 100` | `total > 100` | Mutant |
| --- | --- | --- | --- |
| 99 | 0 | 0 | survived |
| 100 | 10 | 0 | killed |
| 101 | 10.1 | 10.1 | survived |

Write a test with that value:

```ts
it('gives orders of exactly 100 10% off', () => {
  expect(getDiscount(100, false)).toBe(10)
})
```

Then run the mutant again by its id:

```sh
npx mootant run src --mutant q31p
```

```text
● mootant run src --mutant q31p                   vitest 5.1.0

dry run         26 tests in 0.4s
mutants         1 in 1 file
time            0.7s (start 0.2s, dry run 0.4s, isolated 0.1s)

killed          1
survived        0
timeout         0
no coverage     0
score           100%
```

Verdicts are cached, so the next full run reuses them for mutants whose file and
tests did not change.

## Run in CI

Check only the lines a pull request changed, and fail the build when the
mutation score drops below a threshold:

```sh
npx mootant run src --since origin/main --threshold 80
```

The ref must exist locally: in CI, fetch the base branch with enough history to
find its merge base. The [CLI reference](https://mootant.org/guide/cli.md#shared-options) lists the
other options.

## Let an agent fix them

Mootant ships with an agent skill. It reads the report, writes the missing tests
and asks Mootant to confirm each kill:

```sh
npx skills add azat-io/mootant
```

```text
> fix the surviving mutants in src

$ mootant run src --json
{ …, "mutants": [{ …, "file": "src/discount.ts", "line": 2,
    "after": ["  if (total > 100 || isMember) {"],
    "testFiles": { "test/discount.test.ts": 3, … },
    "recheck": "mootant run src --mutant q31p780qv6yk" }] }

test/discount.test.ts
+ test('orders of exactly 100 get 10% off', () => {
+   expect(getDiscount(100, false)).toBe(10)
+ })

$ mootant run src --mutant q31p780qv6yk
killed          1
survived        0
score           100%
```

The [agent skill](https://mootant.org/agent-skill.md) page shows what the skill changes in your
project.

## Next steps

- [CLI reference](https://mootant.org/guide/cli.md): Look up every option, the exit code and the JSON output.
- [Configuration](https://mootant.org/guide/configuration.md): Choose a preset, turn mutators off and set the defaults of a run.
- [Mutators](https://mootant.org/mutators.md): See what each mutator changes and which test catches it.
- [HTML report](https://mootant.org/reports/html.md): Browse every mutant of a run in its code.
