
Product
Rust Support Now in Beta
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
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
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.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.