GithubHelp home page GithubHelp logo

Comments (5)

vassbence avatar vassbence commented on September 3, 2024 3

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?

from jest-mongodb.

sbland avatar sbland commented on September 3, 2024 2

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.

from jest-mongodb.

matthyy avatar matthyy commented on September 3, 2024 2

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"

from jest-mongodb.

djaircarvalho avatar djaircarvalho commented on September 3, 2024

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__);
  });

from jest-mongodb.

danny-does-stuff avatar danny-does-stuff commented on September 3, 2024

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__}`
	)
}

from jest-mongodb.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.