Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with ava asynchronous tests when stubbing with sinon

I have a couple of tests I'd like to run on the .then and .catch blocks of one of my dependencies.

import test from 'ava';
import sinon from 'sinon';

// Fake dependency code - this would be imported
const myDependency = {
    someMethod: () => {}
};

// Fake production code - this would be imported
function someCode() {
    return myDependency.someMethod()
        .then((response) => {
            return response;
        })
        .catch((error) => {
            throw error;
        });
}

// Test code

let sandbox;

test.beforeEach(() => {
    sandbox = sinon.sandbox.create();
});

test.afterEach.always(() => {
    sandbox.restore();
});

test('First async test', async (t) => {
    const fakeResponse = {};

    sandbox.stub(myDependency, 'someMethod')
        .returns(Promise.resolve(fakeResponse));

    const response = await someCode();

    t.is(response, fakeResponse);
});

test('Second async test', async (t) => {
    const fakeError = 'my fake error';

    sandbox.stub(myDependency, 'someMethod')
        .returns(Promise.reject(fakeError));

    const returnedError = await t.throws(someCode());

    t.is(returnedError, fakeError);
});

If you run either test alone, the test passes. But if you run these together, the setup for the test A runs, and then before it completes, the setup for test B runs and you get this error:

Second async test
   failed with "Attempted to wrap someMethod which is already wrapped"

Maybe I'm not understanding how I should be setting up my tests. Is there way to force Test A to complete before Test B starts running?

like image 346
sak_to Avatar asked Jun 18 '16 19:06

sak_to


1 Answers

AVA tests are run concurrently, which messes up your Sinon stubbing.

Instead, declare your tests to be run serially and it should work:

test.serial('First async test', ...);
test.serial('Second async test', ...);
like image 159
robertklep Avatar answered Nov 03 '22 04:11

robertklep