开玩笑的诺言没有解决

时间:2018-07-17 22:29:38

标签: express asynchronous testing jestjs supertest

我正在用Jest测试我的Express服务器。所有测试都通过了,但Jest没有关闭或退出。我不知道为什么,但是我相信测试运行后服务器将关闭。我尝试运行yarn jest --detectOpenHandles,然后返回响应:“ Jest已检测到以下1个打开的句柄,可能会阻止Jest退出:

  • 承诺”

这是我的代码...

const request = require('supertest');
const app = require('../server');


const server = app.listen(8000, '127.0.0.1');

afterAll(() => {
  server.close();
});

describe('GET /', () => {
  it('should respond with JSON (list of all users)', async () => {
    try {
      const response = await request(server).get('/')
        .expect('Content-Type', /json/);
      expect(response.statusCode).toBe(200);
    } catch (e) {
      console.log(e);
    }
  });
});

如果这实际上是问题,我似乎无法理解为什么诺言无法解决。我的express应用程序是从Firebase提取数据,并在获取请求后返回数据。有什么想法吗?

在Server.js文件中,我连接到Firebase也许是问题所在?

const db = admin.database();
const ref = db.ref('users');

app.get('/', (req, res) => {
// Attach an asynchronous callback to read the data at our posts reference
  ref.on('value', function(snapshot) {
    res.send(parseUsers(snapshot.val()));
    res.status(200);
  }, function (errorObject) {
    res.send([],'The read failed: ' + errorObject.code);
    res.status(500);
  });
} );

0 个答案:

没有答案
相关问题