簡體   English   中英

MongoDB co.netion 不會在 Jest afterAll() 中關閉

[英]MongoDB connetion will not close in Jest afterAll()

這是我的設置和拆卸方法的當前 state:

beforeAll(async () => {
    // await populate();

    try {
        await client.connect(() => console.log("Connection opened"));

        const db = client.db('trallo');
        const cards = db.collection('cards');

        const hello = {
            title: "Hello",
            description: "Goodbye"
        };

        const card = await cards.insertOne(hello);

        const todo = {
            title: "Todo",
            cards: [card.insertedId]
        }

        const inProgress = {
            title: "In progress",
            cards: []
        }

        const done = {
            title: "Done",
            cards: []
        }

        const lists = db.collection('lists');
        const list = await lists.insertMany([todo, inProgress, done]);

        const dummy = {
            title: "Dummy",
            lists: Object.keys(list.insertedIds).map(key => list.insertedIds[key])
        }

        const boards = db.collection('boards');
        const board = await boards.insertOne(dummy);



    } finally {
        // Ensures that the client will close when you finish/error
        await client.close(() => console.log("Connection closed"));
        // done();
    }
});

afterAll(async () => {
    // await deleteAll();

    try {
        await client.connect(() => console.log("Connection opened"));
        const db = client.db('trallo');
        const cards = db.collection('cards');
        await cards.deleteMany({});

        const lists = db.collection('lists');
        await lists.deleteMany({});

        const boards = db.collection('boards');
        await boards.deleteMany({});

    } finally {
        // Ensures that the client will close when you finish/error
        await client.close(() => console.log("Connection closed"));
        // done();
    }
});

我試過增加超時時間,在連接打開和關閉的地方四處移動,在測試和設置/拆卸中都傳遞了一個“完成”回調,到目前為止沒有任何效果。

這是運行 jest 時我得到的:

 RUNS  functional-tests/homepage.test.js

測試套件:1 個失敗,總共 1 個測試:1 個失敗,總共 1 個快照:0 個總時間:1.218 秒,估計 6 秒運行所有匹配 /functional-tests/i 的測試套件。

Jest 檢測到以下 2 個打開的句柄可能會阻止 Jest 退出:

