3

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"
  });

1 Answer 1

2

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...

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I will let my function untested

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.