最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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.
发布评论

评论列表(0)

  1. 暂无评论