c9 / architect

A simple yet powerful plugin system for large-scale node applications
MIT License
981 stars 129 forks source link

How to test an architect project ? #31

Closed jeremieca closed 10 years ago

jeremieca commented 10 years ago

Hello,

What is the best practice to test an plugin with architect ? Have you an example of test on a plugin ?

In fact, I have trouble in test of my plugins because it depends on other plugins...

Thank you in advance.

bmatusiak commented 10 years ago

The best way is to test the hole app...

this is a example


var MODE = "test";

Architect.createApp(Architect.resolveConfig(plugins, __dirname + "/plugins"), function(err, architect) {
        if (err) {
            console.error("While compiling app config '%s':", configPath);
            throw err;
        }else{
            var mainInit= architect.services.mainInit;
            if(MODE == "test"){
                mainInit.test(function(err){
                    if(err){
                        console.error("While Testing app '%s'", err);
                    }else{
                        console.log("App Tested good!");
                    }
                });
            }else{
                mainInit.start(function(err){
                    if(err){
                        console.error("While Starting app '%s'", err);
                    }else{
                        console.log("Started App!");
                    }
                });
            }
        }
    });

adding the test... i added a function to mainInit service

var tests = [];

register(null,{
    mainInit:{
        start:function(cb){
            try{
                server.listen();
                cb();
            }catch(e){
                cb(e);
            }
        },
        test:function(cb){
            try{
                for(var i in tests){ tests[i]() }
                cb();
            }catch(e){
                cb(e);
            }

        },
        addTest:function(testFN){
            tests.push(testFN);
        }
    }
});

and a plugin

imports.mainInit.addTest(function(){

    //do test

    //throw new Error("Something Bad Happen Here!");

});

I would like to see how others are testing there stuff...

janjongboom commented 10 years ago

So I can't find a nice example of this, but the way architect is made you have one plugin say:

function myAwesomePlugin(options, imports, register) {
  register();

  return {
    someFn: function() { return imports.dependency.doSomething() }
  }
}

Now you can easily test this unit of work because you can mock all dependencies.

test('something', function() {
  var dependency = { doSomething: sinon.stub() };
  var plugin = myAwesomePlugin({ opt1: "This is a injected option" }, { dependency: dependency  });
  plugin.someFn();
  assert.equal(dependency.callCount, 1);
}

So you really test only this plugin. If you want to test between multiple plugins I'd write an integration test.

jeremieca commented 10 years ago

Thank you for your answers.