Current Project: wordapi.net.

Today I wrote some more API tests. I validated the data being requested by the /api/list route which expects either one of the following parameters:

  • length
  • prefix
  • suffix

The test checks for a 500 status when none of the parameters have been entered:

it('should send a 500 status when no required params are passed', (done) => {
  request(app)
    .get('/api/list?dummy=1')
    .expect(500, done);
});

The data being returned should be an object containing the following properties:

{
  "wordsFound": 0,
  "wordList": []
}

I created four separate tests to validate the result:

  1. it should return JSON
  2. it should return an object with properties: wordsFound and wordList
  3. it should return the number of words found as a Number
  4. it should return the words found as an Array

To check that the result was an object, I used the .to.have.keys method (note I am using Chai) as the assertion library):

expect(res.body).to.have.keys(['wordList', 'wordsFound']);

To validate the format of the individual properties returned, I used the following chains:

// expect a number
expect(res.body.wordsFound).to.be.a('number');

// expect an array
expect(res.body.wordList).to.be.a('array');

The full test list is shown below:

describe('GET /api/list', () => {
    it('should send a 500 status when no required params are passed', (done) => {
      request(app)
        .get('/api/list?dummy=1')
        .expect(500, done);
    });

    it('should respond with JSON', (done) => {
      request(app)
        .get('/api/list?length=4')
        .expect('Content-Type', /json/)
        .expect(200, done);
    });

    it('should return an object with properties: wordsFound and wordList', (done) => {
      request(app)
        .get('/api/list?length=4')
        .expect(200)
        .end((err, res) => {
          if(err) {
            throw(err);
          }

          expect(res.body).to.have.keys(['wordList', 'wordsFound']);
          done();
        });
    });

    it('should return the number of words found as a Number', (done) => {
      request(app)
        .get('/api/list?length=4&prefix=tree')
        .expect(200)
        .end((err, res) => {
          if(err) {
            throw(err);
          }
          expect(res.body.wordsFound).to.be.a('number');
          done();
        });
    });


    it('should return the words found as an Array', (done) => {
      request(app)
        .get('/api/list?length=4&prefix=tree')
        .expect(200)
        .end((err, res) => {
          if(err) {
            throw(err);
          }
          expect(res.body.wordList).to.be.a('array');
          done();
        });
    });
});

I now have passing tests for another route! Tomorrow, I’ll continue testing my app and will start learning more about unit testing.

Thanks for reading! Please ask questions or leave comments in the box below, and if you’d like to follow the project, please click on this link: https://github.com/lyndseybrowning/wordapi.net.