最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

typescript - Mock exported instances of private classes - Stack Overflow

programmeradmin2浏览0评论

I have a class "SomeService" which isn't exported. I export the instance which can be used to call functions on it. Basicly a singleton. Here the example

export const someService = new SomeService(otherService.getConfigObject());

Now when I write unit tests (Currently using sinon and mocha) I can't really mock the otherclass like i would like to. In the beforeEach the instance is already created with a real object.

The only thing that worked is

  1. import sinon
  2. stub otherClass
  3. import someService instance

Which seems very hacky and my formatter (correctly) puts the imports on top.

Is there a convenient way to mock the constructor or otherClass to give my instance a mockConfig? Should i reconsider the architecture choice to export created instances?

发布评论

评论列表(0)

  1. 暂无评论