lundi 29 août 2016

How to rewire only some object parameters during unit testing?

I have one module that contains an object. I would like to rewire only some of the object's parameters. Let's say my module 'module.js' looks like this:

var obj = {
  param_A: 'valueA',
  param_B: 'valueB',
  param_C: 'valueC'
}

And in the test file I import the module and use rewire.

var rewire = require('rewire');
var module = rewire('../module.js');

describe('Unit-Test', function () {
  beforeEach(function () {
    module.__set__({
      'obj': { param_B: 'newValueB' }
    });
  });

...

});

Now I have overwritten the entire object and the parameters A and C do not exist anymore. How can keep them? The only thing that comes to my mind is something like this

module.__set__({
  'obj': {
    param_A: module.obj.param_A,
    param_B: 'newValueB',
    param_C: module.obj.param_C
  }
}

But I feel like there must be a better way.

Aucun commentaire:

Enregistrer un commentaire