From 0d2d0bb1dbed008ec184a02666912d563880ff06 Mon Sep 17 00:00:00 2001 From: Aalhad Date: Tue, 4 Aug 2026 06:01:16 +0530 Subject: [PATCH] doc(test-runner): add example for mocking errors Add an example demonstrating how to mock a function that throws an error and verify it using assert.throws. Fixes: https://github.com/nodejs/node/issues/52357 Co-Authored-By: Claude --- doc/api/test.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/doc/api/test.md b/doc/api/test.md index 24dbd5ec5cd3..27e3ac0b246d 100644 --- a/doc/api/test.md +++ b/doc/api/test.md @@ -2601,6 +2601,19 @@ test('mocks a counting function', (t) => { }); ``` +The following example creates a mock function that throws an error and uses +[`assert.throws`][] to verify it. + +```js +test('mocks a function that throws an error', (t) => { + const fn = t.mock.fn(() => { + throw new Error('mocked error'); + }); + + assert.throws(fn, /mocked error/); +}); +``` + ### `mock.getter(object, methodName[, implementation][, options])`