-
Notifications
You must be signed in to change notification settings - Fork 83
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
Usage with jest globalSetup and globalTeardown #212
Comments
This is achievable by calling const mongoSetup = require('@shelf/jest-mongodb/setup');
module.exports = async config => {
await mongoSetup();
const url = process.env.MONGO_URL;
if(url == null) {
throw new Error("MONGO_URL was not set");
}
// connect
} |
Confused @chanced. You're saying you can do this in jest’s |
Yea. You should be able to, I believe. Given your initial function, it would look like: const mongoSetup = require('@shelf/jest-mongodb/setup');
module.exports = async function globalSetup() {
await mongoSetup();
// process.env.MONGO_URL is now set by
// https://github.com/shelfio/jest-mongodb/blob/fcda766bfa433644a7383993467ccaa06dfea28b/setup.js#L26
await mongoose.connect(process.env.MONGO_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false,
});
}; |
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. import mongoose from "mongoose";
const mongoSetup = require("@shelf/jest-mongodb/setup");
module.exports = async () => {
await mongoSetup();
global.__DB__await = mongoose
.connect(process.env.MONGO_URL, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
useFindAndModify: false,
})
.then(null, (err) => {
console.log(
`In-memory mongodb instance failed to initialize. Error: ${err}`
);
});
};
import mongoose from "mongoose";
module.exports = async () => {
await mongoose.connection.close();
};
module.exports = {
preset: "@shelf/jest-mongodb",
globalSetup: "<rootDir>/server/__tests__/__utils__/jest.setup.js",
globalTeardown: "<rootDir>/server/__tests__/__utils__/jest.teardown.js",
} None of my tests that require a connection to mongodb to run successfully and pass.
All tests exit with I have tried the block of async code in the Any guidance is highly appreciated. |
i still have the same error; can't connect to the database and then timeout error |
Not clear if this will work due to https://mongoosejs.com/docs/jest.html#globalsetup-and-globalteardown |
Awesome 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:I suspect this is because the setup and teardown might run before the preset? Any thoughts as to a way around this limitation?
The text was updated successfully, but these errors were encountered: