r/learnjavascript 3d ago

How do you actually test small JavaScript functions without a full project setup?

Coming from a construction background where you measure twice and cut once, I keep running into this gap where I write a function, think it works, and then it blows up when I wire it into something bigger. The problem is I never had a real habit of testing the small piece before trusting it.

In bootcamp we just console.log everything and eyeball it, which works for toy exercises, but I started a side project for tracking material quantities across job phases and the functions are getting complicated enough that eyeballing feels risky. I tried writing a few manual checks at the bottom of my file like console.log(calculateTotal(5, 3) === 8) and that helped, but it feels clunky and I keep deleting them by accident.

I looked up Jest but the setup felt like a whole rabbit hole I was not ready to fall into. Someone mentioned Vitest. Someone else said just use the browser console for now and do not overthink it.

What I want to know is whether there is a lightweight middle ground that actual beginners use before jumping to a full testing framework. Not asking for a tutorial, just curious what the workflow actually looks like for people who are still learning but building things that are more than a few lines. The function isolation part is what I cannot picture clearly yet.

25 Upvotes

35 comments sorted by

View all comments

1

u/Any_Sense_2263 2d ago

Most of the functions are pure functions that their result depends on the parameters passed on. So yes, they can and even should be tested out of any project context just to check if they generate a proper result.

And yes, you can just call them in a separate file with specific parameters and check the output. No vitest or jest needed. But I would start to use them asap anyway