Struggling to mock a third party library in typescript tests is proving to be quite a challenge.
Currently, I am developing a library based on the foundation of the typescript-starter library that utilizes ava for testing.
Specifically, I am attempting to mock the primary class of ioredis
to prevent my tests from establishing real database connections.
Various attempts have been made using different mocking libraries such as sinon, testdouble.js, and mockery.
For instance, in sinon, I experimented with the following:
let redisStub = sinon.stub(IORedis)
sinon.assert.called(redisStub.Cluster)
Using testdouble, I tried various strategies, such as:
td.replace('ioredis') // #1
td.replace('./homeMadeIoredisWrapperClass') // #2
Additionally, I explored the use of mockery
mockery.enable()
mockery.registerMock('ioredis', {some: 'object'})
Despite multiple attempts, including always using require('ioredis')
in the methods, the challenge persists.
Could it be that I am attempting the impossible? Mocking a database seems to be a common practice, yet the solution eludes me. Perhaps my approach is flawed, and I am targeting the wrong elements for mocking?
Any guidance on this matter would be greatly appreciated!
Ps. For context, I am striving to develop a straightforward ioredis connection wrapper.