Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Jasmine does not reset spy after each test suite #267

sourabhchaki opened this Issue Aug 10, 2012 · 2 comments


None yet
3 participants

I have the following spec.

describe("SN.ExitHistory", function() {

var exitHistory;

beforeEach(function() {

SN.Utils = jasmine.createSpy("utils").andCallFake(function() {
function readSNCookie(cookieName, key) {
return "google.com";

function isUndefinedOrNull(param) {
return (param == null) || (param === "null");

function createSNCookie(snCookieName, key, value, lifeTime) {


var me = {
readSNCookie : readSNCookie,
isUndefinedOrNull : isUndefinedOrNull,
createSNCookie : createSNCookie
return me;

exitHistory = SN.ExitHistory();


it("return last exit link", function() {


exitHistory.getLastExitLink internally uses SN.Utils.

After the test is done Jasmine does not remove the spy object utils. In next test suite also I can see the same utils present. Is there any way to reset the spy object after each test is done?

Instead of creating spy, if I create a new object for utils, behavior is same. Then what is the difference between a spy and actual object in this scenario.

Correct me if I am wrong.

I can't speak to the spy teardown, which should be happening, but in this exact case it looks like there is no reason to use a jasmine spy, though I'm not clear on exactly what you're trying to test. Perhaps you would just want to do something like this: spyOn(SN.Utils, 'readSNCookie').andReturn('google.com');.

Stubbing the other methods seems unnecessary, unless that code isn't loaded for this test suite.


infews commented Aug 14, 2012

This issue is that you're creating a spy as its own variable. These are NOT torn down between tests. But they should fall out of scope depending on how they are declared.

If you create an object and then spy on a function property of that object, we do tear down the spy.

But I agree with Luke, that you should probably test what you're doing differently.

Closing for now as this is a tutorial issue, not a Jasmine core issue.

Thanks for using Jasmine.

@infews infews closed this Aug 14, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment