Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing a modular Javascript web-app

I am building a web app using BackboneJS and RequireJS and need to implement some form of unit testing for UI interaction and data retrieval via AJAX. I have come across QUnit and Jasmine but don't really know how I can integrate this into my app.

If I am testing things such as:

  • Is the user logged in alright?
  • Has the data been received from the server ok?
  • Does clicking a button trigger the expected response?
  • Do click events work on dynamically loaded html content?
  • Does the app respond correctly to changes in hash/push-state urls?

I would imagine the testing has to be directly integrated into my app so as to have access to specific JS objects, work with session specific data and respond to changes in push state URLs.

How can I integrate QUnit or Jasmine (or other suggestions) into my modular app to unit test such features?

like image 275
wilsonpage Avatar asked Nov 21 '11 12:11

wilsonpage


People also ask

Can we write unit test for JavaScript?

JavaScript Unit Testing is a method where JavaScript test code is written for a web page or web application module. It is then combined with HTML as an inline event handler and executed in the browser to test if all functionalities are working as desired. These unit tests are then organized in the test suite.

Can we use cypress for unit testing?

Cypress is an open source end-to-end testing framework that can help you unify web application testing. It works for all programming languages, platforms, and browsers. You can use Cypress for unit and integration testing. All tests are written in JavaScript and run in real browsers.

What is JUnit JavaScript?

JUnit is an open source Unit Testing Framework for JAVA. It is useful for Java Developers to write and run repeatable tests.


1 Answers

Unit testing is really simple.

You make a test HTML page. You include QUnit/NodeUnit/Jasmine/TestLibraryOfChoice

You then use requireJS and load one of your javascript modules,

and you simply test the exported object or function. That means testing the valid inputs of your module and asserting the outputs are correct.

You may have to mock out ajax and write HTML mocks

like image 114
Raynos Avatar answered Sep 23 '22 09:09

Raynos