node-mongodb-fixtures
node-mongodb-fixtures copied to clipboard
I need a help to understand how node-mongodb-fixtures should work?!
Hi, first of all, I'm not familiar with node-mongodb-fixtures, that's why my question can be strange, but I try to add some tests to my REST API. I'm using jest and my API is based on node/express/MongoDB, also I'm using fakingoose to generate an entity from the db schema. So, the node-mongodb-fixtures take a mock entity and insert it to the db collection and drop it after all tests are done, am I right understand its workflow?
So, what did I do:
- created a folder 'fixtures' in the root of the app with the next structure:
-
./fixtures/entities/users.js
const fakingoose = require('fakingoose');
const UserSchema = require('./models/users');
// factory is created based on Mongoose schema defined for the User entity.
// This factory is then used to generate mock objects
const userFactory = fakingoose(UserSchema);
// The mock data generated is exported as an array of mock objects.
module.exports = [
userFactory.generate({
login: '[email protected]',
name: 'User',
phone: '+123456789'
}),
];
-
./fixtures/users.fixture.js const Fixtures = require('node-mongodb-fixtures');
module.exports = async (dbUrl = 'mongodb://localhost:27017/db') => {
const fixtures = new Fixtures({
dir: './fixtures/entities',
filter: 'users.*',
mute: false,
});
await fixtures.connect(dbUrl).then(() => fixtures.load());
const users = await Promise.resolve(fixtures._db.collection('users'))
.then((collection) => {
return collection.find().toArray();
});
const cleanup = () => fixtures.unload()
.then(() => fixtures.disconnect());
return {
// A function that the caller can use to clean up the database.
// A good time to call this returned function is after tests are completed
cleanup,
// The saved users in the MongoDB database
entities: users,
};
};
- and then I have invoked this in my test:
describe('Start test', () => {
let deleteArticles, users;
beforeAll(async () => {
({ cleanup: deleteArticles, entities: users } = await userFixtures());
console.log('users :: ', users); // <= expected to see here a collection of users with included mock user too
});
afterAll(done => mongoose.connection.close(done));
});
I have expected that mock user was added too, but got only previously-stored users. Did I miss something?
Thank for your help
Your filter looks suspicious. It includes a .
prefix where this is none in users.js
.
You have mute: false
... did the console not offer any clues?
Hi, @westyside and thanks for your response
Your filter looks suspicious. It includes a
.
prefix where this is none inusers.js
.
Yes, you are right about the filter property, but this mistake is only here. In code, it looks like this:
filter: 'users.*',
- the copy/past troubles))
You have mute: false... did the console not offer any clues?
no, it looks fine
console.log [done ] load users at log (../../node_modules/node-mongodb-fixtures/lib/index.js:17:22) console.log [done ] *load all at log (../../node_modules/node-mongodb-fixtures/lib/index.js:17:22) console.log [done ] *script all at log (../../node_modules/node-mongodb-fixtures/lib/index.js:17:22)
My only other thought was whether the factory generated mock objects include _id: ObjectId()
, and I don't even know that it is mandated, so this is a guess.
In your shoes I would test with a hardcoded fixture, get that to work, and then work toward the generated version.