1.0.1 • Published 9 years ago

mongoose-test-marker v1.0.1

Weekly downloads
1
License
MIT
Repository
github
Last release
9 years ago

Coverage Status Build Status Dependency Status

1.0.x API Reference

Introduction

This module will help you keep track of documents created during tests in your project. It will also add some metadata to the document, so it can be tracked if anything goes wrong.

Metadata added:

  __test: {
    pid: String,
    hostname: String,
    filename: String,
    tag: String
  }

Api

All the publicly accessible methods on the module api.

Example usage

'use strict';

var test_marker = require('mongoose-test-marker'),
    mongoose = require('mongoose'),
    Schema = mongoose.Schema,
    schema = new Schema({ foo: String }).plugin(test_marker),
    Model = mongoose.model('test', schema);

describe('This is my test', function() {

  before(function() {
    Model.setTestFile(__filename);
  });
  after(function(done) {
    // Removes all documents for this file and model. (including tagged documents).
    Model.removeAllTestDocuments(done);
  });

  describe('Sub tests', function() {
    var sub_tag = 'ThisIsMyTag';

    before(function() {
      Model.setTestTag(sub_tag);
    });
    after(function(done) {
      // Removes all tagged documents, scoped to this file.
      Model.removeTaggedTestDocuments(sub_tag);
    });
  });
});

Model.setTestFile(filename)

Sets the currently tracked file. This function has to be called for the plugin to collect documents inserted to a model. The plugin will throw an error if the filename is not set.

'use strict';

var test_marker = require('mongoose-test-marker'),
    mongoose = require('mongoose'),
    Schema = mongoose.Schema,
    schema = new Schema({ foo: String }).plugin(test_marker),
    Model = mongoose.model('test', schema);

describe('This is my test', function() {

  before(function() {
    Model.setTestFile(__filename);
    // All documents created by this model is now scope to this filename.
  });

});

Model.setTestTag(tagname)

Sets a tag. This will branch of all documents created between the setTestTag and removeTaggedTestDocuments calls. This is useful for isolated sub tests.

'use strict';

var test_marker = require('mongoose-test-marker'),
    mongoose = require('mongoose'),
    Schema = mongoose.Schema,
    schema = new Schema({ foo: String }).plugin(test_marker),
    Model = mongoose.model('test', schema);

describe('This is my test', function() {

  before(function() {
    Model.setTestFile(__filename);
    // All documents created by this model is now scope to this filename.
  });

  describe('Sub tests', function() {
    var sub_tag = 'ThisIsMyTag';

    before(function() {
      // Documents will now be created with a tag, which isolates documents.
      Model.setTestTag(sub_tag);
    });
    after(function(done) {
      // Removes all tagged documents for this model, scoped to this file.
      // Will not remove documents created before the `setTestTag` call.
      Model.removeTaggedTestDocuments(sub_tag);
    });
  });
});

Model.removeAllTestDocuments(callback)

Removes all test documents for the current file and model.

'use strict';

var test_marker = require('mongoose-test-marker'),
    mongoose = require('mongoose'),
    Schema = mongoose.Schema,
    schema = new Schema({ foo: String }).plugin(test_marker),
    Model = mongoose.model('test', schema);

describe('This is my test', function() {

  before(function() {
    Model.setTestFile(__filename);
    // All documents created by this model is now scope to this filename.
  });

  after(function(done) {
    // Removes all documents for this file and model. This includes all tagged documents
    // for this file an model.
    Model.removeAllTestDocuments(done);
  });

});

Model.removeTaggedTestDocuments(tagname, callback)

Removes only the tagged documents for the current file and model.

'use strict';

var test_marker = require('mongoose-test-marker'),
    mongoose = require('mongoose'),
    Schema = mongoose.Schema,
    schema = new Schema({ foo: String }).plugin(test_marker),
    Model = mongoose.model('test', schema);

describe('This is my test', function() {

  before(function() {
    Model.setTestFile(__filename);
  });
  after(function(done) {
    // Removes all documents for this file and model. (including tagged documents).
    Model.removeAllTestDocuments(done);
  });

  describe('Sub tests', function() {
    var sub_tag = 'ThisIsMyTag';

    before(function() {
      Model.setTestTag(sub_tag);
    });
    after(function(done) {
      // Removes all tagged documents, scoped to this file.
      Model.removeTaggedTestDocuments(sub_tag);
    });
  });
});

Running Tests

$ npm test

License

MIT