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?
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', ...);