Norway


Here’s a possibility! Perhaps you are testing your JavaScript with a framework like Jasmine. That’s nice because you can write lots of tests to cover your application, get a nice little UI to see the output, and even integrate it with build and deploy tools to make your ongoing work safer.

Now, perhaps there is this zany developer on your team who keeps changing on you — quite literally breaking things in the process. You decide to write a that hits those and makes sure you’re getting back from it what you expect. Straightforward enough. The only slightly tricky part is that requests are async. To really test it, the test needs to have some way to wait for the results before testing the expectations.

That can be handled in Jasmine through a beforeEach(), which can wait to complete until you call a done() function. Here’s the whole thing:

See the Pen
Test Endpoint with Jasmine
by Chris Coyier (@chriscoyier)
on CodePen.

Here’s largely the same thing but with Mocha/Chai:

See the Pen
Test Endpoint with Mocha/Chai
by Chris Coyier (@chriscoyier)
on CodePen.



Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here