# JavaScript and TypeScript

> How trimtest handles Jest, Vitest and Playwright, tsconfig paths, workspaces and package exports.

| | |
|---|---|
| Runners | Jest, Vitest, Playwright |
| Static graph | `import`, `require`, `import()`, `jest.mock`, tsconfig `paths`, barrel files |
| Recorded tier | Jest and Vitest, function-level by name; Playwright (Chromium), file-level, adds to the static graph |
| Runner prefix | `[runner] js`, `jest`, `vitest` or `playwright` (detected from lockfiles otherwise) |

## Static graph

Beyond plain imports, trimtest follows tsconfig `paths`, barrel re-exports, npm/pnpm/yarn **workspace packages**
(by package name, including the `exports` map with its `./sub` keys and `*` patterns), and `package.json`
`imports` aliases such as `#internal/*`.

## Recording

`record --coverage` sets `NODE_V8_COVERAGE` and runs each test file alone. Node children inherit it, so tests that
spawn Node processes are measured normally. Vitest runs with `--pool=threads` because forked workers are killed
before they flush coverage. Selection is [function-level by name](/concepts/function-level/). Verified on Vitest 2
and Jest 29.

If your config overrides the pool (Vitest `poolMatchGlobs`, for example) the recording is not trusted and those
tests use the static graph.

## Playwright

Playwright tests are selected from the static graph, and file paths are regex-escaped when passed to the runner
(so `e2e/[id].spec.ts` works). E2E suites usually have no import link to the code under test: map them yourself
in [`trimtest.toml`](/reference/config/):

```toml
[map]
"api/**" = ["web/e2e/**"]
```

### Recorded tier (Chromium)

`record --coverage` runs each spec file alone (`--workers=1`) with a small trimtest hook loaded into the
Playwright worker (through `NODE_OPTIONS`, so your config is never replaced). The hook starts Chromium's
`page.coverage.startJSCoverage({ resetOnNavigation: false })` on every page of every context, and when the page
or context closes it writes the scripts that ran. trimtest maps those scripts to repository files: inline and
`.map` source maps are read, and `webpack://`, `webpack-internal://`, `turbopack://`, `/@fs/` and
`/src/x.ts?t=...` style URLs are rewritten to repo-relative paths. Selection is file-level.

Browser coverage only ever **adds** to what the static graph and `[map]` already say. The recording of a spec
is the files its pages executed plus everything the spec statically depends on, so:

- A script the page never loaded does not select the spec (editing `public/unused.js`), while one it ran does.
- Node-side code the spec imports (page objects, fixtures, helpers), backend and SSR code, CSS, HTML and
  environment or `webServer` config are invisible to browser coverage. Their static edges and your `[map]`
  rules keep selecting the spec exactly as before. A loaded HTML document is recorded as a file the spec used.
- A first-party script that cannot be traced to a repository file (a bundle with no source map, a `blob:` or
  `data:` script) makes the spec depend on every file of the project, and so do web or service workers the
  page started (their scripts are not part of the page's measurement).
- A spec is left on the static graph when it never opens a page, when any of its projects is not Chromium (the
  coverage API is Chromium-only), when it calls `page.coverage.startJSCoverage` itself, or when collecting
  fails, including a popup the app closes by itself (`window.close()`) before the spec ends.

Source maps are only as good as your dev server or build: use the dev server (Vite, webpack, Next) or build with
`sourcemap: true`. If a source map's `sources` name a file that is not in the repository, the script counts as
untraceable. `sourceMappingURL`s that point at an http server are fetched while the web server is still up.

## Things to know

- A lockfile or build config change forces a full run of that project.
- `.vue`, `.svelte` and `.astro` files are tracked; their script blocks are parsed for imports.
