Writing the spec

With the server running, open your browser at http://localhost:8000/SpecRunner.html to see the results of our specs.

You can see that even though the server is running, and the spec appears to be correct, it is failing. This is due to the fact that stock.fetch() is asynchronous. A call to stock.fetch() returns immediately, allowing Jasmine to run the expectations before the AJAX request is completed:

it("should update its share price", function() {
  expect(stock.sharePrice).toEqual(20.18);
});

To fix this, we need to embrace the asynchronicity of the stock.fetch() function and instruct Jasmine to wait for its execution before running the expectations.

Asynchronous setups and teardowns

In the example shown, we invoke the fetch function ...

Get Jasmine JavaScript Testing - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.