SpyOn individually exported ES6 functions

前端 未结 2 1117
囚心锁ツ
囚心锁ツ 2021-01-17 19:05

tl;dr:

  1. I use Jasmine;
  2. I want to test aaa function which called bbb from the same module; <
2条回答
  •  自闭症患者
    2021-01-17 19:42

    Even thought is not the same framework, there is a related question that gives you why this doesnt work: How to mock functions in the same module using jest. Basically you wont be able to access this fixed reference of the function and instead make clear that is the same function as in the context of the module.

    I know this doesn't satisfy the constraint that you post in your question, but it just makes obvious that what you want to achieve is not possible by using spy.

    With JavaScript, there is no way to swap out references to something. You cannot swap out a function that is internal to a module. When you try to overwrite bbb with spyOn.and.returnValue in your example, you are just modifying the local binding bbb in your test but it has no effect on the bbb binding in your other file.

提交回复
热议问题