【问题标题】:Mongoose, Supertest: Jest did not exit one second after the test run has completedMongoose,Supertest:测试运行完成后一秒 Jest 没有退出
【发布时间】:2021-03-01 03:48:43
【问题描述】:

我正在尝试使用 jest 和 supertest 来测试我的 node.js + express 应用程序。然而, 我不断收到错误消息“测试运行完成后一秒钟 Jest 没有退出。”

如果我从 app.js 中删除 mongoose.connect(...) 一切正常,但我需要数据库...

这是我的代码:

app.js

const express = require('express');
const mongoose = require('mongoose');
const keys = require('./config/keys');

mongoose.connect(keys.MONGO_URI, 
  { useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true });

const app = express();

app.get('/user', (req, res) => {
  res.send("success");
})

//...

module.exports = app;

app.test.js

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

describe('GET /user', function () {
    afterAll(() => mongoose.disconnect());
    it('has respononse status code 200', function (done) {
        request(app)
            .get('/user')
            .expect(200, done);
    });
});

错误信息:

> jest

 PASS  tests/app.test.js (6.798 s)
  GET /user
    √ has respononse status code 200 (22 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        6.875 s, estimated 7 s
Ran all test suites.

Jest did not exit one second after the test run has completed.

This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.

ReferenceError: You are trying to `import` a file after the Jest environment has been torn down.

      at BufferList.Readable (node_modules/readable-stream/lib/_stream_readable.js:179:22)
      at BufferList.Duplex (node_modules/readable-stream/lib/_stream_duplex.js:67:12)
      at new BufferList (node_modules/bl/bl.js:33:16)
      at new MessageStream (node_modules/mongodb/lib/cmap/message_stream.js:35:21)
      at new Connection (node_modules/mongodb/lib/cmap/connection.js:54:28)
C:\Users\XXX\server\node_modules\readable-stream\lib\_stream_readable.js:111
  var isDuplex = stream instanceof Duplex;
                        ^

TypeError: Right-hand side of 'instanceof' is not callable
    at new ReadableState (C:\Users\XXX\server\node_modules\readable-stream\lib\_stream_readable.js:111:25)
    at BufferList.Readable (C:\Users\XXX\server\node_modules\readable-stream\lib\_stream_readable.js:183:25)
    at BufferList.Duplex (C:\Users\XXX\server\node_modules\readable-stream\lib\_stream_duplex.js:67:12)
    at new BufferList (C:\Users\XXX\server\node_modules\bl\bl.js:33:16)      
    at new MessageStream (C:\Users\XXX\server\node_modules\mongodb\lib\cmap\message_stream.js:35:21)
    at new Connection (C:\Users\XXX\server\node_modules\mongodb\lib\cmap\connection.js:54:28)
    at C:\Users\XXX\server\node_modules\mongodb\lib\core\connection\connect.js:36:29
    at callback (C:\Users\XXX\server\node_modules\mongodb\lib\core\connection\connect.js:280:5)
    at TLSSocket.connectHandler (C:\Users\XXX\server\node_modules\mongodb\lib\core\connection\connect.js:325:5)
    at Object.onceWrapper (node:events:433:28)
    at TLSSocket.emit (node:events:327:20)
    at TLSSocket.onConnectSecure (node:_tls_wrap:1527:10)
    at TLSSocket.emit (node:events:327:20)
    at TLSSocket._finishInit (node:_tls_wrap:932:8)
    at TLSWrap.ssl.onhandshakedone (node:_tls_wrap:706:12)
npm ERR! code 1

我尝试过的事情:

  • 我使用“--detectOpenHandles”运行 Jest,但它没有提供任何其他信息。
  • 没有 app.listen(PORT)(我有一个单独的 server.js 文件)
  • 我在 package.json 中添加了以下内容:

package.json

"jest": {
  "testEnvironment": "node"
}

【问题讨论】:

  • detectOpenHandles 不应该解决它,而是要揭露原因。它没有在输出中添加任何内容吗?猫鼬是问题所在并不明显。你与它断开连接。并且省略了其他相关内容。 ... 是什么?如果有服务器侦听器并且您没有取消侦听,那显然是问题所在。 /users 会发生什么?没有stackoverflow.com/help/mcve,这个问题就离题了。
  • @EstusFlask 我已经更新了帖子并添加了完整的错误消息。 detectOpenHandles 没有添加任何东西,没有服务器监听器,/users 只是发送一条成功消息。

标签: node.js express mongoose jestjs supertest


【解决方案1】:

显然,当我调用 mongoose.disconnect(); 时,mongoose 没有建立连接,这会导致所有错误,所以我将其添加到我的代码中:

beforeAll((done) => {
    if(!mongoose.connection.db){
        mongoose.connection.on('connected', done)
    } else {
        done();
    }
}, 20000);

【讨论】:

  • 是的,该错误表明它是问题所在。这在正常情况下是不需要的,因为 Mongoose 为其余操作链接了一个连接承诺。这可以特定于 Mongoose 或 Mongo 版本。可能是一个错误。
猜你喜欢
  • 2021-01-13
  • 2021-12-26
  • 2020-02-13
  • 2019-06-15
  • 2018-12-08
  • 2019-02-05
  • 2021-03-28
  • 1970-01-01
  • 2020-08-25
相关资源
最近更新 更多