From 4a7b8157b5585cb62e77e9e3c66d6af3aaf3a448 Mon Sep 17 00:00:00 2001 From: Colin Ihrig Date: Sat, 4 Jan 2025 13:30:04 -0500 Subject: [PATCH] test_runner: add assert.register() API This commit adds a top level assert.register() API to the test runner. This function allows users to define their own custom assertion functions on the TestContext. Fixes: https://github.com/nodejs/node/issues/52033 PR-URL: https://github.com/nodejs/node/pull/56434 Reviewed-By: Jacob Smith Reviewed-By: Matteo Collina Reviewed-By: Pietro Marchini --- doc/api/test.md | 23 +++++++ lib/internal/test_runner/assert.js | 50 +++++++++++++++ lib/internal/test_runner/test.js | 52 ++++++--------- lib/test.js | 12 ++++ .../parallel/test-runner-custom-assertions.js | 63 +++++++++++++++++++ 5 files changed, 166 insertions(+), 34 deletions(-) create mode 100644 lib/internal/test_runner/assert.js create mode 100644 test/parallel/test-runner-custom-assertions.js diff --git a/doc/api/test.md b/doc/api/test.md index 7deac4fae1efdf..c3ea68fa4173bf 100644 --- a/doc/api/test.md +++ b/doc/api/test.md @@ -1750,6 +1750,29 @@ describe('tests', async () => { }); ``` +## `assert` + + + +An object whose methods are used to configure available assertions on the +`TestContext` objects in the current process. The methods from `node:assert` +and snapshot testing functions are available by default. + +It is possible to apply the same configuration to all files by placing common +configuration code in a module +preloaded with `--require` or `--import`. + +### `assert.register(name, fn)` + + + +Defines a new assertion function with the provided name and function. If an +assertion already exists with the same name, it is overwritten. + ## `snapshot`