Issue
I want to test a variable function of a void function. How can I get that local variable in the test?
function:
save(){
const stringTest="test";
}
test:
test("variable should be equal to "test" ", () => {
component.save();
//test stringTest from save function that is equal to "test"
});
Solution
First, you cannot access a primitive local variable inside a function after you call it (at least not in conventional ways). The reason may seem obvious, still it is worth pointing out that local variables are released when javascript’s runtime finds them unusable.
Second, you do not want to access locals. Declaring a local variable is a kind of a statement that you want that variable to participate in some internal flow or algorithm. After all, not everything should be exposed to the outside world. Every system encapsulates internal behavior to some extent, and a function is also a system (a very small one, though).
Your function, save(), has neither input (arguments) nor output (return value, side effect), so nothing to test here…
Answered By – opel
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0