1

As you can see in the .gif below, I call tape.js twice in my integration tests and it works fine. But the second time I run the tests, after I've commented out the code to insert test data into my database, the body of the tape.js function never runs.

Why does this happen?

enter image description here

I've put a breakpoint in my database code and it finishes and calls resolve() before the next tape.js test runs so I don't get it.

Here's my test class, followed by database class:

import tape = require('tape');
const testData = require('../../../helper/data');
import * as db from '../../../helper/integration/database';

helper.setTestEnvironmentVariableOn();
const startedServer : hapi.Server = require('../../../../src/app');

(async function runTests() {
  recreateDatabase();
  await db.runQuery(getInsertGetRolesTestPermissionsSql());
  await test_getRoles_returnsAllRoles();

  recreateDatabase();
  await db.runQuery(getInsertGetUsersRolesTestPermissionsSql());
  await test_getUsersRoles_userCanGetHerOwnRoles();
  await startedServer.stop();
})();

function test_getRoles_returnsAllRoles() {
  return new Promise( (resolve, reject) => {
    tape(testeeName + 'GET /roles returns all, and only, Ekaya roles', async (assert) => {
      await helper.createNewUser();
      superagent
      .get(testData.url + `roles`)
      .set('token', testData.token)
      .end( (error: any , result: superagent.Response) => {
        assert.equals(error, null);           
        assert.end();
        resolve();
      });
    });
  });
}

function test_getUsersRoles_userCanGetHerOwnRoles() {
  return new Promise( (resolve, reject) => {
    tape(testeeName + 'GET /users/id/roles gives a user her own roles', async (assert) => {
      const userid = '635de6dc-0df9-43f4-96dc-922bca541515';
      const token = await helper.getTokenForUser(userid);
        superagent
        .get(testData.url + `users/`+userid+`/roles`)
      .set('token', token)
        .end( (error: any , result: superagent.Response) => {
        assert.equals(error, null);                 
            assert.end();
        resolve();
        });
    });
  });
}

function getInsertGetUsersRolesTestPermissionsSql() : string {
  return `
    INSERT INTO enum.permissions(permissions_id, name,...
  `;
}

function getInsertGetRolesTestPermissionsSql() {
  return `
    delete fr...`;
}

Database class:

import * as pg from "pg";
import 'reflect-metadata';
const Config = require('../../../src/config');
const config = new Config();

export function runQuery(sql: string) : Promise<any> {
    return new Promise<any>(async (resolve, reject) => {
    try {
      const _db = new pg.Client(config.ekaya_local_connection_string);
        await connect(_db);
        _db.query(sql, function(error, result) {
            if (error) {
                console.error('Error running query', error);
          _db.end();
          reject(error);
                return;
            }
        _db.end();
            resolve(result);
        });
    }
    catch (error) {
      reject(error);
    }
    });
}

function connect(_db: pg.Client) : Promise<void> {
    return new Promise<void>(  function(resolve, reject) {
    try {
        _db.connect(function(error) {
            if (error) {
                console.error('Could not connect to postgres in integration test helper', error);
                reject(error);
          return;
            }
            resolve(null);
        });
    }
    catch (error) {
      reject(error);
    }
    });
}

UPDATE - after vitaly-t's suggestion

New database code, no effect on the problem:

import 'reflect-metadata';
const Config = require('../../../src/config');
const config = new Config();
const pgp = require("pg-promise")();

export function runQuery(sql: string) : any {
return new Promise<any>(async (resolve, reject) => {
    try {          
      const _db = pgp(config.ekaya_local_connection_string);
      const result = await _db.query(sql);
      pgp.end();
      resolve({rows: result}); 
    }
    catch (error) {reject(error);}
  });
}
Richard
  • 14,798
  • 21
  • 70
  • 103
  • I don't think `Promise(async (resolve, reject) => {})` is correct. This should read `Promise((resolve, reject) => {})`. – Tomalak Jun 22 '16 at 11:42
  • `async` is required before functions to tell the compiler you use `await` inside the function. If I remove it I can't use `await` anymore. – Richard Jun 22 '16 at 11:45
  • 1
    That connection looks bad indeed. If you want `pg` with promises, why not just use [pg-promise](https://github.com/vitaly-t/pg-promise)? Then you wouldn't need to reinvent the basics. Something always ends up wrong. – vitaly-t Jun 22 '16 at 13:25
  • Thanks. I was not aware of that library. I will try it tomorrow morning and reply. – Richard Jun 22 '16 at 14:00
  • Sadly that didn't make a difference. The execution still disappears on the same line in my tape test and never returns nor errors. – Richard Jun 23 '16 at 08:37
  • @Richard I have corrected the pg-promise code you provided ;) – vitaly-t Jun 24 '16 at 02:23
  • Your version doesn't close the connection immediately. I need that. And it doesn't return `{rows:}` (so all my existing tests still work based on the old pg). Also, it still won't fix my problem :( I think the error is probably in my main await code in the .gif somehow, not a database thing. – Richard Jun 24 '16 at 06:49

0 Answers0