Merge branch 'gh1886-spy-args-deep-clone' of https://github.com/evanwalsh/jasmine

Merges #2062 from @evanwaslh
Fixes #1886
This commit is contained in:
Steve Gravrock
2025-06-05 06:17:02 -07:00
3 changed files with 46 additions and 6 deletions

View File

@@ -2803,7 +2803,7 @@ getJasmineRequireObj().CallTracker = function(j$) {
this.track = function(context) {
if (opts.cloneArgs) {
context.args = j$.util.cloneArgs(context.args);
context.args = opts.argsCloner(context.args);
}
calls.push(context);
};
@@ -2911,13 +2911,15 @@ getJasmineRequireObj().CallTracker = function(j$) {
};
/**
* Set this spy to do a shallow clone of arguments passed to each invocation.
* Set this spy to do a clone of arguments passed to each invocation.
* @name Spy#calls#saveArgumentsByValue
* @since 2.5.0
* @param {Function} [argsCloner] A function to use to clone the arguments. Defaults to a shallow cloning function.
* @function
*/
this.saveArgumentsByValue = function() {
this.saveArgumentsByValue = function(argsCloner = j$.util.cloneArgs) {
opts.cloneArgs = true;
opts.argsCloner = argsCloner;
};
this.unverifiedCount = function() {

View File

@@ -134,6 +134,42 @@ describe('CallTracker', function() {
expect(callTracker.mostRecent().args[1]).toEqual(arrayArg);
});
it('allows object arguments to be deep cloned', function() {
const callTracker = new jasmineUnderTest.CallTracker();
callTracker.saveArgumentsByValue(args => JSON.parse(JSON.stringify(args)));
const objectArg = { foo: { bar: { baz: ['qux'] } } },
arrayArg = ['foo', 'bar'];
callTracker.track({
object: {},
args: [objectArg, arrayArg, false, undefined, null, NaN, '', 0, 1.0]
});
objectArg.foo.bar.baz.push('quux');
expect(callTracker.mostRecent().args[0]).not.toBe(objectArg);
expect(callTracker.mostRecent().args[0]).not.toEqual(objectArg);
expect(callTracker.mostRecent().args[0]).toEqual({
foo: { bar: { baz: ['qux'] } }
});
expect(callTracker.mostRecent().args[1]).not.toBe(arrayArg);
expect(callTracker.mostRecent().args[1]).toEqual(arrayArg);
});
it('can take any function to transform arguments when saving by value', function() {
const callTracker = new jasmineUnderTest.CallTracker();
callTracker.saveArgumentsByValue(JSON.stringify);
const objectArg = { foo: { bar: { baz: ['qux'] } } },
arrayArg = ['foo', 'bar'],
args = [objectArg, arrayArg, false, undefined, null, NaN, '', 0, 1.0];
callTracker.track({ object: {}, args });
expect(callTracker.mostRecent().args).toEqual(JSON.stringify(args));
});
it('saves primitive arguments by value', function() {
const callTracker = new jasmineUnderTest.CallTracker(),
args = [undefined, null, false, '', /\s/, 0, 1.2, NaN];

View File

@@ -9,7 +9,7 @@ getJasmineRequireObj().CallTracker = function(j$) {
this.track = function(context) {
if (opts.cloneArgs) {
context.args = j$.util.cloneArgs(context.args);
context.args = opts.argsCloner(context.args);
}
calls.push(context);
};
@@ -117,13 +117,15 @@ getJasmineRequireObj().CallTracker = function(j$) {
};
/**
* Set this spy to do a shallow clone of arguments passed to each invocation.
* Set this spy to do a clone of arguments passed to each invocation.
* @name Spy#calls#saveArgumentsByValue
* @since 2.5.0
* @param {Function} [argsCloner] A function to use to clone the arguments. Defaults to a shallow cloning function.
* @function
*/
this.saveArgumentsByValue = function() {
this.saveArgumentsByValue = function(argsCloner = j$.util.cloneArgs) {
opts.cloneArgs = true;
opts.argsCloner = argsCloner;
};
this.unverifiedCount = function() {