New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to test an architect project ? #31

Closed
jeremieca opened this Issue Jan 8, 2014 · 3 comments

Comments

Projects
None yet
3 participants
@jeremieca

jeremieca commented Jan 8, 2014

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

This comment has been minimized.

Show comment
Hide comment
@bmatusiak

bmatusiak Jan 9, 2014

Contributor

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...

Contributor

bmatusiak commented Jan 9, 2014

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

This comment has been minimized.

Show comment
Hide comment
@janjongboom

janjongboom Jan 9, 2014

Contributor

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.

Contributor

janjongboom commented Jan 9, 2014

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

This comment has been minimized.

Show comment
Hide comment
@jeremieca

jeremieca Feb 5, 2014

Thank you for your answers.

jeremieca commented Feb 5, 2014

Thank you for your answers.

@jeremieca jeremieca closed this Feb 5, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment