0

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: 'me@example.co.uk',
          firstName: 'Dave',
          lastName: 'Smith',
          roles: ['ADMIN', 'STANDARD'],
        }, ],
      })
      .expect(200, done);
  });
});

Any help in understanding appreciated.

Steerpike
  • 1,712
  • 6
  • 38
  • 71

1 Answers1

0

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: 'me@example.co.uk',
          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.

Freezy
  • 108
  • 4
  • thank you for your suggestion. This makes sense. I have re-modelled but now get a "ReferenceError: describe is not defined" error. So, I suspect I have some configuration issue with the placement of this test within WebStorm now. It is seen as a Mocha file, but is not placed under a /test sub folder. I am debugging using the IDE. Perhaps I need to look at my edit configurations in the IDE – Steerpike Dec 16 '18 at 15:24
  • ok, I git the Mocha config working so it now sees it as a bona fide Mocha test - but with the same behaviour, even with your modified code. I'm pretty sure this is to do with failing to call .then() or done() - what do you think? – Steerpike Dec 16 '18 at 15:34
  • I've tried a new test, ripping out the express stuff because I'm testing against a real API but it still simply skips over the test once it hits the 'it' :( – Steerpike Dec 16 '18 at 17:20
  • Can you post your updated code on here so I can take a look? – Freezy Dec 18 '18 at 11:12