Automating Fixture Instantiation in Mocha using Async

by Jason on January 31, 2012

My latest Node.JS project has been entirely test-driven and I always strive to write very simple & concise tests. Convoluted code in a test will be far less likely to be maintained.

So when test driving my latest model, I found myself using hundreds of lines of code just to instantiate models using pre-built fixtures in the before function. Not ideal.

I much prefer this type of code:

var my_model_fixtures = require(app.root+'/spec/models/fixtures/my_model_fixtures.js');

describe('My Model', function() {  
  before(function(done) {    
    this.my_models = new Array;
    var that=this;['fixture1', 'fixture2'],
      function(my_fixture, iterator_callback) {
        var my_model = new MyModel(my_model_fixtures[my_fixture]);,data){
          if(!err) {
            iterator_callback(null, my_model);
          } else {
            iterator_callback(data, null);
        if(err) {
          console.log("Error instantiating fixtures: "+err);


I am looking for a seasoned and motivated developer for a growing, interesting company based in Connecticut which has grown over 600% since 2007. The role is in Norwalk.
• Web development
• eCommerce
• Web Services
It would be great if you had Lucene, Solr, Zend as well…if not, this would a chance to learn them…
Do you have a few minutes to discuss this ? I won’t waste your time.

If this opportunity doesn’t fit you, please let me know if you can recommend someone else.
Dave Perlman
Grenadier Search Group

by Dave Perlman on February 24, 2012 at 4:42 am. #

Thanks this helped! I prefer something like this as well, clean and simple.

by Ramsey on April 2, 2014 at 3:23 pm. #

Leave your comment


Required. Not published.

If you have one.