javascript - 如何配置内存中的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'
}
}
};
解决方案
工作几个小时后。我配置了适合我的 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'
}
}
};
推荐阅读
- c# - 如何在塔防游戏中让敌人停止并开始移动?
- html - 如何在文本框中移动用户输入?
- ruby-on-rails - Rspec 需要状态代码 201,但在代码 200 上失败
- lwjgl - 几种“显示”方法不起作用(LWJGL 2.9.3)
- delphi - 如何在 IDE 中使用 DCC32 进行编译
- python - 通过脚本将 Juypter Notebook 以 PDF 格式直接导出到 Teams
- azure-devops - Azure DevOps 管道代理如何干净地重新启动自身?
- javascript - 更改嵌套函数内的变量值
- php - 如何在 MYSQL 中显示带有 JOIN 的产品?
- c - 期待“Hello world”时输出什么都没有