Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Fix for jasmine.Spy.andCallThrough() and custom function props #258

wants to merge 1 commit into from

3 participants


When a function has object properties, calling spyOn() for that function may break other code that depend on those properties. This fix adds property replication to .andCallThrough(), which is assumed to make the spy "blend in" and follow original function behavior.


This is a known issue with Jasmine spies. There have been a couple of pull requests attempting to solve the problem.

Here's the Tracker story so you can follow its progress:
We expect to refactor spies a bit in an upcoming release and will include something like this feature.

Thanks for the work and thanks for using Jasmine!


This seems like something we could take in advance of any spy refactoring if it checked for the property's existence on the spy itself before overwriting. That would prevent anyone from getting accidentally broken, but provide improved spy functionality.


Closing for now. Please follow Tracker for progress.

@infews infews closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 20, 2012
  1. jasmine.Spy.andCallThrough() no longer breaks original function prope…

    Alexander Tokarev authored
This page is out of date. Refresh to see the latest.
Showing with 13 additions and 0 deletions.
  1. +8 −0 lib/jasmine-core/jasmine.js
  2. +5 −0 spec/core/SpySpec.js
8 lib/jasmine-core/jasmine.js
@@ -312,6 +312,14 @@ jasmine.Spy = function(name) {
jasmine.Spy.prototype.andCallThrough = function() {
this.plan = this.originalValue;
+ // Copy original function properties to the spy
+ for ( var prop in this.originalValue ) {
+ if ( this.originalValue.hasOwnProperty(prop) ) {
+ this[prop] = this.originalValue[prop];
+ };
+ };
return this;
5 spec/core/SpySpec.js
@@ -50,9 +50,14 @@ describe('Spies', function () {
return "return value from original function";
+ TestClass.someFunction.prop1 = 'foo';
+ TestClass.someFunction.prop2 = { bar: 'baz' };
this.spyOn(TestClass, 'someFunction').andCallThrough();
var result = TestClass.someFunction('arg1', 'arg2');
+ expect(TestClass.someFunction.prop1).toBe('foo');
+ expect(TestClass.someFunction.prop2).toEqual({ bar: 'baz' });
expect(result).toEqual("return value from original function");
expect(passedArgs).toEqual(['arg1', 'arg2']);
Something went wrong with that request. Please try again.