Usage with jest globalSetup and globalTeardown
See original GitHub issueAwesome module!
We’re running fine with the current implementation, however, one small sticking point is that we have to perform a new connection and disconnection within every jest block. It would be awesome if we were able to use a globalSetup and globalTeardown to connect/disconnect our client (mongoose) to the database once.
Unfortunately, when I do this, the process.env.MONGO_URL
is undefined:
module.exports = async function globalSetup() {
await mongoose.connect(process.env.MONGO_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false,
});
};
I suspect this is because the setup and teardown might run before the preset? Any thoughts as to a way around this limitation?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:6
Top Results From Across the Web
Getting Jest global setup and global teardown to work in a ...
I want to run a function that opens db connection before running tests (global setup) and another function that closes ...
Read more >Configuring Jest
You can use --config flag to pass an explicit path to the file. ... globals [object]; globalSetup [string]; globalTeardown [string] ...
Read more >jest-pg - npm Package Health Analysis - Snyk
... for each test and delete the database after test using jest globalSetup/globalTeardown. For more information about how to use this package see...
Read more >Integration with Test Runners | mongodb-memory-server
For usage with jest it is recommended to use the globalSetup and globalTeardown options. config.ts : // this file could be anything (like...
Read more >Using with puppeteer - Jest - w3resource
With the Async Test Environment and Global Setup/Teardown APIs, Jest ... each of the Test Environment; use Global Teardown to close puppeter.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Yea. You should be able to, I believe.
mongoSetup
is just the variable name I assigned torequire'@shelf/jest-mongodb/setup')
which is the default export of https://github.com/shelfio/jest-mongodb/blob/master/setup.js#L10Given your initial function, it would look like:
I did as follows, but I am having trouble connecting to the database from my test files.
Here are the relevant files with their code.
jest.setup.js
jest.teadown.js
jest.config.js
None of my tests that require a connection to mongodb to run successfully and pass. Running
npm run test -- --detectOpenHandles
reveals the following errors.All tests exit with
TCPSERVERWRAP
.npm run test
istest: "jest"
in the package.json file.I have tried the block of async code in the
jest.setup.js
andjest.teadown.js
files in abeforeAll()
andafterAll()
embedded into each the test files and it works. All tests pass when the db connection originates from the test files themselves.Any guidance is highly appreciated.