jest-mongodb
jest-mongodb copied to clipboard
Db name different for each test file
Hi,
from Readme I was under impression that if I pass empty object to instance property I will get random db per file.
If I simply print out the global variables in every test file I am getting: One file:
process.env.MONGO_URL: mongodb://127.0.0.1:56925/3b97e130-dd09-440d-9937-a7442a421da7?
global.__MONGO_URI__: mongodb://127.0.0.1:56925/3b97e130-dd09-440d-9937-a7442a421da7?
global.__MONGO_DB_NAME__: 1ed20263-b27c-42c8-a800-805368f8fdb3
Second file:
process.env.MONGO_URL: mongodb://127.0.0.1:56925/3b97e130-dd09-440d-9937-a7442a421da7?
global.__MONGO_URI__: mongodb://127.0.0.1:56925/3b97e130-dd09-440d-9937-a7442a421da7?
global.__MONGO_DB_NAME__: e2eb11c0-c836-4849-a7fb-568a642e02bf
So its true that db name is random, but its same for all test files, which is not really that much useful. My expectation was that it would be unique db name per test file - interestingly thats what we get in MONGO_DB_NAME, but we still need to create sensible MONGO_URL from it, it seems.
Am I missing something?
Hi @jardakotesovec, the jest doc has a slightly different example. Maybe you can have some luck:
beforeAll(async () => {
connection = await MongoClient.connect(global.__MONGO_URI__, {
useNewUrlParser: true,
});
db = await connection.db(global.__MONGO_DB_NAME__);
});
I had to hack the process.env.MONGO_URL variable as below to get this to work
const full_url = DB_NAME ? MONGO_URL.split('/').slice(0, -1).join('/') + '/' + DB_NAME : MONGO_URL;
This converted mongodb://127.0.0.1:41185/1605a3c8-5a40-4d93-880a-bb186f222db7 to mongodb://127.0.0.1:41185/DB_NAME. I then just altered DB_NAME for each test file.
With a config like this
mongodbMemoryServerOptions: {
instance: {},
binary: {
version: '4.0.3', // Version of MongoDB
skipMD5: true,
},
autoStart: false,
},
where instance is set to {}, I was able to get a unique mongo url for each test by using this function inspired by the comment by @sbland above
function getMongoUrl() {
return (
process.env.MONGO_URL.split('/')
.slice(0, -1)
.join('/') + `/${global.__MONGO_DB_NAME__}`
)
}
I agree with you, the variable "MONGO_URI" does not change over different test suites but "MONGO_DB_NAME" variable changes. That's why, the document mentions two steps :
- connect to instance and the default database.
- connect to a new database named "MONGO_DB_NAME" .
connection = await MongoClient.connect(process.env.MONGO_URL, {
useNewUrlParser: true,
useUnifiedTopology: true
});
db = await connection.db();
To make it work, you have to do this : (in order to connect to another database)
connection = await MongoClient.connect(process.env.MONGO_URL, {
useNewUrlParser: true,
useUnifiedTopology: true
});
await connection.db(global.__MONGO_DB_NAME__)
When using mongoose, you can do the following
mongoose.connect(uri: global.__MONGO_URI__, {dbName: global.__MONGO_DB_NAME__}
I am volunteer to make a PR. To me, there are two options:
- update documentation with above examples
- when building MONGO_URI variable in environment.js, we change database name by using "global.MONGO_DB_NAME"
The readme examples were changed in this PR but the Jest website still has parallel friendly setup examples.
I believe the examples should be parallel friendly as most people just grab it and go, but later face some unexpected race conditions when for example multiple test files clear the db in parallel.
(setting db explicitly to an unique string in beforeAll mitigates the race conditions)
@vladgolubev what's your opinion on this?