Jasmine FAQ (Unit Testing) - devuxd/SeeCodeRun GitHub Wiki

Unit Testing with Jasmine

### Background Overview Jasmine is developed by Pivotal Labs and was once named JsUnit. Eventually it is transformed to the known Jasmine Framework. "Jasmine is a behavior-driven development framework for testing JavaScript code. It does not depend on any other JavaScript frameworks. It does not require a DOM. And it has a clean, obvious syntax so that you can easily write tests. This guide is running against Jasmine version 2.0.0."[2] Jasmine is excellent for JavaScript testing and only require a JS enabled browser along with the Jasmine script. It uses testing suites where testing results are compared with expected values. A set of predefined matchers can be called to test the system. The framework can be downloaded from GitHub along with a page of instruction that guides a user to understanding how to run tests in most JavaScript supported browser.[1] Review sources below for more information regarding basics of Jasmine framework.

I'm implementing a page in Aurelia. What can I test? What should I test?

The tests will be designed and implemented based on the requirements and functions of the software application. Using a behavior-driven approach that is more business value perspective to ensure the software comply with the intended usage (validation) and it fulfills the requirements (verification). This type of development also emphasizes on writing unit tests and from outside-in (focus on the goal of a piece of code). Some area of testing that can be done including user inputs, database integrity, and source code reachability using different sets of coverage criterion. These are also things that should be tested to ensure all the test requirements are satisfied, which are derived based on the functionality of this software.

Individual developers will be responsible for ensuring the correctness and robustness of the code they write and develop Unit tests for possibly each use case scenario of the system.

Do I need to use setup or teardown to setup or destroy any state?

Jasmine provides an organized and consistent way inside each describe() function using the beforeEach() function to setup state before running each test and afterEach() function to teardown the remaining state after running each test inside a spec. These two functions are not required to have inside the describe() function, however, it is definitely important to put the system under the state that is necessary to run a test correctly.

describe(name,function) - a function for grouping related specs with a given name.

Most of the behavior that I want to test depends on data loaded from Firebase in the cloud. How do I test this?

Jasmine provides the function of setting the states before each test using beforeEach(). A tester can load the necessary data from Firebase by creating a reference to the firebase database using the firebase api and pass in the URL to the firebase app. After retrieving the data, they can be set to the current session variables for further testing purpose.

I'm writing some functionality in a new JS file. Is there a convention on where I should put the corresponding tests in the file structure?

There isn't any restriction or special conventions of where the code should be placed with the corresponding tests. Generally in other unit testing frameworks and languages (ex. JUnit in Eclipse), all the unit tests are stored together in a same class and created in separate functions. Similarly, Jasmine can group similar test together using "a test suit begins with a call to the global Jasmine function describe with two parameters: a string and a function", and the convention is to name the suite with what functionality or code is under testing [2].

I wrote some tests. How do I run them?

One way to run your test is to first download the standalone distribution which contains all the libraries you need to start running Jasmine. Unzip the file and open SpecRunner.html will allow you to start running the default/included specs (test suites). To run your own customized test, you simply replace the link for the respective specs inside the head section of the SpecRunner.html with your own customized specs.

What is a Spy, and when should I use it?

A spy is a function that is used for creating test doubles. It can be used to "stub any function and tracks calls to it and all arguments. A spy only exists in the describe or it block in which it is defined, and will be removed after each spect[2]." Generally, a test double is used whenever you try to test the system under conditions that might cause a permanent effect on the current system, therefore you create a "double" to protect your current system from the after effect.

Where can I learn more about Jasmine?

Below are sources where I referenced most of the information for creating this wiki page. You will be able to learn most of important usage related details through the link of Jasmine Introduction.

  1. Jasmine Background (http://www.htmlgoodies.com/beyond/javascript/testing-javascript-using-the-jasmine-framework.html)
  2. Jasmine Introduction (http://jasmine.github.io/2.0/introduction.html)
  3. Jasmine on Github (https://github.com/jasmine/jasmine)
  4. Jasmine Release (https://github.com/jasmine/jasmine/releases)
  5. Jasmine Unit Test (https://www.adobe.com/devnet/archive/html5/articles/unit-test-javascript-applications-with-jasmine.html)
⚠️ **GitHub.com Fallback** ⚠️