Quick note on this challenge in the back-end development & API course:
The testing code requires you to read the value of process.env.MESSAGE_STYLE as is.
If you store this value in a const, the testing logic won’t let you pass even if your response object contains the correct result.
not exactly, the issue is where you store the value of the environment variable. If you do it inside the endpoint, it’s fine, if you do it in the global scope it’s not because it captures the value at the start of the app, while the tests change the value during the app execution
2 Likes
Interesting, but the output value is the same though, so what prevents it being a valid result?
if the value of the environment variable changes during app execution, no the value is not the same.
Consider:
let mock_environment_variable = "lowercase";
let my_variable = mock_environment_variable;
function my_func() {
if (my_variable === "uppercase") {
return "MESSAGE"}
return "message"
}
console.log(my_func()) // prints "message"
mock_environment_variable = "uppercase";
console.log(my_func()); // still prints "message"
and consider this other situation:
let mock_environment_variable = "lowercase";
function my_func() {
let my_variable = mock_environment_variable;
if (my_variable === "uppercase") {
return "MESSAGE"}
return "message"
}
console.log(my_func()) // prints "message"
mock_environment_variable = "uppercase";
console.log(my_func()); // now prints "MESSAGE"
which of the two manages correctly when the mock_environment_variable
changes?