Search code examples
javascriptnode.jsmocha.jssupertest

Mocha skipping over test


beginner here (Node, JS) trying to understand why Mocha is skipping over my test. I realise I am using using request / supertest libraries sub-optimally, but I just want to understand why, when it hit the 'it' in debugging, it simply skips to the closing bracket of the 'describe' block without running the code within:

const request = require('supertest')('https://my-app123.com');
const createJWT = require('../../lib/createApp/createJWT');
const app = require('./app');
let jwt;

describe('App creation', () => {
  it('should create new app', function(done) {
    jwt = createJWT();

    request
      .post('/v1/home')
      .set('Content-Type', 'application/json')
      .set('Authorization', `Bearer ${jwt}`)
      .send({
        name: 'Test',
        organisation: 'Test Inc.',
        objectionProcessingDefault: 'auto-uphold',
        users: [{
          email: '[email protected]',
          firstName: 'Dave',
          lastName: 'Smith',
          roles: ['ADMIN', 'STANDARD'],
        }, ],
      })
      .expect(200, done);
  });
});

Any help in understanding appreciated.


Solution

  • Try to let Nodejs evaluate the promise first before comparing. For example, it should be

    const api = request('https://123-api.myapplication.io', {
      json: true
    }, (err, res, body) => {
    
      if (err) {
        return console.log(err);
      }
      console.log(body.url);
      console.log(body.explanation);
    
    });
    
    describe('POST /v1/creation', () => {
      it('should return a 200', async() => {
        const app = api();
    
        let jwt = createJWT();
    
        await (supertest(app)
          .post('/v2/create')
          .set('Content-Type', 'application/json')
          .set('Authorization', `Bearer ${jwt}`)
          .send({
            name: 'Test',
            organisation: 'Test Inc.',
            objectionProcessingDefault: 'auto-uphold',
            users: [{
              email: '[email protected]',
              firstName: 'Bob',
              lastName: 'Smith',
              roles: ['ADMIN', 'AGENT'],
            }, ],
          }))
          .expect(200);
      });
    });

    Also from looking at your code, you might need to set the headers before you making a post request.