Assertion Syntax¶
readme-assert transforms special comments in your code blocks into
assertion calls. Primitives use assert.strictEqual(), while objects and
arrays use assert.deepStrictEqual(). Error assertions use assert.throws().
Equality¶
Use //=> after an expression to assert its value:
The // → (unicode arrow) and // -> (ascii arrow) variants also work:
Throws¶
Assert that an expression throws using // throws with a regex pattern:
Or use //=> with an error name and optional message to match both:
This generates assert.throws(() => { expr; }, { name: "TypeError", message: "bad input" }).
The message can also be a regex:
You can omit the message to match only the error name:
When the expression uses await, the assertion is automatically promoted
to assert.rejects with an async wrapper:
Console Output¶
Assert console output — the call is preserved and an assertion is added:
Resolves¶
Since await returns the resolved value, you can assert it directly:
Or use the explicit resolves to form without await:
The to is optional:
This generates assert.strictEqual(await expr, value) for primitives, or
assert.deepStrictEqual(await expr, value) for objects and arrays.
Rejects¶
Assert that a Promise rejects matching a pattern:
This generates await assert.rejects(() => expr, /pattern/).
Or match the error name and message with //=> rejects:
The message can also be a regex:
Await expressions work the same way — // throws and // rejects are
both promoted to async rejects automatically:
What Gets Generated¶
Given:
readme-assert generates: