Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Injecting mocks into Browserify for testing

I know, Browserify isn't really a DI framework, but is it possible to "inject", or somehow fake injecting, mock data into an application during unit testing?

For example, to test the function:

var MyModel = require('./models/My.js');

function doSomething() {
  // do something with model.
}

with a mock My.js, like

describe('Do Something', function() {

  beforeEach(function() {
    // replace './models/My.js' with a Mock implementation.
  });

  it('with model', function() {
    // ... test
  });
})

what goes in the beforeEach function?

like image 964
nicholas Avatar asked May 01 '14 05:05

nicholas


1 Answers

There are a few tools for mocking require calls in browserify.

  • https://github.com/thlorenz/proxyquireify
  • https://github.com/i-like-robots/rewireify
  • https://github.com/thlorenz/browserify-swap
  • https://github.com/Colingo/mock
  • https://github.com/mfncooper/mockery

I haven't personally used these. Also, mockery wasn't written with Browserify in mind so mockery might not even work. The others were written for Browserify though so they should work with little effort. :) Proxyquireify and Rewireify seem to be the only 2 active within the last year though.

like image 150
dannyfritz Avatar answered Sep 24 '22 06:09

dannyfritz