🚀 Socket Launch Week 🚀 Day 5: Introducing Socket Fix.Learn More
Socket
Sign inDemoInstall
Socket

mongoose-fixture-loader

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongoose-fixture-loader

A simple fixture loader for Mongoose

1.0.0
Source
npm
Version published
Maintainers
1
Created
Source

Mongoose Fixture Loader

Build Status

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)
    ]);
    

Installation

npm install --save mongoose-fixture-loader

Usage

  • 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, load the fixture into MongoDB.

    const loadFixture = require('mongoose-fixture-loader');
    const UserModel = require('../src/models/user-model.js');
    const user = require('./fixtures/user.js');
    
    describe('a test suite', () => {
      before((done) => {
        loadFixture(UserModel, user)
          .then((userInst) => {
            done();
          })
          .catch((err) => {
            done(err);
          });
      });
    });
    
  • Enjoy testing!

Contributing

  • Fork it!
  • Create your feature branch: git checkout -b my-new-feature
  • Commit your changes: git commit -am 'Add some feature'
  • Push to the branch: git push origin my-new-feature
  • Submit a pull request :D

License

Copyright (c) 2016 Supasate Choochaisri

Licensed under the Apache License.

Keywords

mongoose

FAQs

Package last updated on 01 Aug 2016

Did you know?

Socket

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.

Install

Related posts