dimanche 27 décembre 2015

Mocking node module dependencies containing browser-only code

I'm currently trying to test individual node modules that I bundle for the browser using Browserify. I'm using a combination Rewire and Sinon to mock any dependencies I don't want to test. This works for the most part.

My main issue is when I require a module that contains another require call to a module that has a front-end dependecy.

// This is module #1 - what I'm wanting to test
var module2 = require('module2');

// This is module #2 - which I can mock with rewire
var module3 = require('module3'); // This is still executed
module.exports = something;

// This is module #3
var audioContext = new AudioContext(); // Browser-only API
module.exports = something

I want to mock the require in module #2 completely. I don't want it even trying to require module #3 because it dies when it doesn't understand code intended for the browser. Mocha spits out:

ReferenceError: AudioContext is not defined

Is this possible? If not, what's a good workaround? I suppose I could use Karma to launch the tests in a browser, but that seems like a big overhead just to get a dependency I'm not wanting to test to work.

Aucun commentaire:

Enregistrer un commentaire