question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Db name different for each test file

See original GitHub issue

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?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:12
  • Comments:5

github_iconTop GitHub Comments

3reactions
vassbencecommented, Jan 12, 2021

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?

2reactions
matthyycommented, Dec 12, 2020

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 :

  1. connect to instance and the default database.
  2. 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:

  1. update documentation with above examples
  2. when building MONGO_URI variable in environment.js, we change database name by using “global.MONGO_DB_NAME
Read more comments on GitHub >

github_iconTop Results From Across the Web

how to change DB name (PROD to TEST) - Oracle Communities
Dear Sirs please tell me simple ways to change the db name b'cos I have 2 db's (10g) as same name PROD in...
Read more >
Different db for testing in Django? - Stack Overflow
The way I handle this is through having multiple settings files, since I use that to maintain a set of common settings with...
Read more >
Why you should cleverly name Database Objects for SQL Unit ...
This article highlights the importance of clever naming of database objects from both development and SQL unit testing point of view.
Read more >
Sharing fixtures - ScalaTest
A test fixture is composed of the objects and other artifacts (files, sockets, database connections, etc.) tests use to do their work.
Read more >
Db - Codeception - Documentation
Alternatively, you can enable Db module in suite configuration file and run ... before each test populator: 'mysql -u $user -h $host $dbname...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found