Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Calling $timeout.flush() with no pending request should not throw error #2507

@ryanzec

Description

@ryanzec

I have the following test for a service that internally uses $timeout:

describe('Beat', function(){
    var nagBeat, $timeout;

    beforeEach(module('nag.beat'));

    beforeEach(inject(function($injector) {
        nagBeat = $injector.get('nagBeat');
        $timeout = $injector.get('$timeout');
    }));

    it('should be able to add and remove beat', function() {
        var test = 0;

        nagBeat.add('increment', function() {
            test += 1;
        }, 50);

        //wait and flush timeout 2 times
        waits(50);
        $timeout.flush();

        waits(50);
        $timeout.flush();

        expect(test).toEqual(2);
        expect(nagBeat.activeBeatsCount()).toEqual(1);

        nagBeat.remove('increment');

        waits(50);
        $timeout.flush();

        expect(nagBeat.activeBeatsCount()).toEqual(0);
    });
});

Now everything works fine until I get to the last set of waits/$timeout.flush(). This test fails with the error:

No deferred tasks to be flushed

However that is the desired result, the timeout should no longer exist after I call nagBeat.remove().

Now maybe there is a way to test this functionality but when I remove the throw Error() from angular-mocks.js and run the test again it passes fine.

If there is a better way to test this type of functionality, please let me know. If not I would be happy to make this change.

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