Javascript 如何配置内存中的mongodb?
我正在使用MongoDB数据库为我的NodeJS-Typescript应用程序进行集成测试。我正在使用Jest作为测试框架。如何用内存数据库(mongoDb)替换真实的db配置,我可以使用它进行测试。有人能帮我配置吗 config.tsJavascript 如何配置内存中的mongodb?,javascript,node.js,mongodb,typescript,jestjs,Javascript,Node.js,Mongodb,Typescript,Jestjs,我正在使用MongoDB数据库为我的NodeJS-Typescript应用程序进行集成测试。我正在使用Jest作为测试框架。如何用内存数据库(mongoDb)替换真实的db配置,我可以使用它进行测试。有人能帮我配置吗 config.ts /** * @file Configuration file - Testing Configuration. */ export default { jwtPrivateKey: '11234.xsdfcswfe.23rcscdsfg',
/**
* @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'
}
}
};
您可以在运行测试之前设置一个真正的测试数据库&只需在运行测试之后删除它。在本例中(使用
mongoose
),即使在运行测试之前,也会清理数据库(以防上次运行时出现问题)
这将删除我已经开始使用的测试\u db
到目前为止,它工作得很好
他们网站上的文档很棒,回购协议也有不错的例子
这也是jest在其网站上推荐的图书馆——因此,如果您还没有开始阅读,我建议您在工作数小时后开始阅读。。我配置了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'
}
}
};
是的,我已经提到了。在我的例子中,我需要连接测试数据库进行集成测试。所以我需要在config.ts中设置一个内存配置,这样我就可以避免在每个测试文件中出现之前和之后的块。没有为该用例尝试过完全内存,但您是否考虑过旋转docker MongoDB容器,然后针对该实例运行测试?同样的工作方式,可以防止额外的拆卸需要,因为它会被破坏。不要破坏你的帖子。通过在此网站上发布,您已不可撤销地授予Stack Exchange network在其认为合适的时间内根据发布该内容的权利。有关删除的替代方案,请参见:
/**
* @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'
}
}
};