title: Why is Jasmine not resetting a spy when spying on $.ajax?
body: <p>I am trying to spy on <code>$.ajax</code> in Jasmine 2.0 tests. Here is a simplified example (TypeScript) showing my scenario:</p>
  
  <pre><code>describe("first test", () =&gt; {
      var deferred = jQuery.Deferred();
      spyOn($, "ajax").and.callFake((uri: string, settings: JQueryAjaxSettings) =&gt; {
          return deferred.resolve("ThisIsADummyResult");
      });
  
      it("should return dummy result", done =&gt; {
          $.ajax("http://somedummyserver.net").then(result =&gt; {
              expect(result).toBe("ThisIsADummyResult");
              done();
          });
      });
  });
  
  describe("second test", () =&gt; {
      var deferred = jQuery.Deferred();
      spyOn($, "ajax").and.callFake((uri: string, settings: JQueryAjaxSettings) =&gt; {
          return deferred.resolve("ThisIsAnotherResult");
      });
  
      it("should return another result", done =&gt; {
          $.ajax("http://somedummyserver.net").then(result =&gt; {
              expect(result).toBe("ThisIsAnotherResult");
              done();
          });
      });
  });
  </code></pre>
  
  <p><code>firstTest</code> as well as <code>second test</code> work if I run them alone. However, if I run both tests as shown above, I get the following error message: <code>ajax has already been spied upon</code>.</p>
  
  <p>So my questions are:</p>
  
  <ul>
  <li>Shouldn't the spies be reset by Jasmine after each test automatically? Why doesn't that work in my case?</li>
  <li>Is there another way of using <code>spyOn</code> which makes Jasmine reset the spies?</li>
  <li>How can I manually reset the spies?</li>
  </ul>
  
  <p><strong>Update:</strong> I continued experimenting and found a possible solution myself. If I set up the spies inside of the <code>it</code> spec, both tests run fine. Here is the code for <code>first test</code> showing what I mean:</p>
  
  <pre><code>describe("first test", () =&gt; {
      it("should return dummy result", done =&gt; {
          var deferred = jQuery.Deferred();
          spyOn($, "ajax").and.callFake((uri: string, settings: JQueryAjaxSettings) =&gt; {
              return deferred.resolve("ThisIsADummyResult");
          });
  
          $.ajax("http://somedummyserver.net").then(result =&gt; {
              expect(result).toBe("ThisIsADummyResult");
              done();
          });
      });
  });
  </code></pre>
  
  <p>Still, it would be very interesting why the first version does not work. Why does Jasmine not reset the spies in the first version whereas it does in the second one?</p>