O'Reilly logo

CoffeeScript, 2nd Edition by Trevor Burnham

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

Mock-Testing a Node Server with Supertest

In the previous section, we tested each of our Backbone entities against a mock server, using Sinon to fake responses to each collection’s HTTP requests. Now it’s time to create the mirror image of those tests, testing that our server provides the correct response to a set of fake requests.

Before we start writing tests, though, we should refactor our server to make it test-friendly. As we wrote it originally, it would define an Express instance (called app) and then immediately start listening on a port. For testing, we don’t want to actually open a TCP port. It’s much easier to simulate our requests. So let’s separate the code that defines app from the code that tells app to start listening:

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