# Public API

The `fwa` module exports functions for preparing and running tests. Use the
[CLI](/fwa/cli) to run tests from a terminal.

## Asynchronous Run

Use `runSuiteAsync` to prepare and execute a suite in one call.

```ts
import { runSuiteAsync } from 'fwa';

const result = await runSuiteAsync({
  projectDir: process.cwd(),
  output: process.stdout
});

process.exitCode = result.exitCode;
```

`runSuiteAsync` is equivalent to preparation followed by execution and returns
a `Promise<SuiteRunResult>`. Configuration and preparation errors reject the
promise. Test failures resolve with `status: 'failed'`. Runner, reporter, and
output-stream errors reject it. The result includes `testFiles`, test counts,
the execution duration, and an `exitCode` value for orchestration decisions.

The asynchronous API is library-oriented: it does not write reporter output
unless `output` is supplied and never mutates `process.exitCode`. A supplied
output stream remains owned by the caller: `fwa` waits for its reporter writes
to complete but does not end the stream.

`output` accepts any writable destination matching the exported `SuiteOutput`
type. Standard Node.js writable streams such as `process.stdout` are compatible.

Pass an `AbortSignal` to cancel a run in progress.

```ts
import { runSuiteAsync } from 'fwa';

const controller = new AbortController();

const resultPromise = runSuiteAsync({
  projectDir: process.cwd(),
  signal: controller.signal
});

controller.abort();

const result = await resultPromise;
```

Cancellation is represented as a completed unsuccessful run: the promise
resolves with `status: 'failed'`, `exitCode: 1`, and cancelled-test counts. It
does not reject solely because the signal was aborted. Native runner or stream
errors still reject the promise.

The four functions shown above and the exported TypeScript types from the
package root are the public API. Internal files under `dist` are
implementation details.

## Prepare and Run

For orchestration, prepare the list of tests and pass it to the runner separately.

```ts
import {
  prepareSuite,
  runPreparedSuite
} from 'fwa';

const plan = prepareSuite({
  projectDir: process.cwd()
});

const result = await runPreparedSuite(plan, {
  onEvent: (event) => {
    if (event.type === 'summary') {
      console.info(event.data);
    }
  }
});

process.exitCode = result.exitCode;
```

`prepareSuite` resolves the TypeScript project, discovers compiled tests, and
checks stale output without starting tests or changing `process.exitCode`. It
returns a `SuitePlan` containing the resolved directories and test-file list.
Preparation options cover project resolution, pruning, runner exclusion, and
diagnostic logging. Pass execution options such as `isolation` and `nodeArgs` to `runPreparedSuite`.
`prepareSuite` takes the preparation options.

`runPreparedSuite` executes the recorded file paths without finding tests again
or rechecking source existence and freshness. The plan does not snapshot file
contents. If the TypeScript config, source tests, or compiled tests change,
prepare a new plan before execution.

An empty plan returns `{ status: 'empty', exitCode: 1 }` and does not start
Node's test runner.

## Compatibility API

The compatibility API runs tests in the same mode as the [CLI](/fwa/cli).

```ts
import { runSuite } from 'fwa';

runSuite({
  projectDir: process.cwd()
});
```

`runSuite` performs configuration and compiled-test validation synchronously,
starts the native test stream, and returns `void` after the stream is wired. It
does not return a `Promise` and cannot be used with `await` to wait for suite completion. This is
the compatibility API used by the CLI.

## TypeScript Project Config

Use `tsConfigPath` to select a config file or a directory containing
`tsconfig.json`.

```ts
import { runSuite } from 'fwa';

runSuite({
  projectDir: process.cwd(),
  tsConfigPath: 'tsconfig.test.json'
});
```

The option follows the same file-or-directory shape as `tsc --project`.
Relative paths are resolved from `projectDir`.

Config parsing is internal to `fwa`. Calling `runSuite` does not load or
constrain the consuming project's `typescript` package.

## Pruning

