Let say I have an Express route which is handled by a controller. The controller uses a service and the service uses a repository to talk to a data source.
I want to create an integration test using Supertest to test that route :
test -> my Express app -> controller -> service -> repository -> data source
My problem is that I need to mock the repository/data source, to run the test! I want to hardcode some values as if they were coming from the real data source. What are my options?
In a Java world, I would be using dependency injection with Spring or Guice and I would replace the repository with a mock version that way. What are the patterns to achieve such mocking in a Typescript/Node.js world?
I guess using plain Javascript I could use Proxyquire
and its Globally override require feature, to mock the repository from the test itself. But I'm not sure this works well with Typescript.
So what are the recommended ways of mocking a "deep" component (a transitive dependency) from within a test file, using Typescript and Node.js?
IoC.use(..)
function, so in my test bootstrap file I add my mocks folders so that it looks there first, and will resolve my mock data source instead of the data source in my other folders or node modules. – Liam Gray