jeudi 23 août 2018

How to mock nested dependency for integration tests

I'd like to create an integration test for my server. Unfortunately, there are many error cases which I can't cover as it depends on the failure of some dependency deep in my project.

To explain the issue a little bit easier, consider this project:

main:

const dep = require('./dep');

module.exports = () => {
    return dep();
}

dependency:

const uuid = require('uuid/v4');

module.exports = () => {
    return uuid();
}

test:

const t = require('tap');

const subject = require('./subject');

t.test('should return 12345', t => {
  // mock uuid/v4 module to return 12345

  const result = subject();

  t.equal(result, '12345');
  t.end();
});

This is a 1-level deep dependency situation but consider dependency to be like 5-levels deeper.

So I'm coming here with few questions:

  1. How can I achieve mocking something easily really deep in the project?
  2. Maybe I should ignore integration test cases which I can't cover and just rely on unit tests?

Important note:

I've tried numerous mocking libraries (mock-require, proxyquire, rewire, testdouble, mockery) already but none of them were created thinking of integration testing (they rather focus on unit tests).

Would love to hear your answers!

Aucun commentaire:

Enregistrer un commentaire