Jasmine is a Javascript Testing framework which starts with behavioral specifications as a basis for writing test cases. To put it another way, you can consider user stories as a reference for write test cases. Each user story in your project can have one or more Jasmine test cases.
Majorly, Jasmine is made up of three functions
- describe
- it
- assertions (expect)
Like standard frameworks, it also has setup method and tear down methods
- beforeEach() – Setup Method
- afterEach() – Teardown Method
For the sake of simplicity of post, I would not discuss much in detail. However, you can navigate to official Jasmine guide for deep dive.
Download Jasmine
You can download latest version of Jasmine from here. Once downloaded, you can extract and save lib folder in your project.
Simple Javascript code for Jasmine Test Framework
Let’s consider below Javascript code. It simply declares a person object with few properties and method to get full name and change Age.
Its time to see how we can write a test method using Jasmine Framework.
Above code snippet shows how we are using describe, it and expect methods provided by Jasmine framework.
Let’s create a sample HTML page which will load Jasmine Framework, our Javascript code and Test method to Test our code.
Below is our files and folders structure
Below is the output of our example. As all test passed, we don’t see any error
In case, if some method fails test execution, then it would show error report like below image
First 25 minutes of below video is basics of Jasmine Test Framework with same example as of this post.
Leave a Reply