错误:超时超过2000毫秒。承诺在测试运行之前不会返回

时间:2019-05-16 17:02:08

标签: javascript node.js mocha

我遇到了await

错误

如果返回承诺以确保其解决,这是有道理的,但我真的以为我已经做到了(通过使用const sqlite3 = require('sqlite3').verbose(); const util = require('util'); async function getDB() { return new Promise(function(resolve, reject) { let db = new sqlite3.Database('./project.db', (err) => { if (err) { console.error(err.message); reject(err) } else { console.log('Connected to the project database.'); resolve(db) } }); return db }); } exports.getDB = getDB try { // run these statements once to set up the db // let db = getDB(); // db.run(`CREATE TABLE services(id INTEGER PRIMARY KEY, service text, date text)`); // db.run(`INSERT INTO services(id, service, date) VALUES (1, 'blah', '01-23-1987')`) } catch(err) { console.log(err) } exports.get = async function(service) { function getResults(service) { return new Promise(async function (resolve, reject) { const db = await getDB(); let sql = `SELECT Id id, Service service, Date date FROM services WHERE service = ?`; db.get(sql, [service], (err, row) => { if (err) { console.error(err.message); reject(err) } else { if (row) { let this_row = {'id': row.id, 'service': row.service, 'date': row.date}; this_row ? console.log(row.id, row.service, row.date) : console.log(`No service found with the name ${service}`); resolve(this_row) } else { resolve(null) } } }) }); } let row = await getResults(service) return row } exports.clear = async function(service) { function deleteResults(service) { return new Promise(async function (resolve, reject) { const db = await getDB(); let sql = `DELETE from services WHERE service = ?`; db.run(sql, [service]); }); } await deleteResults(service) } 关键字)。这里似乎有一个我不理解的比赛条件

我正在为数据库连接模块编写单元测试:

states.js

const mocha = require('mocha');

const assert = require('assert');
const expect = require('chai').expect;
const should = require('chai').should();

const state = require('../state');

let deletion_sql = `DELETE from services WHERE service  = ?`;


it("get() should return the expected row", async function() {
    let db = await state.getDB()
    await db.run(deletion_sql, 'blah')
    await db.run(`INSERT INTO services(id, service, date) VALUES (1, 'blah', '01-23-1987')`)
    let result = await state.get('blah')
    console.log("get test result is")
    console.log(result)
    assert.deepEqual(result, { 'id': 1, 'service': 'blah', 'date': '01-23-1987' })
});


it("clear() should delete row from db", async function() {
  await state.clear('blah')
  let result = await state.get('blah')
  assert.equal(result, null)
})

我的testStates.js:

clear() should delete row from

测试create table test_tbl (id int, name character varying(10)); insert test_tbl values(1,'A'); insert test_tbl values(2,'B'); insert test_tbl values(3,'C'); insert test_tbl values(4,'D'); insert test_tbl values(5,'F'); db`每次都因相同的错误而失败,并且一次都没有通过。由于我不了解JavaScript中的诺言,因此我如何使用这些诺言有根本的错误。

1 个答案:

答案 0 :(得分:0)

我能够使用.then()而不是await关键字来运行测试:

it("clear() should delete row from db", async function() {
  state.clear('blah').then(async function() {
    let result = await state.get('blah')
    assert.equal(result, null)
  })
})

我很想找出为什么await关键字在这里无效的原因,因为它在另一个测试中有效;我很乐意接受这作为答案。