O'Reilly logo

Jasmine JavaScript Testing - Second Edition by Paulo Ragonha

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required