Skip to content

Jasmine FAQ (Unit Testing)

htsungwing edited this page Feb 2, 2016 · 16 revisions

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 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] "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] 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 test-driven approach to ensure the software comply with the intended usage (validation) and it fulfills the requirements (verification). 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.

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?

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?

I wrote some tests. How do I run them?

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

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. History/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)

Clone this wiki locally