While I was setting up multi project jest testing I wanted to have mongodb connected before running any tests, so i placed that code in a setup file. But its not working as it should be.
My root jest.config.js file looks like this
module.exports = {
projects: [
'<rootDir>/shared/utilities/jest.config.js',
// '<rootDir>/jest-prettier.config.js',
],
setupFiles: ['./jest.setup.js', 'dotenv/config'],
}
And in /shared/utilities/jest.config.js i have
const { resolve } = require('path')
const root = resolve(__dirname, '../../')
module.exports = {
rootDir: root,
displayName: 'utilities',
testEnvironment: 'node',
restoreMocks: true,
coveragePathIgnorePatterns: ['node_modules', 'tests'],
coverageReporters: ['json-summary', 'text', 'lcov', 'clover', 'html'],
};
The jest.setup.js file does not execute while running jest
const mongoose = require("mongoose");
beforeAll(async() => {
conso.log("Connecting to MongoDB...");
await mongoose.connect(process.env.MONGO_TEST_DB_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false,
});
},30000);
afterAll(() => {
mongoose.connection.close();
});