-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
setupGlobal global not available when running multiple tests #6408
Comments
Duplicate of #6007 |
That does remind me that the example is wrong though - it only works with |
@SimenB thanks for the quick answer. The example should propably not advocate to use global in the globalSetup? for anyone wondering this is how I got around the problem: const MongodbMemoryServer = require('mongodb-memory-server')
const path = require('path')
const fs = require('fs')
const mongoServer = new MongodbMemoryServer.MongoMemoryServer()
const mongoUriFile = path.join(__dirname, 'mongoUri.txt')
module.exports = async () => {
const mongoUri = await mongoServer.getConnectionString()
fs.writeFileSync(mongoUriFile, mongoUri)
} // mongo-environment
const NodeEnvironment = require('jest-environment-node')
const path = require('path')
const fs = require('fs')
const mongoUriFile = path.join(__dirname, 'mongoUri.txt')
class MongoEnvironment extends NodeEnvironment {
constructor(config) {
super(config)
}
async setup() {
this.global.__MONGO_URI__ = fs.readFileSync(mongoUriFile, 'utf-8')
await super.setup()
}
async teardown() {
await super.teardown()
}
runScript(script) {
return super.runScript(script)
}
}
module.exports = MongoEnvironment |
@maloguertin why this works for parallel tests? |
I've come across this exact issue too, and the solution was to store mongo URI in the env.
|
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
🐛 Bug Report
I took inspiration from this: https://facebook.github.io/jest/docs/en/mongodb.html in order to run my MongoDB+Mongoose in memory. When I run the test command on for a specific file:
npm run test User.test.js
everything works fine and the globals are properly assigned.
When i run the test command for all my files:
npm run test
the globals are always
undefined
in my custom environmentsetup()
method.To Reproduce
Steps to reproduce the behavior:
my global setup file looks like this:
and then my custom environment:
Expected behavior
I should see a
console.log()
with a string of the mongo uri in the output but I getundefined
when running withnpm run test
Run
npx envinfo --preset jest
Paste the results here:
The text was updated successfully, but these errors were encountered: