mfncooper / mockery

Simplifying the use of mocks with Node.js
Other
1.1k stars 60 forks source link

Simpler API for using mockery #28

Open 3den opened 9 years ago

3den commented 9 years ago

I have been working mockery for some time but since the core API is verbose we have create a wrapper to simplify the use of mockery but i think it would be nicer to have something like that merged in the official repo.

// This is the wrapper we use to mock
function mockRequire(mocks, callback) {
    mockery.enable({
        warnOnUnregistered: mocks.warnOnUnregistered,
        useCleanCache: !mocks.useCache
    });

    Object.keys(mocks).forEach(function (lib) {
        mockery.registerMock(lib, mocks[lib]);
    });

    try {
        callback(mocks);
    } finally {
        mockery.deregisterAll();
        mockery.disable();
    }
}

// This are the tests for this feature
describe('#mockReqire', function () {
    it('mocks some libs', function () {
        var tdMocked;
        var fsMocked;

        mockRequire({
            '../index': 'I am Mocked!',
            'fs': 'I am NOT real!'
        }, function () {
            tdMocked = require('../index');
            fsMocked = require('fs');
        });

        assert.equal(tdMocked, 'I am Mocked!');
        assert.equal(fsMocked, 'I am NOT real!');

        assert.notDeepEqual(tdMocked, require('../index'));
        assert.notDeepEqual(fsMocked, require('fs'));
    });
});

// Usecase Example                                                                                                                                                                        
describe('SomeComponent', function () {      
    var SomeComponent;

    mockRequire({                                                                                                                                                            
        '../services': mockServices,                                                                                                                           
        'api': mockAPI                                                                                                                                                    
    }, function () {                                                                                                                                                         
        SomeComponent = require('../../services/yFinLists/trending_tickers');                                                                                      
    });                                                                                                                                                                   

    it('tests stuff on the component', function () {                                                                                                               
          ...                                               
    });

The advantage is because you can mock several modules at once and there is not need to use before/after hocks. This feature is very useful for the projects I am working and I would be glad to submit a PR.

bendrucker commented 9 years ago

That's basically exactly what proxyquire's API looks like. Have you tried it?

3den commented 9 years ago

I never tried proxyquire but it looks very similar to what i am currently doing with mockery the main difference is that proxyquire returns an object with the mocked stuff and the mockRequire gives you a callback to do whatever you want.

I think that implementing something like what was suggested above on mockery would still be helpful. But thanks for the suggestion :+1: