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
.
- Would it be possible to change this query counter such that it uses a unique query counter per test case?
- If not, how can I prevent that tests run in parallel? I already have set
maxConcurrency: 1
andmaxWorkers: 1
in my Jest config, and use therunInBand
flag when I run the tests, but still they're executed in parallel.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745356235a4624136.html
评论列表(0条)