如何配置内存中梦导?



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

配置.ts

/**
* @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(,甚至在运行测试之前就清理了数据库(以防上次运行出现问题(

mongoose.connect('mongodb://localhost/testing_db')

const db = mongoose.connection
db.on('error', err => {
console.error(err.toString())
done(err)
})
db.once('open', () => {
db.db.dropDatabase(() => {
done()
})
})

这会降低testing_db

我已经开始使用 @shelfio/开玩笑-mongodb,到目前为止,它工作得很好。

他们网站上的文档很棒,存储库有不错的例子。

它也是开玩笑在他们的网站上推荐的库 - 与MongoDB一起使用,所以我建议你开始看这个,如果你还没有的话。

工作了几个小时后。我配置了配置,这对我来说很好用。

/**
* @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'
}
}
};

最新更新