javascript - Mocha and Chai test fails when testing function with setInterval - Stack Overflow

admin2025-04-08  1

I'm new to TDD and working with Mocha and Chai. I have created a test that passes when a value is increased, but when that increase is put within a setInterval, it fails. The objective of this code is to have something move across the screen.

function startMovingThing(){
    var position = setInterval(function() {
        moveThing(10);
    }, 100);
}

function moveThing(number){
    thing.position += number;
    thingOnScreen.style.left = thing.position + 'px';
}

test:

describe('Thing', function() {

    it('should increase position', function(){
        assert.increases(startMovingThing, thing, 'position');
    });

});

How can I get this test (or what should the test be) to pass?

I don't want moveThing() to be outside of the interval, because if the interval is cleared and the function is called, the thing should not move.

I'm new to TDD and working with Mocha and Chai. I have created a test that passes when a value is increased, but when that increase is put within a setInterval, it fails. The objective of this code is to have something move across the screen.

function startMovingThing(){
    var position = setInterval(function() {
        moveThing(10);
    }, 100);
}

function moveThing(number){
    thing.position += number;
    thingOnScreen.style.left = thing.position + 'px';
}

test:

describe('Thing', function() {

    it('should increase position', function(){
        assert.increases(startMovingThing, thing, 'position');
    });

});

How can I get this test (or what should the test be) to pass?

I don't want moveThing() to be outside of the interval, because if the interval is cleared and the function is called, the thing should not move.

Share Improve this question edited Mar 3, 2016 at 19:05 MikeBird asked Mar 3, 2016 at 17:13 MikeBirdMikeBird 3961 gold badge5 silver badges17 bronze badges 2
  • With the code above it's not clear how the code should work. Is increases a function from an assertion library? where thing is defined? – thitemple Commented Mar 3, 2016 at 18:23
  • increases is from the Chai assertion library. Thing is defined in my javascript – MikeBird Commented Mar 3, 2016 at 19:06
Add a ment  | 

1 Answer 1

Reset to default 10

Ok, the problem is that you're using setInterval which is async and you're trying to assert that the value was changed in a synchronous way.

Here's a modified version of your test, using sinonjs to simulate that the time passed.

var assert = require('chai').assert;
var sinon = require('sinon');

var thing = { position: 0 }
var thingOnScreen = { style: { left: '' } };

function startMovingThing(){
    var position = setInterval(function() {
        moveThing(10);
    }, 100);
}

function moveThing(number){
    thing.position += number;
    thingOnScreen.style.left = thing.position + 'px';
}

describe('Thing', function() {

  beforeEach(function() {
    this.clock = sinon.useFakeTimers();
  });

  afterEach(function() {
    this.clock = sinon.restore();
  });

  it('should increase position', function(){
    startMovingThing();
    this.clock.tick(101);
    assert.equal(thing.position, 10);
  });
});

In summary, sinonjs is replacing the global functionality of setInterval and is executing the code without having to wait for the specified milliseconds.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744124679a232410.html

最新回复(0)