Skip to content Skip to sidebar Skip to footer

Javascript Unit Test

I have a javascript app within 250 lines and want to add tests to that. Whenever i make a tiny change, i have to run tests for atleast 10 cases manually, which i want to automate.

Solution 1:

Unit Testing frameworks are helpful because the provide environments that will run the tests and give you feedback about them. To not use one would be to take implementation of those responsibilities on yourself and would be much more work than it sounds like you are trying to do.

If you are using node, setting up a unit testing framework is very easy. I like to use Karma as a test runner with the mocha testing framework and chai assertion library.

Solution 2:

I would go with a framework. While your use case might be simple now, you might find yourself in a project where you need more functionality down the road. You should be familiar with at least one testing framework/library for any language you use seriously. The same goes for build systems and package management.

For javascript, I've only used mocha. It's pretty comprehensive, easy to learn, can be used in browser or with node, and tests actually look really clean and easy to read.

Also, with mocha, you can choose your own assertions library, I use chai. It's built with tdd and bdd in mind, and it makes writing assertions feel more like writing natural sentences than code. Of course, you can still use whatever default assertion facilities your js environment gives you, if any.

Solution 3:

its really hard to setup automated unit tests with plain javascript without any framework. if you try to reinvent the wheel of existing javascript unit testing framework yourself, that might be huge effort than your 250 lines of code for which you are writing test.

so if you are going for an framework, jasmine and qunitjs are promising unittesting framework for javascript.

http://jasmine.github.io/

https://qunitjs.com/

Post a Comment for "Javascript Unit Test"