Comments (5)
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.
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.
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"
from jest-mongodb.
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.
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)
- Major version change log is missing
- Breaking change in 4.1.0: can't require @shelf/jest-mongodb/setup HOT 2
- Jest: Got error running globalSetup - First run works - After Monorepo + Babel
- $unwind $lookup problems HOT 1
- Error in 4.1.3 - Cannot read properties of undefined (reading 'rootDir') HOT 2
- Atlas search directives are not supported HOT 1
- TypeError: Class extends value undefined is not a constructor or null HOT 8
- StdoutInstanceError: Instance failed to start because a library is missing or cannot be opened: "libcrypto.so.1.1" HOT 6
- ERESOLVE: Found: [email protected], peer mongodb@"3.x.x || 4.x" from @shelf/[email protected] HOT 1
- Need to update the documentation to reflect current 'mongodb' library HOT 2
- [Question] How to use views in tests?
- jest-environment-node peer dependency
- useSharedDBForAllJestWorkers: false is not working and all jest workers are getting the same database URI HOT 2
- Preset conflicting with jest --watch
- Upgrade dependencies for mongodb@6 and higher HOT 4
- jest --watchAll fails when having test suites without mongodb mocking
- Improvements Suggestions
- Improvements Suggestions
- Not working on Ubuntu 23.04 HOT 3
- Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature. HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jest-mongodb.