DutchKevv
DutchKevv

Reputation: 1699

Unit testing javascript of a backend driven website

I implemented NightWatch for (Behavior driven) testing a couple of websites. All of them are backend driven (Java, JSP).

Now some of the employees say its the wrong choice, and it should be a unit test framework.

But the front-end doesn't have any 'units', it simply reacts on the HTML present on the page, and binds some plugins / Vanilla JS Class to it.

Am I wrong, that unit testing would be simple impossible for the front-end, as there are (almost) no functions that simply return a value. They are all bound on page-load to an HTML element.

For example:

<div data-components="myComponent"><div>inner</div></div>

Loads some JavaScript (myComponent), that will alter/add/remove some HTML from the page. It does not 'return' anything. It updates the page.

All advice is more than welcome.

Upvotes: 0

Views: 125

Answers (1)

Halcyon
Halcyon

Reputation: 57709

Unit testing is one of many ways to test your code. If you have existing code that doesn't have any tests yet there's a good chance you're not going to be able to test it with unit tests. Unit testing has to be considered when designing the application. Unit testing is popular because good design and testability tend to go hand in hand. So unit tests can be seen as a matter of code quality, eventhough it's possible to write really bad unit tests.

That being said, testing things like user interfaces tend to fall outside the realm of unit tests. It's not usually possible to design your UI-code in such a way that you can write usefull tests. The approach here is to remove as much logic as you can from untestable parts.

At my company we have a large unit test suite for server side code and client side code (JS) and a couple of Selenium tests. It could very well be that your work would envolve only things testable by Selenium tests.

It depends on what kind of things you're making. Some user-interfaces can very complex and elaborate and you definitly want to test those. At the same time, look for oppurtunities for unit tests.

Upvotes: 2

Related Questions