Giter Club home page Giter Club logo

test_prepare's Introduction

Test-Prepare is a MondoDB test preparer. Is designed to clean and import test fixtures before a test to simulate scenarios and go back to initial stage after test.

Installation

First install node.js and mongodb. Then

$ npm install test-prepare --save-dev

Setup

Import test-prepare in your test file.

var prepare = require('test-prepare')({
    mongo_host: 'http://localhost:27017',
    fixtures_path: '/path-to-fixtures',
    verbose: true
});

Supported setup options

  • mongo_host: Host address of MongoDB, required
  • mongo_user: User of MongoDB, not required
  • mongo_password: Password of MongoDB, not required
  • fixtures_path: Phisical path of fixtures folder, required. Ex: ${__dirname}/../fixtures
  • verbose: Enable verbose mode. This will log test prepare info and feedbacks about connection and fixtures imports. Default is false.

Create fixture file

Inside fixtures folder, create a fixture file that will be imported to the test database.

  • model: Name od MongoDB collection
  • fixtures: Array of data that will be imported

Example, people.json:

{
    "model": "People",
    "fixtures": [
        {
             "name": "Jhon",
             "age": 31
        },
        {
             "name": "Michael",
             "age": 25
        }
    ]
}

Prepare database before test

before(function (done) {
    prepare.start(['fixture_file_name_without_extension', 'other_fixture_file_name_without_extension', '...']).then(done);
});

So if you want to import your fixture file people.json:

before(function (done) {
    prepare.start(['people']).then(done);
});

And importing many fixtures:

before(function (done) {
    prepare.start(['people', 'cars', 'telephones']).then(done);
});

Dropping database after test

After test you can clean test data ro reset your test scenario. This uill drop test-prepare temporary database.

after(function (done) {
    prepare.end().then(done);
});

Change fixture data before import

If you want change fixture data before import to the database, you can use the middleware function.

prepare._importFixture('people', middleware, callback);

prepare._importFixture('people', function(data) {
    // change fixture data  through middleware then import to the database.
    data.fixtures[0].name = 'Jhon Doe';
    return data;
})
.then(() => {
    //.... here on callback, you make your test asserts ...
    var result = example_get_by_name('Jhon Doe');
    expect(result.length).to.be.equal(1);
    done();
});

Accessing fixture data

Whenever a fixture is imported, its data is accessible through the fixture_fixtureName property that is exposed in the test-prepare object.

beforeEach(function (done) {
    prepare.start(['people', 'cars']).then(done);
});

it('my test', function(done) {
    var people = prepare.fixture_people;
    var cars   = prepare.fixture_cars;
    done();
});

test_prepare's People

Contributors

barslev avatar diogolmenezes avatar jeanfonsecaaquino avatar jrflga avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.