# contractdiff/testing

> Every export of contractdiff/testing, with its signature and doc comment: ConformanceError, conformance, RULES, PluginError, plus 18 types.

Source: https://contractdiff.interlace.tools/docs/api/testing

<!-- Generated by scripts/api-reference.ts from the built dist/*.d.ts. Do not edit; run `npx tsx scripts/api-reference.ts`. -->

```ts
import { ConformanceError, conformance, RULES, … } from 'contractdiff/testing';
```

## Classes

### ConformanceError

Every rule a plugin broke. Its message is the list, one failure and its `fix:` line per entry.

```ts
class ConformanceError extends Error {
    readonly failures: readonly Failure[];
    constructor(plugin: string, failures: readonly Failure[]);
}
```

### PluginError

A refusal says what is wrong and what to do about it: the family's one vocabulary.

```ts
class PluginError extends Error {
    readonly code: PluginErrorCode;
    readonly fix: string;
    constructor(code: PluginErrorCode, message: string, fix: string);
}
```

## Constants

### conformance

Run the conformance suite on `plugin`. Resolves when it passes every rule; rejects with a
`ConformanceError` naming every rule it breaks. A plugin `host.validate` refuses is reported
under `schema` alone, since no other rule can run on it.

```ts
const conformance: (plugin: unknown, options?: ConformanceOptions) => Promise<void>;
```

### RULES

```ts
const RULES: readonly ["schema", "sample", "deterministic", "regeneration", "roundtrip", "fix-lines", "paths", "no-io"];
```

## Interfaces

### Adapter

An input format: a parsed document in, the model out.

```ts
interface Adapter {
    readonly description: string;
    readonly detect: (document: unknown) => boolean;
    readonly read: (document: unknown) => Contract;
}
```

### ConformanceOptions

```ts
interface ConformanceOptions {
    /** More documents to run every generator on: each must give the same bytes twice, and safe paths. */
    readonly corpus?: readonly unknown[];
    /**
     * The sample document after a spec change, for the `regeneration` rule. The suite makes this
     * itself for an OpenAPI or Swagger sample (one operation and one property more); any other
     * format needs it.
     */
    readonly mutate?: (document: unknown) => unknown;
    /** The extract half, for the `roundtrip` rule: run with its sample's options plus `files`, what the generator wrote. */
    readonly extractor?: Extractor;
    /** The generator `extractor` pairs with. Every generator of the plugin, when absent. */
    readonly generator?: string;
}
```

### Extractor

Code → spec (spec.md R-TE2): reads the user's code, named by its options, and returns a document, or `{ document, findings }`.

```ts
interface Extractor {
    readonly description: string;
    readonly options?: Readonly<Record<string, unknown>>;
    readonly sample: ExtractorSample;
    readonly run: (options: Options) => unknown;
}
```

### Failure

One rule broken in one place, and what to do about it.

```ts
interface Failure {
    readonly rule: Rule;
    readonly where: string;
    readonly message: string;
    readonly fix: string;
}
```

### GeneratedFile

```ts
interface GeneratedFile {
    /** Relative to the output folder, with `/` separators. */
    readonly path: string;
    /** The body: the writer adds the generated header, so a generator never writes one. */
    readonly content: string;
    readonly owner: Owner;
    /** The symbols this file declares per operation. Optional: a file without them is still mapped by its path. */
    readonly symbols?: readonly GeneratedSymbol[];
}
```

### Generator

Spec → code: data plus one function (R-PL2).

```ts
interface Generator {
    readonly description: string;
    /** Names of the input formats it accepts, e.g. `openapi`. */
    readonly inputs: readonly string[];
    /** burgee option declarations, keyed by option name. */
    readonly options?: Readonly<Record<string, unknown>>;
    readonly sample: Sample;
    readonly run: (contract: Contract, options: Options) => readonly GeneratedFile[] | GeneratorResult | Promise<readonly GeneratedFile[] | GeneratorResult>;
}
```

### GeneratorResult

What a generator may return when it has something to report besides files.

```ts
interface GeneratorResult {
    readonly files: readonly GeneratedFile[];
    readonly findings: readonly ContractFinding[];
}
```

### LintNode

```ts
interface LintNode {
    /** A JSON Pointer into the source document, or a logical one for an inline payload schema. */
    readonly pointer: string;
    readonly value: Readonly<Record<string, unknown>>;
    /** The operation the node belongs to; absent for the document. */
    readonly operation?: Operation;
    /** A property's or parameter's name. */
    readonly name?: string;
    /** What a `path` selected, as it is: a `then` with no `field` reads this, not `value`. */
    readonly matched?: unknown;
}
```

### LintRule

```ts
interface Rule {
    readonly description: string;
    readonly given: Given;
    /** What must hold, by the built-in functions; every entry must. */
    readonly then?: Then | readonly Then[];
    /** What must hold, when no built-in function says it: the problems on one node, each a message. Pure. */
    readonly check?: (node: LintNode, context: LintContext) => readonly string[];
    readonly fix: string;
    /**
     * A document and exactly what the rule finds on it (R4): the rule's own test, which the
     * conformance suite runs. Required of a rule a plugin contributes.
     */
    readonly sample?: RuleSample;
}
```

### Plugin

The keys contractdiff reads. Any object with these fields is a plugin here, whatever else it carries.

```ts
interface Plugin {
    readonly name: string;
    readonly contract?: number;
    readonly adapters?: Readonly<Record<string, Adapter>>;
    readonly generators?: Readonly<Record<string, Generator>>;
    readonly extractors?: Readonly<Record<string, Extractor>>;
    /** `lint` rules (apicontract-lint R4): each with the sample that is its test. */
    readonly rules?: Readonly<Record<string, Rule>>;
}
```

### RuleSample

```ts
interface RuleSample {
    readonly document: unknown;
    readonly findings: readonly SampleFinding[];
}
```

### SampleFinding

Where a rule finds a problem on its sample, and what it says.

```ts
interface SampleFinding {
    readonly pointer: string;
    readonly message: string;
}
```

## Types

### Options

```ts
type Options = Readonly<Record<string, unknown>>;
```

### Rule

```ts
type Rule = (typeof RULES)[number];
```

## Re-exported

Documented on the page of the entry point that declares them.

| Export | Kind | Documented in |
| :-- | :-- | :-- |
| `Contract` | interface | [`contractdiff/core`](/docs/api/core#contract) |
| `ContractFinding` | interface | [`contractdiff/core`](/docs/api/core#contractfinding) |
| `Operation` | interface | [`contractdiff/core`](/docs/api/core#operation) |
| `Payload` | interface | [`contractdiff/core`](/docs/api/core#payload) |
