jeudi 15 mars 2018

Efficiently test events with Jasmine

I'm testing a rather large project, and everything is fine except for events (that is EventEmitter / event.js from node) where I have to change the environment to get different outcomes from the same event.

My current setup looks as follows:

beforeEach((done)=> {
        //<sets up some stuff> 


       //<sets up spies >

        <the_Event_Emitter>.emit('keyword1');


        setTimeout(() => {
            done();
        },1);

    });

and then in the specs are just the expects.

Which works perfectly but I'd have to write an entire describe-Block with a specific beforeAll-block for EVERY variant to the environment (the event in question has a switch-case over some variable)

making the beforeEach-block look like this:

<the_Event_Emitter>.emit('keyword1');
<change Environment>
<the_Event_Emitter>.emit('keyword1');
...

would "work" but is unacceptably unclean, with calls leaking into the wrong specs etc.

Is there a decent way of doing this? The waitFor() from the old (pre 2.0) Jasmine would have solved ALL my problems with ease.

[I tried having no beforeEach ( the .emit etc. within the spec) but that doesnt work since events run asynchronously, so the code in question is run AFTER the expects have been tried (and failed becuase the code hasn't run yet)]

Aucun commentaire:

Enregistrer un commentaire