如何在玩笑的配置文件中添加mongodb配置?

时间:2019-11-04 10:02:50

标签: node.js typescript jestjs

我试图用玩笑在MongoDB中编写测试代码。因此,我遇到了运行测试文件的问题,该测试文件抛出错误未定义配置属性“ MongoDB.dbConfig” 。您能否提供有关如何处理打字稿中此错误的解决方案

jest.config.js

module.exports = {
  "roots": [
    "<rootDir>/src"
  ],
  "transform": {
    "^.+\\.tsx?$": "ts-jest"
  },
}

config / default.ts

export default {
    // Development Database configuration
    MongoDB: {
        dbConfig: {
            host: 'localhost',
            port: '27017',
            dbName: 'DB'
        }
    },

mongodb.ts

import config from 'config';
export class mongodb{
static async connect(colName: string) {
        const db_config: MongoDbConfig = config.get('MongoDB.dbConfig');
        const url: string = `mongodb://${db_config.host}:${db_config.port};
        const client: MongoClient = new MongoClient(url, {useNewUrlParser: true, useUnifiedTopology: true});
        await client.connect();
        const db: Db = client.db(db_config.dbName);
        const collection: Collection = db.collection(colName);
        return new MongoDb(client, collection);
    }
}

test.ts

import { mongodb } from '../../../mongodb';

describe(() => {
    it( async () => {
        const result = await mongodb.connect('db1');
        console.log(result)
    });
});

如何将MongoDB配置为有趣的配置文件

0 个答案:

没有答案