You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have growing codebase, where most early test suites were testing functions interacting with MongoDB, so all those tests needed MongoDB mocking to memory database. All went well up until that.
Now that I have tests suites in working that are about external API functionalities, those test APIs don't have MongoDB mocking in them. This is when problems started to arise.
When I run my test suite with either jest --watchAll or npm test -- --watchAll, initially all tests pass, as expected. But once I make any change to any file within project, most tests fail with following error:
FAIL __tests__/users/users.test.js
● Test suite failed to run
ENOENT: no such file or directory, open '/home/user/path/to/my/project/globalConfig.json'
at MongoEnvironment.setup (node_modules/@shelf/jest-mongodb/lib/environment.js:22:58)
Each test suite gives that same error. If I make new change, most likely similar errors occur again. Randomly few test suites might pass, while others give this same error. And really randomly, all tests suites might pass, as expected.
If I manually press enter in console where jest --watchAll is running, making it to rerun all tests, all tests pass once again.
I can prevent this error by adding MongoDB mocking lines to my non-database related test file, mainly these lines:
And once those are present, jest --watchAll works as expected.
Another alternative is to use jest --onlyChanged --watch, which doesn't seem to be affected with this, even if there are both database related testsuites and non-database related testsuites present at same time.
To me this feels like a bug, but I can't really pinpoint exact cause/location for it. Another alternative is that I use this jest-mongodb package completely wrong, which causes this kind of behaviour.
Test files that have mongodb mocking have it done like this:
// __tests__/users/users.test.js
'use strict';
const { createUser } = require('../../lib/users');
const { connect, disconnect } = require('../../lib/db/mongodb/mongodb');
jest.mock('../../lib/db/mongodb/mongodb');
let db;
let collection;
beforeAll(async () => {
// Mock the connection object
db = await connect();
collection = db.collection('users');
});
afterAll(async () => {
// Close the MongoDB connection
await disconnect();
});
describe('createUser', () => {
it('should create new user to database', async () => {
const newUser = {
email: '[email protected]',
firstName: 'Sam',
lastName: 'Sample',
};
const result = await createUser(newUser);
const dbresult = await collection.findOne({ email: newUser.email });
expect(result).toMatchObject(newUser);
});
});
I have growing codebase, where most early test suites were testing functions interacting with MongoDB, so all those tests needed MongoDB mocking to memory database. All went well up until that.
Now that I have tests suites in working that are about external API functionalities, those test APIs don't have MongoDB mocking in them. This is when problems started to arise.
When I run my test suite with either jest --watchAll or npm test -- --watchAll, initially all tests pass, as expected. But once I make any change to any file within project, most tests fail with following error:
Each test suite gives that same error. If I make new change, most likely similar errors occur again. Randomly few test suites might pass, while others give this same error. And really randomly, all tests suites might pass, as expected.
If I manually press enter in console where jest --watchAll is running, making it to rerun all tests, all tests pass once again.
I can prevent this error by adding MongoDB mocking lines to my non-database related test file, mainly these lines:
And once those are present, jest --watchAll works as expected.
Another alternative is to use
jest --onlyChanged --watch
, which doesn't seem to be affected with this, even if there are both database related testsuites and non-database related testsuites present at same time.To me this feels like a bug, but I can't really pinpoint exact cause/location for it. Another alternative is that I use this jest-mongodb package completely wrong, which causes this kind of behaviour.
Test files that have mongodb mocking have it done like this:
Here are related files:
Any help would be greatly appreciated.
The text was updated successfully, but these errors were encountered: