From ac041a20ea001d40ebac7be0f1fd945e4260dd1b Mon Sep 17 00:00:00 2001 From: KJyang-0114 Date: Wed, 4 Mar 2026 00:50:12 +0800 Subject: [PATCH 1/3] test_runner: print failed coverage reports with dot reporter When coverage threshold is not met, the dot reporter now outputs the error message so users can understand why tests failed. Refs: #60884 --- lib/internal/test_runner/reporter/dot.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/internal/test_runner/reporter/dot.js b/lib/internal/test_runner/reporter/dot.js index 45ff047bc4e5a0..974842505bec19 100644 --- a/lib/internal/test_runner/reporter/dot.js +++ b/lib/internal/test_runner/reporter/dot.js @@ -18,6 +18,10 @@ module.exports = async function* dot(source) { yield `${colors.red}X${colors.reset}`; ArrayPrototypePush(failedTests, data); } + if (type === 'test:diagnostic') { + const levelColor = data.level === 'error' ? colors.red : data.level === 'warning' ? colors.yellow : colors.white; + yield `\n${levelColor}${data.message}${colors.reset}\n`; + } if ((type === 'test:fail' || type === 'test:pass') && ++count === columns) { yield '\n'; From d42834148c5c15c060a841a4d0a550a02dd2db25 Mon Sep 17 00:00:00 2001 From: KJyang-0114 Date: Sun, 8 Mar 2026 23:05:05 +0800 Subject: [PATCH 2/3] docs: add example for mocking functions that throw Add documentation explaining how to mock functions that throw errors and validate them using assert.throws(). Includes examples showing how mock tracks both successful calls and thrown errors. PR-URL: https://github.com/nodejs/node/pull/52358 Refs: https://github.com/nodejs/node/issues/52357 --- doc/api/test.md | 63 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/doc/api/test.md b/doc/api/test.md index 76ffe2f215af90..f5fcb59c73dd76 100644 --- a/doc/api/test.md +++ b/doc/api/test.md @@ -925,6 +925,69 @@ test('mocks setTimeout to be executed synchronously without having to actually w }); ``` +### Mocking functions that throw + +The mock function API allows mocking functions that throw errors and validating +them using `assert.throws()`. This is useful for testing error handling code paths. + +```mjs +import assert from 'node:assert'; +import { mock, test } from 'node:test'; + +test('mocks a function that throws', () => { + const fn = mock.fn(() => { + throw new Error('mock error'); + }); + + // The function throws when called + assert.throws(() => fn(), { message: 'mock error' }); +}); +``` + +```cjs +const assert = require('node:assert'); +const { mock, test } = require('node:test'); + +test('mocks a function that throws', () => { + const fn = mock.fn(() => { + throw new Error('mock error'); + }); + + // The function throws when called + assert.throws(() => fn(), { message: 'mock error' }); +}); +``` + +When using the spy functionality, the mock tracks both successful calls and +thrown errors: + +```mjs +import assert from 'node:assert'; +import { mock, test } from 'node:test'; + +test('mocks track both results and errors', () => { + let callCount = 0; + const fn = mock.fn(() => { + callCount++; + if (callCount === 2) { + throw new Error('error on second call'); + } + return 'success'; + }); + + // First call succeeds + assert.strictEqual(fn(), 'success'); + + // Second call throws + assert.throws(() => fn(), { message: 'error on second call' }); + + // Check mock metadata + assert.strictEqual(fn.mock.callCount(), 2); + assert.strictEqual(fn.mock.calls[0].result, 'success'); + assert.strictEqual(fn.mock.calls[1].error.message, 'error on second call'); +}); +``` + ### Dates The mock timers API also allows the mocking of the `Date` object. This is a From 348329d7d28f1fe95d004916107a1bb0a598c6f5 Mon Sep 17 00:00:00 2001 From: KJyang-0114 Date: Mon, 9 Mar 2026 23:03:07 +0800 Subject: [PATCH 3/3] docs: fix unclosed link in Test Runner documentation Fixes an unclosed link tag that causes all text after it to be underlined and clickable in the HTML documentation. Before: [] After: [RegExp] --- doc/api/test.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/doc/api/test.md b/doc/api/test.md index f5fcb59c73dd76..38cff9c261eb34 100644 --- a/doc/api/test.md +++ b/doc/api/test.md @@ -288,10 +288,10 @@ it('should do the thing', { expectFailure: 'feature not implemented' }, () => { ``` If the value of `expectFailure` is a -[](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) | -[](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) | -[](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) | -[](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error), +[RegExp](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) | +[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) | +[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) | +[Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error), the tests will pass only if they throw a matching value. See [`assert.throws`][] for how each value type is handled.