# One answer instead of three: document a feature next to its code with FACET

> Write the real behaviour of one feature into a FACET record, a short description file next to the code, connect it with one comment, and let a check confirm both still match in every pull request.

Source: https://facet-framework.com/en/guides/write-down-what-a-feature-does/  
Last updated: 2026-11-26T23:28:23+01:00

A new colleague asks in the team chat what happens when a customer enters a discount code that expired yesterday. The backend developer says the shop refuses it. Support says it still works until midnight. The product manager remembers a grace period from a planning meeting. All three are sure, and none of them can point to where it is written down.

FACET is a command-line tool that keeps a short, checkable description of each feature next to your code and tells you when code and description drift apart. By the end of this guide that answer is written down once, next to the code: a FACET record, a small YAML file with plain Given/When/Then scenarios, and a comment in the source, called an anchor, that connects the two. `facet lint` then checks on your machine and in every pull request that the record and the code still point at each other.

The commands below use the shop from the [FACET examples](https://github.com/facet-framework/examples/tree/main/shop). Copy it into a project of its own:

```sh
git clone --depth 1 https://github.com/facet-framework/examples.git
cp -r examples/shop shop && cd shop
git init && git add -A && git commit -m "Example shop"
```

Branch `guide/write-down-what-a-feature-does` holds the result. In your own project, pick one feature that people describe differently and use its files instead.

## Add FACET to the project

FACET is a command-line tool distributed through npm. Install the release candidate as a development dependency in the root of your project, so everyone on the team gets the same version from the lockfile:

```sh
npm install --save-dev facet-framework
```

Check that the project's copy runs:

```sh
npx facet version
```

The first line starts with `facet core`. If npm instead asks to install a package called `facet`, you are in a directory without the dependency from the first command. Answer no, then change into the project root.

## Create the record layout

Preview what FACET will add. Nothing is written yet:

```sh
npx facet init -n
```

Then create the files:

```sh
npx facet init
```

```text
=== Done — 22 file(s) written, 0 kept ===

Still to do (these need judgement, not copying):
```

`init` keeps every file the project already has, so running it twice is safe. It leaves two things to you: the first record, which is the rest of this guide, and `.facetignore`, which lists what the anchor scan never needs to open. Add the lint suite that `init` just wrote, so its own self-test is not reported as a test file without a record:

```sh
printf 'tools/facet-lint/\n' >> .facetignore
```

Add media, fonts and build output of your project the same way, one pattern per line, as in `.gitignore`.

## Find out what the code really does

Before writing anything down, read the code that decides. In the example shop it is `src/checkout/discount.ts`:

```ts
// The expiry day ends in UTC for every customer. Comparing against local
// midnight made codes expire a day early west of UTC (fixed in 2026-08).
function isExpired(code: DiscountCode, now: Date): boolean {
  return now.getTime() > Date.parse(`${code.expiresOn}T23:59:59.999Z`);
}
```

So all three were partly right. The shop refuses an expired code with error `E2002`. The last day counts until 23:59:59 UTC, not until local midnight. There is no grace period.

Write down what the code does today. If the team wants different behaviour, change the code and the record together, in one pull request.

## Write the answer down as a record

A record is a YAML file under `docs/facets/features/`. Its path follows its id, which has three parts: domain, capability, feature. Create the folder:

```sh
mkdir -p docs/facets/features/checkout/discounts
```

Save this as `docs/facets/features/checkout/discounts/redeem-code.yaml`:

```yaml
schema_version: "0.1"
id: checkout/discounts/redeem-code
lifecycle: active
intent:
  summary: A customer enters a discount code in the cart and gets its discount, or a reason why not.
contract:
  scenarios:
    - id: expired-code-is-refused
      statement: |
        Given the code SPRING5, whose last day is 2026-05-31
        When a customer redeems it after 23:59:59 UTC on that day
        Then the shop refuses it with E2002 and the cart keeps its price
    - id: last-day-ends-in-utc
      statement: |
        Given a code whose last day is today
        When a customer redeems it at 23:30 UTC, in any time zone
        Then the discount is applied
    - id: minimum-order-value
      statement: |
        Given a code with a minimum order value
        When the cart subtotal is below that value
        Then the shop refuses the code with E2003
    - id: one-code-per-order
      statement: |
        Given a cart that already has a code
        When the customer enters a second code
        Then the shop refuses it with E2004
verification:
  unit_tests:
    - id: tests/discount.test.ts
      proves: [expired-code-is-refused, last-day-ends-in-utc, minimum-order-value, one-code-per-order]
lineage:
  owner:
    team: checkout
  created_at: "2026-11-22"
```

- **`lifecycle: active`** says the feature exists in the code today. FACET then expects to find that code.
- **`contract.scenarios`** are the answers to the colleague's question, one case each, in words that support and product can read.
- **`verification`** names the test file that checks these cases. List a scenario under `proves` only after reading the test body: FACET checks that the scenario ids exist, not what the test does.
- **`lineage`** says which team answers questions about the feature and since when the record exists. Use today's date.

## Let the check find the missing link

Run the check:

```sh
npx facet lint
```

```text
error: docs/facets/features/checkout/discounts/redeem-code.yaml: active record "checkout/discounts/redeem-code" has no @facet: anchor in source
warning: tests: 2 test file(s) in tests are named by no record: invoice.test.ts, refund.test.ts

1 record(s), 0 anchor(s) checked — 1 error(s), 1 warning(s) (unclaimed-tests 1)
```

The error is expected. The record says the feature is active, and no source file claims to implement it yet. The command exits with status 1, which is what fails a pull request later.

## Connect the record to the code

In `src/checkout/discount.ts`, add two comment lines directly above `redeemDiscountCode`, the function that redeems a code:

```ts
// @facet: checkout/discounts/redeem-code
// @facet-role: primary
/** Redeems a code on a cart. Returns the new cart; throws a ShopError and leaves the cart unchanged otherwise. */
export function redeemDiscountCode(cart: Cart, input: string, now = new Date()): Cart {
```

`@facet:` names the record. `@facet-role: primary` marks this as the main place the feature lives; other files can carry the same id without that line. Run the check again:

```sh
npx facet lint
```

```text
1 record(s), 1 anchor(s) checked — 0 error(s), 1 warning(s) (unclaimed-tests 1)
```

The remaining warning lists test files that no record claims yet. Warnings never fail the check. Each of those files is a candidate for the next record.

## Commit it and let every pull request check it

Commit the record, the anchor and what `init` wrote:

```sh
git add .facetignore .github docs tools package.json package-lock.json src/checkout/discount.ts
git commit -m "Describe discount-code redemption in a FACET record"
```

`init` also wrote `.github/workflows/facet-lint.yml`. On GitHub it runs `facet lint` on every pull request, with the FACET version pinned in `tools/facet-lint/facet`. If someone renames the function's file and drops the comment, or deletes the record, that pull request fails with the same error you saw in step 5.

Next time the question comes up in the chat, the answer is a link to `docs/facets/features/checkout/discounts/redeem-code.yaml`.

> **What this does not do**
>
> `facet lint` checks that records are well-formed and that records and code point at each other. It does not run your
> tests and does not prove that the code behaves as the scenarios say. That is still your tests' job, and the record
> names which test file covers which scenario.

## If the check says something else

- **`@facet: tag references "…", which has no record`:** the id in the comment and the `id:` in the record differ. Copy the id from the record into the comment.
- **`has no @facet: anchor in source`, although you added the comment:** check that the file is not excluded by a pattern in `.facetignore` or `.gitignore`. FACET does not scan ignored files.
- **`lineage: missing property 'created_at'`, or another field name:** the record does not match the schema `init` wrote to `docs/facets/schema/`. Compare it with the example in step 4.
- **`is anchored to …, which is reachable only from tests`:** the anchored function is called only from test files. Put the anchor on the code the product actually runs, or set `lifecycle: planned` while the feature is still being built.
- **npm asks to install `facet`:** run the command from the project root, where step 1 installed `facet-framework`.

For more on why teams keep feature behaviour next to the code, read [“It’s done.” Does it do what we wanted?](https://facet-framework.com/en/solutions/product-management/).

### Sources

- [FACET Core: quick start and minimal example](https://github.com/facet-framework/core#quick-start)
- [Where each language puts its `@facet:` anchor](https://github.com/facet-framework/core/blob/main/docs/ANCHOR-GUIDE.md)
- [How to write a record](https://github.com/facet-framework/core/blob/main/docs/AUTHORING.md)
