From 08d6bd1b6eae8f5bf9620909b3345193adbadf35 Mon Sep 17 00:00:00 2001 From: nthbotast Date: Sun, 8 Mar 2026 16:28:54 +0100 Subject: [PATCH] doc(test_runner): add mock method throws example Signed-off-by: nthbotast --- doc/api/test.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/doc/api/test.md b/doc/api/test.md index c526aec69735d9..3fb0358f4b06a0 100644 --- a/doc/api/test.md +++ b/doc/api/test.md @@ -2460,6 +2460,29 @@ test('spies on an object method', (t) => { }); ``` +The same approach can be used to mock a method that throws and verify the +error with [`assert.throws()`][]: + +```js +test('mocks a method that throws', (t) => { + const number = { + value: 5, + subtract(a) { + return this.value - a; + }, + }; + + t.mock.method(number, 'subtract', () => { + throw new Error('boom'); + }); + + assert.throws(() => number.subtract(3), { + message: 'boom', + }); +}); +``` + + ### `mock.module(specifier[, options])`