Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
mongoose-fixture-loader
Advanced tools
A promise fixture loader for Mongoose.
Load a single object
loadFixture(UserModel, userObject);
Load an array of objects
loadFixture(UserModel, arrayOfUserObjects);
Load a sequence of objects
loadFixture(UserModel, userObject)
.then((userInstance) => {
loadFixture(BookModel, bookObjectRelatedToUserObject)
});
Load objects in parallel
Promise.all([
loadFixture(UserModel, userObject),
loadFixture(CatModel, catObject),
loadFixture(DogModel, dogObject)
]);
npm install --save mongoose-fixture-loader
Assume you have a user model file src/models/user-model.js
as the following.
const mongoose = require('mongoose');
const UserSchema = new mongoose.Schema({
firstName: { type: String, required: true },
lastName: {type: String, required: true },
email: { type: String },
created: { type: Date, default: Date.now }
});
module.exports = mongoose.model('User', UserSchema);
Create a fixture file test/fixtures/user.js
to export a JSON object,
module.exports = {
firstName: 'John',
lastName: 'Doe',
email: 'john.doe@test.com'
};
or an array of JSON object.
module.exports = [
{
firstName: 'John',
lastName: 'Doe',
email: 'john.doe@test.com'
},
{
firstName: 'Alice',
lastName: 'Bob',
email: 'alice.bob@test.com'
}
];
In your test file test/index-test.js
, you can load and test your fixture.
const expect = require('chai').expect;
const mongoose = require('mongoose');
const loadFixture = require('mongoose-fixture-loader');
const UserModel = require('../src/models/user-model.js');
const user = require('./fixtures/user.js');
// Mongoose default promise is deprecated
mongoose.Promise = global.Promise;
describe('a test suite', () => {
mongoose.connect('mongodb://localhost/mongoose-fixture-loader-test');
before((done) => {
loadFixture(UserModel, user)
.then((userInst) => {
done();
})
.catch((err) => {
done(err);
});
});
after((done) => {
UserModel.remove({})
.then(() => {
return mongoose.connection.close();
})
.then(() => {
done();
})
.catch((err) => {
done(err);
});
});
it('should find John', (done) => {
UserModel.find({})
.then((users) => {
expect(users[0].firstName).to.equal('John');
done();
})
.catch((err) => {
done(err);
});
});
});
Enjoy testing!
git checkout -b my-new-feature
git commit -am 'Add some feature'
git push origin my-new-feature
Copyright (c) 2016 Supasate Choochaisri
Licensed under the Apache License.
FAQs
A promise fixture loader for Mongoose
The npm package mongoose-fixture-loader receives a total of 38 weekly downloads. As such, mongoose-fixture-loader popularity was classified as not popular.
We found that mongoose-fixture-loader demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.