2 Test server endpoint
Josh Callebaut edited this page 2016-04-21 14:29:49 -07:00

Problem

You want to write tests for a server endpoint

Solution

Put tests which target the endpoint as a whole in /spec/server/functional. Use utility functions to clear models from the test db, create users, and so on. Use utils.wrap to perform asynchronous actions with yield and keep your tests flat.

Details

Test Data

Tests are run within the same process as the server, so tests can go in and use the same tools as the server and spy and mock as desired. It runs on top of a separate mongo, which is cleared at process start, so you'll need to create your own data for each test.

Note that the db is not cleared between each separate test during a run. Legacy tests depend on one another. Once all tests are refactored to be standalone, test will always start with a clear db by default. In the meantime, you'll need to clear relevant collections manually before each test.

Globals

Globals are defined in spec/server/common.coffee. The most important ones are listed here.

The global getURL function takes the path and turns it into an absolute url pointing to the test server.

url = getURL('/db/article') # returns 'http://localhost:3001/db/article'

Most, if not all, models are global. Check common.coffee for a full list.

request is global. Be sure to use it, it has {jar:true} as a default option so that one request to the next maintains the same cookies. It also has been promisified by bluebird, so that it has functions such as getAsync and postAsync for your yielding needs.

Co

utils.wrap is based on co-express, a succinct and elegant method which ties express and co together. utils.wrap ties co and jasmine together so that the function that is wrapped can throw errors and has this set to the jasmine test object. The vast majority of yield will either use request which has been promisified, or Mongoose whose functions already return promises.

it 'is a jasmine test', utils.wrap (done) ->
  [res, body] = yield request.getAsync({uri: getURL('/db/article'), json: true})
  users = yield User.find({...})
  expect(...).toBe(...)
  done()

Don't forget to use utils.wrap and yield in your tests, or they won't run properly.

Resources