Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asynchronous tests when stubbing with sinon #1066

Closed
yamsellem opened this issue Oct 6, 2016 · 4 comments
Closed

Asynchronous tests when stubbing with sinon #1066

yamsellem opened this issue Oct 6, 2016 · 4 comments
Labels

Comments

@yamsellem
Copy link

This is a copy of this stackoverflow question — I'm encountering the same issue, and don't know how to process.

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"

The stackoverflow answer suggest to test.serialon every test — which works. But I'm wondering if there ain't no better way.

This may be totally on Sinon, but because I'm switching from Mocha to Ava, I start by asking it here.

Thanks a million for this 🚀 library.

@novemberborn
Copy link
Member

@vadimdemedes
Copy link
Contributor

If the thing that's worrying you is writing .serial for each test, you could use --serial flag, which makes all your tests run serially. As people on SO said, it complicates things that you stub the same dependency in multiple tests, so I guess --serial is the only workaround.

@alathon
Copy link
Contributor

alathon commented Oct 19, 2016

If it were possible to run each test in its own process, instead of each file, then this would be possible as each process would have access to its own copy of sinon. Incidentally not a bad idea, as an option - but I'm not sure its possible without additional syntactic sugar for that specific case, which may be unwanted. @sindresorhus @novemberborn any interest in such a feature, if it should come up as a PR? (and is even feasible, haven't dug too deep into the worker/process stuff)

@sindresorhus
Copy link
Member

any interest in such a feature, if it should come up as a PR? (and is even feasible, haven't dug too deep into the worker/process stuff)

No, it's not something we want to explore right now.
Relevant issue where we should have this discussion: #421

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants