首页 > 解决方案 > 如何配置内存中的mongodb?

问题描述

我正在使用MongoDB数据库对我的 NodeJS-Typescript应用程序进行集成测试。我使用Jest作为测试框架。如何用可用于测试的内存数据库(mongoDb)替换真实的数据库配置。谁能帮我配置一下?

配置文件

/**
 * @file Configuration file - Testing Configuration.
 */

export default {
    jwtPrivateKey: '11234.xsdfcswfe.23rcscdsfg',
    // Testing Database configuration
    MongoDB: {
        dbConfig: {
            user: 'xxxx',
            password: 'xxxx',
            host: '11.222.333.444',
            port: '27017',
            authMechanism: 'SCRAM-SHA-1',
            authSource: 'permissionlevel',
            dbName: 'sample_db'
        }
    }

};

标签: javascriptnode.jsmongodbtypescriptjestjs

解决方案


工作几个小时后。我配置了适合我的 config.ts。

/**
 * @file Configuration file - Testing Configuration.
 */

// configuring In-memory mongodb
const globalAny:any = global;
const inMemoryUri= globalAny.__MONGO_URI__
let uri=inMemoryUri.split('/')
let hostPort=uri[2].split(':')

export default {    
    jwtPrivateKey: '121231231fbuyfg.hfvufuewfr3452',
    // Testing Database configuration
    MongoDB: {
        dbConfig: {
            user:'',
            host: hostPort[0],
            port: '27017',
            authMechanism: 'SCRAM-SHA-1',
            authSource: 'permissionlevel',
            dbName: 'jest'
        }
    }
};

推荐阅读