<!-- https://testapilocally.com/docs/core-features/tests-scripts/ -->
# Tests & scripts

* **Platform Availability:** Windows App · VS Code Extension (shared shell)
* **Overview:** Pre-request scripts and tests run in a QuickJS sandbox with the `qa.*` dialect (plus `pm.*` / `bru.*` / `pw.*` aliases). Collection and folder scopes use Scope Script Studio.

![Windows - Tests tab](/docs/assets/screenshots/windows/windows-request-tab-tests.png)

## How It Works (Windows App)

1. Open a request → **Pre-request Script** and/or **Tests**.
2. Write assertions with `qa.test` / `qa.expect`.
3. Send or run via Runner — failures show in Checks / Runner report.
4. For folder/collection scripts, open **Open Scripts** from the tree context menu.

![Windows - Pre-request](/docs/assets/screenshots/windows/windows-request-tab-pre-request.png)

## How It Works (VS Code Extension)

Same editors and sandbox inside the webview. Host-gated APIs (`qa.ui`, some file/DB helpers) follow the VS Code host bridge.

![VS Code - Tests](/docs/assets/screenshots/vscode/vscode-request-tab-tests.png)

## Code / Configuration Examples

```javascript
// Pre-request: set a timestamp variable
qa.variables.set("ts", String(Date.now()));

// Tests
qa.test("status 200", () => {
  qa.expect(qa.response.code).to.equal(200);
});

qa.test("json has url", () => {
  const body = qa.response.json();
  qa.expect(body.url).to.be.a("string");
});

// Extract for later requests
qa.variables.set("gotUrl", qa.response.json().url);
```

Postman-style alias:

```javascript
pm.test("status is 200", function () {
  pm.response.to.have.status(200);
});
```

## Edge Cases, Troubleshooting & Pro-Tips

- Prefer `qa.*` in new scripts; aliases exist for migration.
- `qa.sendRequest` nests HTTP from scripts — avoid infinite loops.
- `qa.db` / `qa.load` / `qa.files` / `qa.ui` are host-gated; behavior can differ slightly by runtime.

> **GAP IDENTIFIED:** Some scripting host APIs are gated per runtime (Electron vs VS Code). If a `qa.ui` call no-ops, run that flow on Windows desktop or use variables/tests only.

## Next

- [Runner](/docs/core-features/runner/)
- [MCP & CLI](/docs/core-features/mcp-and-cli/)