● TCPWRAP

  12 |
  13 |         try {
> 14 |             await client.connect(() => console.log("Connection opened"));
     |                          ^
  15 |
  16 |             const db = client.db('trallo');
  17 |             const cards = db.collection('cards');

  at resolveSRVRecord (../node_modules/mongodb/src/connection_string.ts:81:7)
  at connect (../node_modules/mongodb/src/operations/connect.ts:42:28)
  at ../node_modules/mongodb/src/mongo_client.ts:425:14
  at maybePromise (../node_modules/mongodb/src/utils.ts:517:3)
  at MongoClient.connect (../node_modules/mongodb/src/mongo_client.ts:424:24)
  at Object.connect (homepage.test.js:14:26)
  at TestScheduler.scheduleTests (../node_modules/@jest/core/build/TestScheduler.js:317:13)
  at runJest (../node_modules/@jest/core/build/runJest.js:407:19)
  at _run10000 (../node_modules/@jest/core/build/cli/index.js:338:7)
  at runCLI (../node_modules/@jest/core/build/cli/index.js:190:3)

● TCPWRAP

  63 |
  64 |         try {
> 65 |             await client.connect(() => console.log("Connection opened"));
     |                          ^
  66 |             const db = client.db('trallo');
  67 |             const cards = db.collection('cards');
  68 |             await cards.deleteMany({});

  at resolveSRVRecord (../node_modules/mongodb/src/connection_string.ts:81:7)
  at connect (../node_modules/mongodb/src/operations/connect.ts:42:28)
  at ../node_modules/mongodb/src/mongo_client.ts:425:14
  at maybePromise (../node_modules/mongodb/src/utils.ts:517:3)
  at MongoClient.connect (../node_modules/mongodb/src/mongo_client.ts:424:24)
  at Object.connect (homepage.test.js:65:26)
  at TestScheduler.scheduleTests (../node_modules/@jest/core/build/TestScheduler.js:317:13)
  at runJest (../node_modules/@jest/core/build/runJest.js:407:19)
  at _run10000 (../node_modules/@jest/core/build/cli/index.js:338:7)
  at runCLI (../node_modules/@jest/core/build/cli/index.js:190:3)

● 測試完成后無法登錄。 你忘了在你的測試中等待一些異步的東西嗎? 試圖記錄“連接已打開”。

  63 |
  64 |         try {
> 65 |             await client.connect(() => console.log("Connection opened"));
     |                                                ^
  66 |             const db = client.db('trallo');
  67 |             const cards = db.collection('cards');
  68 |             await cards.deleteMany({});

  at console.log (../node_modules/@jest/console/build/CustomConsole.js:172:10)
  at log (homepage.test.js:65:48)
  at ../node_modules/mongodb/src/utils.ts:532:5
  at ../node_modules/mongodb/src/mongo_client.ts:427:9
  at connectCallback (../node_modules/mongodb/src/operations/connect.ts:38:5)
  at ../node_modules/mongodb/src/operations/connect.ts:103:5
  at ../node_modules/mongodb/src/sdam/topology.ts:476:47
  at ../node_modules/mongodb/src/cmap/connection_pool.ts:482:13
  at handleOperationResult (../node_modules/mongodb/src/sdam/server.ts:538:14)
  at Connection.onMessage (../node_modules/mongodb/src/cmap/connection.ts:452:5)

● 測試完成后無法登錄。 你忘了在你的測試中等待一些異步的東西嗎? 試圖記錄“連接已打開”。

  12 |
  13 |         try {
> 14 |             await client.connect(() => console.log("Connection opened"));
     |                                                ^
  15 |
  16 |             const db = client.db('trallo');
  17 |             const cards = db.collection('cards');

  16 |             const db = client.db('trallo');
  17 |             const cards = db.collection('cards');

  at console.log (../node_modules/@jest/console/build/CustomConsole.js:172:10)
  17 |             const cards = db.collection('cards');

  at console.log (../node_modules/@jest/console/build/CustomConsole.js:172:10)
  at log (homepage.test.js:14:48)
  at ../node_modules/mongodb/src/utils.ts:532:5
  at ../node_modules/mongodb/src/mongo_client.ts:427:9
  at connectCallback (../node_modules/mongodb/src/operations/connect.ts:38:5)
  at ../node_modules/mongodb/src/operations/connect.ts:103:5
  at ../node_modules/mongodb/src/sdam/topology.ts:476:47
  at ../node_modules/mongodb/src/cmap/connection_pool.ts:482:13
  at handleOperationResult (../node_modules/mongodb/src/sdam/server.ts:538:14)
  at Connection.onMessage (../node_modules/mongodb/src/cmap/connection.ts:452:5)

終止批處理作業(是/否)? y PS C:\Users\tracii\Documents\Projects\trallo> cd.\server\db
PS C:\Users\tracii\Documents\Projects\trallo\server\db> node.\mongoSeed.js 連接關閉 連接關閉 PS C:\Users\tracii\Documents\Projects\trallo\server\db>

嘗試編寫測試,就像連接到數據庫時在 Jest 文檔中所做的一樣,可能會有所幫助: https://jestjs.io/docs/mongodb

因此,而不是(我假設clientMongoClientnew實例):

beforeAll(async () => {    
    try {
        await client.connect(() => console.log("Connection opened"));

        const db = client.db('trallo');
        // ...rest of the code

嘗試等待MongoClient並像這樣立即連接:

describe('Your test', () => {
let connection;
let db;

beforeAll(async () => {
    try {
        connection = await MongoClient.connect("YOUR_MONGO_URI");
        db = await connection.db("trallo");
        // ...rest of the code

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM