mocha: done is not defined()

Prerequisites

  • Checked that your issue hasn’t already been filed by cross-referencing issues with the faq label
  • Checked next-gen ES issues and syntax problems by using the same environment and/or transpiler configuration without Mocha to ensure it isn’t just a feature that actually isn’t supported in the environment in question or a bug in your code.
  • ‘Smoke tested’ the code to be tested by running it outside the real test suite to get a better sense of whether the problem is in the code under test, your usage of Mocha, or Mocha itself
  • Ensured that there is no discrepancy between the locally and globally installed versions of Mocha. You can find them with: node node_modules/.bin/mocha --version(Local) and mocha --version(Global). We recommend avoiding the use of globally installed Mocha.

Description

Steps to Reproduce

Expected behavior: [What you expect to happen]

Actual behavior: [What actually happens]

Reproduces how often: [What percentage of the time does it reproduce?]

Versions

  • The output of mocha --version and node node_modules/.bin/mocha --version:
  • The output of node --version:
  • The version and architecture of your operating system:
  • Your shell (bash, zsh, PowerShell, cmd, etc.):
  • Your browser and version (if running browser tests):
  • Any other third party Mocha related modules (with versions):
  • The code transpiler being used:

Additional Information

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 16 (5 by maintainers)

Most upvoted comments

It would appear this is what you meant to do… redone as Promise-based test.

“subscribe-promise.spec.js”

const http = require('http');
const cassandra = require('cassandra-driver');
const request = require('request');
request.debug = true;    // :DEBUG:

const telemetryUrl = 'https://mytelemetryserver.example.com/';
const dbAuthority = '127.0.0.1:9042';
let subscriptionId;
let client;

before(() => {
  subscriptionId = '123456';

  console.log('connecting to db cluster...');
  const clientOptions = {
    contactPoints: [dbAuthority], 
    authProvider: new cassandra.auth.PlainTextAuthProvider('cassandra', 'cassandra')
  };
  client = new cassandra.Client(clientOptions);
});

it('should fetch subscription and display corresponding db data', () => {
  let fetchPromise = new Promise((resolve, reject) => {
    console.log('requesting subscription for ' + subscriptionId + '...');
    const requestOptions = {
      json: true,
      url: telemetryUrl + 
              '/graph/v1/services/telemetry/subscription/' +
              subscriptionId
    };
    request.get(requestOptions, (error, response, body) => {
      if (error) {
        console.error('request: error:', error.message);
        if (error.code === 'ETIMEDOUT') {
          console.error(error.connect === true
                          ? 'timed out attempting to establish connection'
                          : 'timed out awaiting response from server');
        }
        return reject(error);
      }
      if (response.statusCode !== 200) {
        let description = http.STATUS_CODES[response.statusCode];
        console.error('request: statusCode:', response.statusCode, '(' + description + ')');
        return reject(new Error('unexpected response statusCode ' + response.statusCode));
      }
      console.log('response body:', '\n', JSON.stringify(response.body, null, 2), '\n');
      let tenantId = response.body['tenantId'];
      console.log('tenantId:', JSON.stringify(tenantId));
      if (!tenantId) {
        return reject(new TypeError('expected tenantId value to be truthy'));
      }
      return resolve(tenantId);
    });
  });

  fetchPromise.then((tenantId) => {
    if (!tenantId) {
      throw new TypeError('tenantId value must be truthy'));
    }
    let query = 'SELECT * FROM telemetry_tenant_registration WHERE tenant_id=? ALLOW FILTERING';
    console.log('querying db for tenant ' + tenantId + '...');
    client.execute(query, [tenantId])
      .then((result) => {
        console.log('db responded...');
        expect(result.rows[0].tenant_id,
               tenantId,
               'Unable to find tenantID in db');
      });
  });
});