簡體   English   中英

無法為 mongodb 代碼編寫笑話測試

[英]Unable to write a jest test for mongodb code

我正在嘗試編寫我的第一個笑話測試來測試數據庫調用。 但是我無法進行基本的 mongo 測試。

這是我的 package.json

{
  "scripts": {
    "test": "jest"
  },
  "devDependencies": {
    "@shelf/jest-mongodb": "^2.1.0",
    "jest": "^27.2.4"
  },
  "dependencies": {
    "mongodb": "^4.1.4"
  },
  "jest": {
    "preset": "@shelf/jest-mongodb"
  }
}

我去了https://jestjs.io/docs/mongodb並抓住了這個例子並粘貼到db.test.js

const {MongoClient} = require('mongodb');

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

  beforeAll(async () => {
    connection = await MongoClient.connect(global.__MONGO_URI__, {
      useNewUrlParser: true,
    });
    db = await connection.db(global.__MONGO_DB_NAME__);
  });

  afterAll(async () => {
    await connection.close();
    await db.close();
  });

  it('should insert a doc into collection', async () => {
    const users = db.collection('users');

    const mockUser = {_id: 'some-user-id', name: 'John'};
    await users.insertOne(mockUser);

    const insertedUser = await users.findOne({_id: 'some-user-id'});
    expect(insertedUser).toEqual(mockUser);
  });
});

然后我運行了這些命令:

npm install
npm run test

我得到了這些錯誤:

 FAIL  ./db.test.js
  ● insert › should insert a doc into collection

    TypeError: Cannot read properties of undefined (reading 'match')

       6 |
       7 |   beforeAll(async () => {
    >  8 |     connection = await MongoClient.connect(global.__MONGO_URI__, {
         |                                    ^
       9 |       useNewUrlParser: true,
      10 |     });
      11 |     db = await connection.db(global.__MONGO_DB_NAME__);

      at new ConnectionString (node_modules/mongodb-connection-string-url/src/index.ts:98:23)
      at parseOptions (node_modules/mongodb/src/connection_string.ts:249:15)
      at new MongoClient (node_modules/mongodb/src/mongo_client.ts:327:34)
      at Function.connect (node_modules/mongodb/src/mongo_client.ts:507:27)
      at db.test.js:8:36


  ● Test suite failed to run

    TypeError: Cannot read properties of undefined (reading 'close')

      13 |
      14 |   afterAll(async () => {
    > 15 |     await connection.close();
         |                      ^
      16 |     await db.close();
      17 |   });
      18 |

      at db.test.js:15:22

似乎 jest 無法創建 mongo 實例? 我做錯了什么?

哦,我想通了。 這是我的最終解決方案:

我的 package.json

{
  "scripts": {
    "test": "jest"
  },
  "devDependencies": {
    "@shelf/jest-mongodb": "^2.1.0",
    "jest": "^27.2.4"
  }
}

我還需要一個jest.config.js ,它看起來像這樣:

module.exports = {
  preset: '@shelf/jest-mongodb',
};

https://jestjs.io/docs/mongodb給出的示例測試有問題。 所以我在我的 db.test.js 中修復了它,現在看起來像這樣:

const {MongoClient} = require('mongodb');

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

  beforeAll(async () => {
    connection = await MongoClient.connect(global.__MONGO_URI__, {
      useNewUrlParser: true,
    });
    db = await connection.db(global.__MONGO_DB_NAME__);
  });

  afterAll(async () => {
    await connection.close();
    if(db.close) {
      await db.close();
    }
  });

  it('should insert a doc into collection', async () => {
    const users = db.collection('users');

    const mockUser = {_id: 'some-user-id', name: 'John'};
    await users.insertOne(mockUser);

    const insertedUser = await users.findOne({_id: 'some-user-id'});
    expect(insertedUser).toEqual(mockUser);
  });
});

然后我運行npm install; npm run test; npm install; npm run test; . 我看到測試運行並成功。

暫無
暫無

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

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