Pass `prune` to remove compiled tests whose source files no longer exist.

```ts
import { runSuite } from 'fwa';

runSuite({
  projectDir: process.cwd(),
  prune: true
});
```

Pruning is disabled by default.

For safety, pruning requires the configured `outDir` to be a dedicated
directory inside `projectDir`.

If `prune` removes every discovered test, the files are still removed and
preparation reports an empty suite through `log` when supplied. The CLI and
`runSuite` set `process.exitCode = 1`. `prepareSuite` returns an empty plan.
Asynchronous execution returns `status: 'empty'` and `exitCode: 1` without
changing `process.exitCode`.

## Diagnostic Output

Pass `log` to receive preparation diagnostics and keep `console.warn` available
for other messages.

```ts
import { runSuite } from 'fwa';

runSuite({
  projectDir: process.cwd(),
  log: (message) => {
    console.info(message);
  }
});
```

The callback receives successful prune diagnostics and the empty-suite message.
Errors that prevent resolving the selected TypeScript config, its `extends`
chain, or its `rootDir` and `outDir` paths are thrown, as are stale or outdated
compiled-test errors. Other TypeScript compiler diagnostics remain the
responsibility of the consumer's build. In the asynchronous API, use `log` for
preparation diagnostics and `output` or `onEvent` for execution output.

`onEvent` receives `pass`, `fail`, `summary`, `stdout`, and `stderr` events.
The `data` field contains the corresponding `node:test` event data. With process
isolation, Node.js versions with a native `summary` event may report per-file
summaries before the aggregate summary. On runtimes without that event, `fwa`
emits an aggregate summary at the end and measures elapsed time. An exception
in `onEvent` rejects the `Promise`.

## Runner File Exclusion

`runnerFile` excludes one JavaScript file from the discovered test list. It is
intended for custom CLI or bootstrap entrypoints that may themselves be emitted
inside the target `outDir`.

Normal programmatic API callers should omit this option. When it is needed,
prefer an absolute path. relative values follow the process working directory
during exclusion.

## Node.js Test Runner Options

`runSuite` uses the Node.js executable of the current process. It does not
accept an external Node.js executable or manage another runtime.

With `isolation: 'process'`, native test child processes use the current
runtime. With `isolation: 'none'`, tests run in the current process. `nodeArgs`
changes only the flags passed to isolated child processes.

A `runSuite` call confirms behavior only on that runtime. Compatibility with
other Node.js versions must be verified separately by the consuming project's
CI matrix.

Set the isolation mode to run tests in the current process.

```ts
import { runSuite } from 'fwa';

runSuite({
  projectDir: process.cwd(),
  isolation: 'none'
});
```

The `isolation` option requires Node.js `>=22.8.0` when explicitly configured.

Pass Node.js flags to isolated test child processes.

```ts
import { runSuite } from 'fwa';

runSuite({
  projectDir: process.cwd(),
  nodeArgs: [
    '--no-warnings',
    '--conditions=development'
  ]
});
```

A non-empty `nodeArgs` array requires Node.js `>=22.10.0` and cannot be used with
`isolation: 'none'`. An empty array behaves like an omitted option.

## Errors And Exit Code

The compatibility `runSuite` API throws synchronous configuration and
validation errors to the caller.

During compatibility suite execution, `fwa` does not call `process.exit()` directly.
On test failure, or when no runnable tests are found, it sets the following exit code.

```ts
process.exitCode = 1;
```

Test failures are reported after `runSuite` returns because native test
execution is stream-based. The asynchronous orchestration API leaves exit-code
policy to the caller and reports the same outcome through `SuiteRunResult`.

## Current Working Directory

`projectDir` controls config resolution and compiled-test discovery. The native
Node test runner is still started in the caller's current process context. the
API does not temporarily call `process.chdir` or provide per-suite environment
mutation. Orchestrators that require a separate working directory or
environment should launch an isolated worker process around `fwa`.
