ViewBlock / binance-api-node

:chart: A complete and heavily tested wrapper with typings for the Binance API.
656 stars 496 forks source link

A library that mocks binance-api-node #559

Open martudev opened 2 years ago

martudev commented 2 years ago

Hello, how are you? First of all, I wanted to thank you for having made this library, it turned out great!

Regarding the "issue", a few months ago I have been developing a bot with its library and it became somewhat extensive, therefore I decided to add tests, so I decided to add mocks, and for that I should mock the binance-api-node library, to mock it I use a library called sinon but this library does not support mocking libraries with an export default as constructor function, so I was forced to use another library called proxyquire, and I was thinking that the code I made could be useful to other people who also need mocker for example the "order" function or some other, and I wanted to propose that it would be good if you put this library that I am going to create in your documentation so that other people can use this mock and test what they need with your api. Tell me what you think about it, would something like this be viable? I remain at your disposal

Greetings! martudev

JonathanLoscalzo commented 2 years ago

I think you can try:

// in service.ts
import BinanceFactory, { Binance, DepositHistory, DepositHistoryResponse } from 'binance-api-node';
class Service {
  client: Binance;
  constructor() { this.client = BinanceFactory({ apiKey: config.BINANCE_APIKEY, apiSecret: config.BINANCE_SECRET }); }
}

// in your tests, such service.test.ts
test("mock deposit", async ()=>{
  service = new Service();
  const spy = jest.spyOn(service.client, 'depositHistory').mockResolvedValue([]);
  await service.client.depositHistory() // [] mock response

  expect(spy).toBeCalled();
})
martudev commented 2 years ago

Hi @JonathanLoscalzo yes thats works perfectly! im searching for a way that i dont want to expose this.client because i dont want that the people how use Service make things with this.client so i need to use the librarys i mention to mock or spy that functions. But is a great proposal!! i keep it in my mind! thanks for sharing 😊

JonathanLoscalzo commented 2 years ago

Yes, is a workaround.. I guess you could create a "BinanceWrapper", and export all functions, or...

martudev commented 2 years ago

Yes, ill need the first one or if its a default export function i need to use external library like proxyquire to mock that function. About the second point i dont try it but i read that is not the best thing to do if you want to mock because not have control at all of the mocking functions, maybe im wrong with this but i need to try it and i awnser you in this aspect! again thanks for all!