typescript - Jest test case interference in parallel runs with Mongoose - Stack Overflow

I have a Jest test file as follows:import mongoose from 'mongoose';import { MongoMemoryServ

I have a Jest test file as follows:

import mongoose from 'mongoose';
import { MongoMemoryServer } from 'mongodb-memory-server';

describe('mySystem', () => {
    let inMemoryDb: InMemoryDb;
    let queryCount = 0;

    beforeAll(() => {
        mongoose.set('debug', (_collectionName, _method, _query, _doc) => {
            queryCount++;
        });
    });

    beforeEach(async () => {
        inMemoryDb = await useInMemoryDb();  // Uses MongoMemoryServer
        queryCount = 0;
    });

    afterEach(async () => {
        await inMemoryDb.cleanUp();  // Disposes of MongoMemoryServer
    });

    it('should do one thing', async () => {
        // Do something with the DB here
        expect(queryCount).toBe(2);
    });

    it('should do another thing', async () => {
        // Do something with the DB here
        expect(queryCount).toBe(3);
    });
});

This checks per use case how many queries are made. If the tests are running sequentially, this works fine. However, if the tests are running in parallel, the tests interfere with each other, even though they each have their own database. This results for instance that all queries from the two individual tests are counted towards a single queryCount.

  1. Would it be possible to change this query counter such that it uses a unique query counter per test case?
  2. If not, how can I prevent that tests run in parallel? I already have set maxConcurrency: 1 and maxWorkers: 1 in my Jest config, and use the runInBand flag when I run the tests, but still they're executed in parallel.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745356235a4624136.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信