Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

124
Views
How to set up Jest with a setup file to run beforeEach across multiple test files

I have two Models which I am testing in individual files and in each file I am using beforeEach to clear the DB and seed the DB. I am also using afterEach to drop the DB and close the connection to MongoDB.

I am getting a MongoDB error for a duplicate key of my users so it seems that the beforeEach isn't running perfectly beforeEach test. If I run the files isolated then no issues. It is only when I run the whole test suite.

user.test.js

const mongoose = require('mongoose');
const request = require('supertest');
const app = require('../app');

const User = require('../models/user.model');
const userData = require('../db/data/userData');

// Valid User for Loggin In
// {
//  username: 'Bcrypt User',
//  password: 'YECm9jCO8b',
// };

beforeEach((done) => {
  mongoose.connect(process.env.DB_URI, () => {
    const seedDB = async () => {
        await User.deleteMany({});
        await User.insertMany(userData);
    };

    seedDB().then(() => {
        done();
    });
  });
});

afterEach((done) => {
  mongoose.connection.db.dropDatabase(() => {
    mongoose.connection.close(() => done());
  });
});

message.test.js

const mongoose = require('mongoose');
const request = require('supertest');
const app = require('../app');

const User = require('../models/user.model');
const userData = require('../db/data/userData');

const messageData = require('../db/data/messageData');
const Message = require('../models/message.model');

const validUserAccount = {
  username: 'Bcrypt User',
  password: 'YECm9jCO8b',
};

beforeEach((done) => {
  mongoose.connect(process.env.DB_URI, () => {
    const seedDB = async () => {
        await User.deleteMany({});
        await User.insertMany(userData);
        await Message.deleteMany({});
        await Message.insertMany(messageData);
    };

    seedDB().then(() => {
        done();
    });
  });
});

afterEach((done) => {
  mongoose.connection.db.dropDatabase(() => {
    mongoose.connection.close(() => done());
  });
});

So far I have tried using a jest.config.js and also tried add config to package.json but can't seem to figure out a global file to run before each

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Have you checked setupFilesAfterEnv option https://jestjs.io/docs/configuration#setupfilesafterenv-array?

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!