3

Using proxyquire I'm mocking a method of module B (injected with require() in module A), when testing the method in the module A. The mock (mocking get_campaigns method of admitad.model.js module):

const admitadModelMock = {
    '../services/admitad.model': {
        get_campaigns: (limit, page) => new Promise((resolve, reject) =>
            setTimeout(resolve({campaigns: testData, count: 1000}), 5000)
        ),
    },
};

The test:

it('shold get all campaigns from Admitad', async function () {
    this.timeout(60000);
    let err, data;
    // mock dependencie (get_campaigns() of module B will be mocked):
    let $serviceStubbed = proxyquire('../services/campaign-sync', admitadModelMock);
    // getAdmitadCampaigns() just calls get_campaigns method of module B
    [err, data] = await to($serviceStubbed.getAdmitadCampaigns(50));
    data.length.should.be.equal(50);
});

The problem is the test passing without expected delay of 5 seconds.

Update.

This is working:

setTimeout(() => resolve({campaigns: mockedCampaigns, count: 1000}), 2000)

Here is my final nice approach:

// helper to wrap timeout generation
const timer = (data, time) =>
    new Promise((resolve, reject) =>
        setTimeout(() => resolve(data), time)
    );

// Factory function to generate mock with data we need
const blacklistedModelMockFactory =
    (onRead = [], onUpdate = 'Ok', onCreate = 'Ok', onDelete = 'Ok') => ({
        '../services/campaigns-blacklist.model': {
            read: () => timer(onRead, 2000),
            update: () => timer(onUpdate, 2000),
            create: () => timer(onCreate, 1000),
            delete: () => timer(onDelete, 1000),
        },
    });

// Test example
it('should filter Registered and Blacklisted collections', async function () {
    this.timeout(60000);
    $service.should.have.property('filterRB').a('function');
    const sourceRegistered = mockedCampaigns.slice(5, 10);
    const sourceBlacklisted = mockedCampaigns.slice(15, 18);
    let error, success;
    // mock dependencies in tested module with our expected data:
    let $serviceStubbed = proxyquire(
        '../services/campaign-sync', Object.assign(
            {},
            blacklistedModelMockFactory(sourceBlacklisted),
            registeredModelMockFactory(sourceRegistered)
        )
    );
    [error, success] = await to($serviceStubbed.filterRB(mockedCampaigns));
    expect(error).to.be.equal(null);
    success.filtered.length.should.be.equal(12);
    success.blacklisted.length.should.be.equal(3);
    success.registered.length.should.be.equal(5);
});

1 Answer 1

5
setTimeout(resolve({campaigns: testData, count: 1000}), 5000)

The above line call flow can be explained as below.

let res = resolve({campaigns: testData, count: 1000});
setTimeout(res, 5000);

You don't want that, do you :-)

Try,

setTimeout(() => resolve({ campaigns: testData, count: 1000 }), 5000)

as it wraps the resolve call inside an anonymous function and passes that as 1st parameter to setTimeout call.

3
  • 1
    Sanks a lot! Can you please check my update? Is there any way to make timeout without creating new promise object and resolving it, couse it will strongly litter the code? Feb 16, 2018 at 16:19
  • No problem, Done it ) Feb 16, 2018 at 16:43
  • @AntonPegov Okay
    – explorer
    Feb 19, 2018 at 6:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.