What is the reason to UNIT TEST like use test framework such Jest or Vitest, what is the benefit ? #128236
-
Select Topic AreaQuestion BodyHey guys, how's going? I just wondering about why we need to use a test framework and UNIT TEST for apps.I watched the documentation of the framework like Jest, and I'm confused about why we don't use console.log to displace UNIT TEST ? We can just test a function by printing a value and judge that type of it but why do we need to write another method like Compare the code in the following, I can not see any profit to using a test framework, Is there anybody tell me why ? it's so weird !!!
const sum = require('./sum');
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
console.log(sum(1, 2)); // 3 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Hi @gaomingzhao666, thanks for posting in GitHub Discussions! I've gone ahead and moved this post to our Programming Help category, as this topic might be more relevant there and hopefully someone give you a nudge in the right direction 😄 |
Beta Was this translation helpful? Give feedback.
-
I agree that a framework is often overkill. What matters is that the tests are useful. If you can do that without a library, great. One benefit of testing frameworks however is that they give helpful errors when tests fail. A test framework for the test you listed would typically generates output like:
Some even use a highlighted diff, to help spot differences in large compared objects. In contrast:
|
Beta Was this translation helpful? Give feedback.
I agree that a framework is often overkill. What matters is that the tests are useful. If you can do that without a library, great.
One benefit of testing frameworks however is that they give helpful errors when tests fail. A test framework for the test you listed would typically generates output like:
Some even use a highlighted diff, to help spot differences in large compared objects.
In contrast:
If you just use logging, you would have to check the results manually.
If you use a function call like
console.assert
that simply errors if the check fails, you will get a less specific error: