Is there any way to mock private functions with Jest?

前端 未结 4 1761
遥遥无期
遥遥无期 2020-12-10 01:36

The ES6 module that I want to test looks as follows:

function privateFunction() {
   ...
}
export function publicFunction() {
   ... does something ...
   pr         


        
4条回答
  •  -上瘾入骨i
    2020-12-10 01:59

    There is no way through the nature of JavaScript. The function is bound to the scope of the module, so there is no way to know that this function exists from the outside, so no way to access the function and in the end no way to mock it.

    Maybe more important, you should not test on the internals of the object under test but only the public API. Cause that is everything that counts. No one cares how stuff is done internally as long as the public API stays stable.

提交回复
热议问题