Skip to content

Proposal: multiple async conditions  #29

@mcbain

Description

@mcbain

With a little modified version of the pollUntil function multiple async conditions using normal expectations could be supported. Since those expect() call always throw an informative assertion-error, it could be used also as more helpful test-error message.

            var i = 0;

            Meteor.setInterval(function () {
                i++;
            }, 100);

            until(
                waitFor,
                function () {
                    expect(i).to.equal(10);
                });

            until(
                waitFor,
                function () {
                    expect(i).to.equal('A');
                });

This example has two conditions, where the last one will never be true and result in a timeout with

   timeout after 4800ms waiting for: expected 48 to equal 'A'

One problem is still the "global-test-timeout", which after 5s terminates the test with Async batch timed out. The detailed error messages are only visible when they timeout little earlier.

Even more better to read could be

  waitFor.condition(function () {
                    expect(i).to.equal(10);
                })

This would need some changes in the ExpectationManager.

Here is the until function.

    until = function (expect, condition, timeout) {
        var step = 100;
        var start = (new Date()).valueOf();
        timeout = timeout || 4800;

        var lastError;

        var complete = expect(function (error) {
            if (error) {
                throw new Error('timeout after ' + timeout + 'ms waiting for: ' + lastError.message);
            }
        });

        var helper = function () {
            try {
                condition();
                complete();
                return;
            }
            catch (e) {
                lastError = e;
            }

            if (start + timeout < (new Date()).valueOf()) {
                complete(true);
                return;
            }
            Meteor.setTimeout(helper, step);
        };
        helper();
    };

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions