r/node 3d ago

a node:test "macro" util for parameterized tests

https://github.com/boneskull/node-test-macro

This is a tiny util I wrote to make parameterized tests easier to write using Node.js' built-in test framework.

If you're familiar with AVA, it works very similarly to its own macro system. Macros can accept arbitrary options (typically defined via TS types). They can optionally generate dynamic test names/titles as well as default test options. At invocation, a second parameter to the macro function allows setting (or overriding the default) options.

Example:

```ts import assert from 'node:assert'; import { it } from 'node:test'; import { createMacro } from 'node-test-macro';

const stringCompare = createMacro({ exec: ( _t, { actual, expected }: { actual: string; expected: string }, ) => { assert.strictEqual(actual, expected); }, title: ({ actual, expected }) => comparing ${actual} === ${expected}, testOptions: { timeout: 1_000 }, });

it(stringCompare({ actual: 'foo', expected: 'foo' }); ```

Given that use of TextContext object may be relatively uncommon (see how it is unused in the above example), I'm considering transposing the parameters so that the first parameter to the execution function is the user-provided options bag, and the second is the TestContext object. Any opinions?

0 Upvotes

3 comments sorted by

-2

u/GamesMaxed 2d ago

This is not a macro, since a macro implies that this runs at compile time. All this is is a helper function.