0

I'm developing postcss plugin and want to test it with mocha. Here is my test:

function exec(cb) {
    postcss()
        .use(regexp)
        .process(source)
        .then(cb);
}

it('should add warnings to messages', function(done) {
    var expected = 'somemessage';
    var message = '';

    function getMessage(result) {
        message = result.messages;
        assert.equal(message, expected);
        done();
    }

    exec(getMessage);
});

But it fails and I get Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.

What am I doing wrong?

Katya Pavlenko
  • 3,303
  • 3
  • 16
  • 28

2 Answers2

0

Your callback is not getting called within the default timeout of 2000 ms.

If you are sure that there is nothing wrong in your exec plugin and is expected to take more than 2s , you can increase the time using

In mocha testing while calling asynchronous function how to avoid the timeout Error: timeout of 2000ms exceeded.

Community
  • 1
  • 1
jozzy
  • 2,863
  • 3
  • 15
  • 12
0

I've found solution myself! We need to return promises in exec and it, and so then there is no need in done()

function exec(cb) {
    return postcss()
        .use(regexp)
        .process(source)
        .then(cb);
}

it('should add warnings to messages', function() {
    var expected = 'somemessage';
    var message = '';

    function getMessage(result) {
        message = result.messages;
        assert.equal(message, expected);
    }

    return exec(getMessage);
});
Katya Pavlenko
  • 3,303
  • 3
  • 16
  • 28