为什么这个承诺代码永远不会运行?

时间:2016-06-22 11:32:05

标签: node.js postgresql typescript ecmascript-7 node.js-tape

正如您在下面的.gif中所看到的,我在集成测试中调用了tape.js两次,它运行正常。但是第二次运行测试时,在我注释掉将测试数据插入数据库的代码之后,tape.js函数的主体永远不会运行。

为什么会这样?

enter image description here

我在我的数据库代码中放了一个断点,它在下一次tape.js测试运行之前完成并调用resolve(),所以我没有得到它。

这是我的测试类,然后是数据库类:

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...`;
}

数据库类:

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);
    }
    });
}

更新 - 在vitaly-t的建议之后

新的数据库代码,对问题没有影响:

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);}
  });
}

0 个答案:

没有答案