From 1058218b8aaae5f399ab9a896d6393caae06c9ae Mon Sep 17 00:00:00 2001 From: Moshe Atlow Date: Tue, 9 Aug 2022 23:03:08 +0300 Subject: [PATCH] CR --- lib/internal/assert/calltracker.js | 7 +++++-- test/parallel/test-assert-calltracker-getCalls.js | 9 +++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/internal/assert/calltracker.js b/lib/internal/assert/calltracker.js index afe70654d53ef4..bac80dc3fd55a5 100644 --- a/lib/internal/assert/calltracker.js +++ b/lib/internal/assert/calltracker.js @@ -2,8 +2,10 @@ const { ArrayPrototypePush, + ArrayPrototypeSlice, Error, FunctionPrototype, + ObjectFreeze, Proxy, ReflectApply, SafeSet, @@ -36,7 +38,8 @@ class CallTrackerContext { } track(thisArg, args) { - ArrayPrototypePush(this.#calls, { thisArg, arguments: args }); + args = ObjectFreeze(ArrayPrototypeSlice(args)); + ArrayPrototypePush(this.#calls, ObjectFreeze({ thisArg, arguments: args })); } get delta() { @@ -47,7 +50,7 @@ class CallTrackerContext { this.#calls = []; } getCalls() { - return this.#calls; + return ObjectFreeze(ArrayPrototypeSlice(this.#calls)); } report() { diff --git a/test/parallel/test-assert-calltracker-getCalls.js b/test/parallel/test-assert-calltracker-getCalls.js index 6758998896a819..243f72c90f1def 100644 --- a/test/parallel/test-assert-calltracker-getCalls.js +++ b/test/parallel/test-assert-calltracker-getCalls.js @@ -28,6 +28,15 @@ describe('assert.CallTracker.getCalls()', { concurrency: true }, () => { assert.throws(() => tracker.getCalls(fn), { code: 'ERR_INVALID_ARG_VALUE' }); }); }); + + it('should return a frozen object', () => { + const fn = tracker.calls(); + fn(); + const calls = tracker.getCalls(fn); + assert.throws(() => calls.push(1), /object is not extensible/); + assert.throws(() => Object.assign(calls[0], { foo: 'bar' }), /object is not extensible/); + assert.throws(() => calls[0].arguments.push(1), /object is not extensible/); + }); }); describe('assert.CallTracker.reset()', () => {