cujojs / wire

A light, fast, flexible Javascript IOC container
Other
862 stars 71 forks source link

BDD + CujoJS #159

Closed jbadeau closed 9 years ago

jbadeau commented 10 years ago

Hi Guys,

Just brainstorming a bit here. So after having developed a large and highly async SPA I feel the current stack of HTML5 + CujoJs is really productive. Howeve; I have found unit testing my moduals to be pretty cumbersome. Whereas SPA frameworks have realized great benefits from applying techniques like dependency-injection, aspect oriented programming, observables and promises; testing rarely if ever takes advantages of them.

For instance I often see test code like:

function(done) {
   var listener = function(event) {
        expect(event).to.equal('foo');
        done()
    }
    publish('foo');
}

or

function(done) {
    asyncTask('foo')
    .then(function(value) {
       expect(value).to.equal('bar');
        done()
    }, function(error) {
        fail(error.message)
        // or throw error.messsage;
    });
}

I find then not only unintuitive to write but also hard to read.

However; with a bit of help with our friends mr promise and mrs observe we can transform them to this.

    it('should succeed when called with a valid message', function () {
        return this.fooService.foo('bar'); // returns a resolved promise
    })
    .so.expect().to.resolve();
}

or

    function () {
        this.fooService.foo('foo');
    })
    .so.expect(this.listener) // observe calls via aspect
    .to.be.calledWith('foo');
}

These are just a few examples of how techniques applied to SPA can be used in teh testing contest.

Please note this is just an idea any positive or negative comments are welcome.

The main areas of concern are:

A small gist of my idea and possible API is here: https://gist.github.com/jbadeau/9911863

I am posting here as my intended libs to use are wire, meld, when and possibly cola

briancavalier commented 10 years ago

Hey @jbadeau, this really looks like great stuff, and is also somewhat in line with other test DSL extensions, like those in chai-as-promised for example.

I would absolutely love to find the right way to integrate wire into the test process, ie with the ability to mock certain components, etc etc. Looking at your gist, it seems like that's exactly what you're thinking!

Have you done any experiments with coding any of this to make it work yet? Where would you like to go with it from here?

jbadeau commented 10 years ago

I started working on an implementation this week.

You could really help out by pointing me to cujojs test cases that you either found difficult to implement or needed allot of boilerplate (mocking, spying, async, setup, etc).

Cheers

jbadeau commented 9 years ago

Was not an issue. Just a nite. Moste of this stuff is implemented in intern

briancavalier commented 9 years ago

Thanks @jbadeau. That's cool that you're using Intern.

jbadeau commented 9 years ago

yes its very nice, only thing I really miss from karma is it's ability to relaunch after a browser crash.