# Testing a package

> nxm test checks a package before anyone uses it: action cases against the rules of the instance, custom views in a test browser with the real view SDK, and module services against a fake platform. Claude Code writes the tests along with the module.

`nxm validate` checks that a package is well formed. Whether it does what it should is another question: does "Erledigen" refuse an entry that is already done? Does the overview page still show the overdue entries in red after the last change? `nxm test` answers that, in three kinds of tests that live in the package folder and never go into the `.nxm`.

<Flow
  steps={[
    { icon: 'list-checks', title: 'Action cases', text: 'tests/*.json: a record, an action, what it should do. Checked by the instance, nothing is written.' },
    { icon: 'app-window', title: 'Views', text: 'tests/*.test.mjs: your custom view in a test browser with the real view SDK and test records.', tone: 'cyan' },
    { icon: 'server', title: 'Services', text: 'services/<key>/*.test.ts: your service against a fake platform, under Deno.', tone: 'lavender' },
  ]}
  caption="nxm test runs whatever of the three the package has, and fails when one of them does."
/>

```
acme.kunden/1.1.0/
├── module.json
├── tests/
│   ├── erledigen.json        action cases
│   └── offen.test.mjs        a view test
└── services/
    └── rechner/
        ├── main.ts
        └── main.test.ts      a service test
```

## Action cases

A case names an action, the record it meets, and what should happen. The record does not have to exist: give its data under `record`, and the instance treats it as the record (`recordId` uses a stored one instead).

```json
{
  "cases": [
    {
      "name": "Ein offener Eintrag wird erledigt",
      "action": "erledigen",
      "record": { "titel": "Angebot schicken", "status": "offen" },
      "expect": { "runs": true, "writes": { "status": "erledigt" } }
    },
    {
      "name": "Ein erledigter Eintrag bleibt, wie er ist",
      "action": "erledigen",
      "record": { "titel": "Rechnung", "status": "erledigt" },
      "expect": { "runs": false, "refusal": "NOT_APPLICABLE" }
    }
  ]
}
```

| `expect` | Checks |
|---|---|
| `runs` | Whether the action would run (required) |
| `writes` | The values an `update` or `create` would write, or a `mail` would set after sending. Only the fields you name; `"$any"` accepts any value that is set |
| `refusal` | The code it would refuse with: `NOT_APPLICABLE` (`when` does not hold), `FORBIDDEN` (a right is missing), `VALIDATION` (a value breaks a rule of the entity) and so on |

`payload` gives the input of an action that asks for one. The cases run through the trace of your development tenant, after `nxm test` installed the folder there (`--no-apply` skips that): permission, `when`, every template, the entity's validation and unique values, exactly as a click would meet them. Nothing is written and nothing is sent.

## View tests

A view test loads one of your custom views into a test browser, with the same view SDK the app gives it, and answers its calls from records you give it. It runs with [vitest](https://vitest.dev); install it once in the folder with your packages:

```sh
npm install --save-dev vitest jsdom @nexxus-platform/nxm
```

```js
// tests/offen.test.mjs


test('the Open page shows open entries only, overdue ones in red', async () => {
  const view = await mountView(new URL('../ui/offen.html', import.meta.url), {
    records: { eintrag: [
      { title: 'Angebot', data: { titel: 'Angebot', status: 'offen', faellig_am: '2020-01-01' } },
      { title: 'Rechnung', data: { titel: 'Rechnung', status: 'erledigt' } },
    ] },
  });
  const items = [...view.document.querySelectorAll('.nx-list__item')];
  expect(items.map(i => i.textContent)).toEqual(['Angebot · 2020-01-01']);
  expect(items[0].classList.contains('ueberfaellig')).toBe(true);

  await view.click('.nx-list__item');
  expect(view.navigations).toEqual([{ entity: 'eintrag', id: 1 }]);
  view.close();
});
```

`mountView(file, options)` resolves once `nx.ready()` has. What you get back:

| | |
|---|---|
| `document`, `window` | The view's page |
| `click(selector)`, `type(selector, text)` | A click or input, and the wait for what follows |
| `settle()` | Waits until the view's calls are answered |
| `calls` | Every call of the view: `{method, params}`, in order |
| `records` | The records as the view left them, per entity |
| `toasts`, `navigations` | What `nx.ui.toast` showed and where `nx.nav.open` went |
| `emit(name, data)` | A notice to the view, as the platform sends it (`record`, `context`) |

The options: `records` per entity, `record` (`{entity, id}`) for a view in a record slot, `params`, `locale`, `theme`, `user`, and `handlers` with answers to other calls by method, such as `invoke` or `lookup`. A call without an answer fails with `UNKNOWN_METHOD`, so a test names everything the view depends on. Nothing checks rights in a view test; that is what the action cases are for.

## Service tests

A module service is tested like the module services of nexxus itself: against a fake platform that hands out tokens, signs calls and keeps records in memory. It ships with the service kit:

```ts
// services/rechner/main.test.ts

Deno.test('the calculation answers with the deadline', async () => {
  const platform = await FakePlatform.start();
  // ... call your handler with platform.signInvocation('acme.kunden') and check platform.writes()
  await platform.stop();
});
```

`nxm test` runs `deno test` in every service folder with a test, after it put the kit where the service's `deno.json` expects it.

## Running them

| | |
|---|---|
| `nxm test` | Everything the package has, after an apply to the development tenant |
| `nxm test --only actions` | Only the action cases (`views`, `services` likewise) |
| `nxm test --no-apply` | Against what is installed now |
| `nxm test --json` | One line with every case, for scripts and CI |

The exit code is `0` when everything passed and `1` when something failed, so a pipeline can stop on it. In CI, set `NXM_URL` and `NXM_TOKEN` as for every other command.

## Asking Claude Code for it

| You want | You could say |
|---|---|
| Cases for an action | "Write test cases for Erledigen: open entries are finished, finished ones refuse." |
| A view test | "Test the Open page: only open entries, overdue in red." |
| Everything checked | "Run the tests and fix what fails." |

<CardGrid>
  <LinkCard title="Develop locally" href="/modules/develop/" description="nxm, the scratch and development tenant, logs and traces." />
  <LinkCard title="Help, tours and news" href="/modules/help/" description="What the people who use the package read." />
</CardGrid>