T
T
TempUserMain2014-07-31 16:26:11
JavaScript
TempUserMain, 2014-07-31 16:26:11

How to migrate asynchronous tests from JSTestDriver to Jasmine?

JSTestDriver has the ability to create asynchronous tests using:
queue.call();
These blocks can go several in a row.
I need to migrate tests from JSTestDriver to Jasmine.
Jasmine uses spies and done() to create asynchronous tests.
With done() it is relatively simple - execute the necessary code and at the end we call done(). But what if one block is dependent on another (for example, some element is created in the first one, and then it is used later) and how to make them execute correctly?
Can you please tell me how to transfer asynchronous tests from JSTestDriver to Jasmine ?
In addition, I would be happy to provide links to articles that clearly describe the features of translating tests to Jasmine.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Eugene Ponomarev, 2014-08-07
@Elergy

I think in your case Jasmine version 1.3 is more suitable - jasmine.github.io/1.3/introduction.html
They had support for such things - runs / waitsFor.

var id = 0;
        runs(function() {
            setTimeout(function() {
                console.log('1st');
                id = 4;
            }, 5000);
        });

        waitsFor(function() {
            return id === 4; //если за 7секунд эта штука не вернет true, получим ошибку "timeout: timed out after 7000 msec waiting for id === 4" и дальше выполнение не пойдет
        }, 'id === 4', 7000);

        runs(function() {
            //в эту функцию мы перейдем, только после того как предыдущая waitsFor вернет true.
            console.log('2nd');
            expect(id).toEqual(4);
        });

        runs(function() {
            console.log('3rd');
            setTimeout(function() {
                id = 8;
            }, 5000);
        });

        waitsFor(function() {
            return id === 8;
        }, 'id === 8', 7000);

        runs(function() {
            console.log('4th');
            expect(id).toEqual(8)
        });

For 2.0, the only solution is to have multiple it's and do something after each one. But that's not a very good solution - that's not what it's intended for.

I
inmarko, 2014-08-22
@inmarko

Based on my own experience, I would wrap the framework that you use to create and manage elements on the page, so that each method returns a promise, then you can build the entire test on promises. Jasmine itself can be spared from done() etc. etc. so that the code is more beautiful, and it itself is wrapped in a promise. Look in the direction of jasminewd , you can patch jasmine itself with it, and the tests will be beautiful =) and everything will work as it should. And the tests will look something like this
describe("Some test-suite", function(){
it("Simple test", function(){
page.createElement(a);
page.element(a).test();
})
})
Maybe the answer is chaotic, I hope it will lead you to the right thoughts, and direct you in the right direction =)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question