From bd24e6244b43fe34bdb035226da62dc7025e7acb Mon Sep 17 00:00:00 2001 From: Moshe Atlow Date: Fri, 1 Jul 2022 10:32:10 +0300 Subject: [PATCH 1/3] assert: callTracker throw a specific error message when possible --- lib/internal/assert/calltracker.js | 15 ++++++---- .../test-assert-calltracker-verify.js | 29 ++++++++++++++----- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/lib/internal/assert/calltracker.js b/lib/internal/assert/calltracker.js index f00f2e33271980..2ab0f0e5da6942 100644 --- a/lib/internal/assert/calltracker.js +++ b/lib/internal/assert/calltracker.js @@ -88,12 +88,17 @@ class CallTracker { verify() { const errors = this.report(); - if (errors.length > 0) { - throw new AssertionError({ - message: 'Function(s) were not called the expected number of times', - details: errors, - }); + if (!errors.length) { + return; } + let message = 'Function(s) were not called the expected number of times'; + if (errors.length === 1) { + message = errors[0].message; + } + throw new AssertionError({ + message, + details: errors, + }); } } diff --git a/test/parallel/test-assert-calltracker-verify.js b/test/parallel/test-assert-calltracker-verify.js index 75d20bf9c49c38..3856668830edb0 100644 --- a/test/parallel/test-assert-calltracker-verify.js +++ b/test/parallel/test-assert-calltracker-verify.js @@ -6,27 +6,42 @@ const assert = require('assert'); const tracker = new assert.CallTracker(); -const msg = 'Function(s) were not called the expected number of times'; +const generic_msg = 'Function(s) were not called the expected number of times'; function foo() {} +function bar() {} + const callsfoo = tracker.calls(foo, 1); +const callsbar = tracker.calls(bar, 1); -// Expects an error as callsfoo() was called less than one time. +// Expects an error as callsfoo() and callsbar() were called less than one time. assert.throws( () => tracker.verify(), - { message: msg } + { message: generic_msg } ); callsfoo(); -// Will throw an error if callsfoo() isn't called exactly once. -tracker.verify(); +// Expects an error as callsbar() was called less than one time. +assert.throws( + () => tracker.verify(), + { message: 'Expected the bar function to be executed 1 time(s) but was executed 0 time(s).' } +); callsfoo(); -// Expects an error as callsfoo() was called more than once. +// Expects an error as callsfoo() was called more than once and callsbar() was called less than one time. +assert.throws( + () => tracker.verify(), + { message: generic_msg } +); + +callsbar(); + + +// Expects an error as callsfoo() was called more than once assert.throws( () => tracker.verify(), - { message: msg } + { message: 'Expected the foo function to be executed 1 time(s) but was executed 2 time(s).' } ); From fe5a34dfd2e55cc76214f309f5a34452bc88b252 Mon Sep 17 00:00:00 2001 From: Moshe Atlow Date: Tue, 5 Jul 2022 00:11:23 +0300 Subject: [PATCH 2/3] CR --- lib/internal/assert/calltracker.js | 9 ++++----- test/parallel/test-assert-calltracker-verify.js | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/internal/assert/calltracker.js b/lib/internal/assert/calltracker.js index 2ab0f0e5da6942..cf53b1af10fe28 100644 --- a/lib/internal/assert/calltracker.js +++ b/lib/internal/assert/calltracker.js @@ -88,13 +88,12 @@ class CallTracker { verify() { const errors = this.report(); - if (!errors.length) { + if (errors.length === 0) { return; } - let message = 'Function(s) were not called the expected number of times'; - if (errors.length === 1) { - message = errors[0].message; - } + const message = errors.length === 1 ? + errors[0].message : + 'Functions were not called the expected number of times'; throw new AssertionError({ message, details: errors, diff --git a/test/parallel/test-assert-calltracker-verify.js b/test/parallel/test-assert-calltracker-verify.js index 3856668830edb0..4dd0fd0579d906 100644 --- a/test/parallel/test-assert-calltracker-verify.js +++ b/test/parallel/test-assert-calltracker-verify.js @@ -6,7 +6,7 @@ const assert = require('assert'); const tracker = new assert.CallTracker(); -const generic_msg = 'Function(s) were not called the expected number of times'; +const generic_msg = 'Functions were not called the expected number of times'; function foo() {} From f8782981567167ce5a7f0a2ed2de5318c1953bb2 Mon Sep 17 00:00:00 2001 From: Moshe Atlow Date: Tue, 5 Jul 2022 11:58:52 +0300 Subject: [PATCH 3/3] CR --- test/parallel/test-assert-calltracker-verify.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/test/parallel/test-assert-calltracker-verify.js b/test/parallel/test-assert-calltracker-verify.js index 4dd0fd0579d906..118f04f7352780 100644 --- a/test/parallel/test-assert-calltracker-verify.js +++ b/test/parallel/test-assert-calltracker-verify.js @@ -28,16 +28,22 @@ assert.throws( () => tracker.verify(), { message: 'Expected the bar function to be executed 1 time(s) but was executed 0 time(s).' } ); +callsbar(); + +// Will throw an error if callsfoo() and callsbar isn't called exactly once. +tracker.verify(); + +const callsfoobar = tracker.calls(foo, 1); callsfoo(); -// Expects an error as callsfoo() was called more than once and callsbar() was called less than one time. +// Expects an error as callsfoo() was called more than once and callsfoobar() was called less than one time. assert.throws( () => tracker.verify(), { message: generic_msg } ); -callsbar(); +callsfoobar(); // Expects an error as callsfoo() was called more than once