Unit Testing Tool Jasmine - nighttourist/Batch-Management-System GitHub Wiki
Jasmine
Niaz Rahaman
Jasmine is a popular behavior-driven development (BDD) testing framework for JavaScript. It allows developers to write clear and structured tests for their JavaScript applications, both in the browser and in Node.js environments. Jasmine doesn't depend on any other JavaScript frameworks.
Key Features of Jasmine:
- Zero Dependencies: Jasmine is a standalone framework that doesn’t require any additional libraries.
- Behavior-Driven Development (BDD): Tests are written to focus on your application's behaviour, making them easy to understand.
- Assertions: Jasmine provides a rich set of matchers like
toBe()
,toEqual()
,toContain()
, to assert whether certain conditions are met. - Test Suites and Specifications:
- A suite (
describe
block) is a group of related tests, while - A specification or spec (
it
block) is an individual test case.
- A suite (
Basic Syntax:
describe
: Defines a suite of tests or a group of test cases.it
: Defines a single test (spec) that expects a certain behavior.expect
: Asserts the expected outcome.- Matchers: Functions that compare the actual result with the expected result.
Example:
javascript
describe("A simple Jasmine test suite", function() {
it("checks if a variable is true", function() {
let flag = true;
expect(flag).toBe(true); // Assertion using 'toBe' matcher
});
it("compares two objects", function() {
let obj1 = { key: 'value' };
let obj2 = { key: 'value' };
expect(obj1).toEqual(obj2); // 'toEqual' is used for deep equality check
});
});
Setup
Step 1: Install Node.js
Jasmine is a JavaScript testing framework, and to install it, you need to have Node.js installed on your machine.
- Download Node.js from the official website: Node.js Downloads.
- Run the downloaded installer and follow the installation steps.
Step 2: Initialize a Node.js Project
Navigate to your project folder in Command Prompt or PowerShell:
cd path\to\your\project
Initialize a package.json
file for your project (if you don’t have one already):
npm init -y
This will create a package.json
file with default settings in your project folder.
Step 3: Install Jasmine
Install Jasmine as a development dependency:
npm install --save-dev jasmine
Initialize Jasmine in your project:
npx jasmine init
This will create the necessary configuration files for Jasmine, such as spec/support/jasmine.json
.
Step 4: Create Test Files
Create a directory for your test specs (if it doesn't exist):
mkdir spec
Inside the spec
folder, create a test file, for example, example.spec.js
:
// spec/example.spec.js
// Test Suite
describe("Basic Math Operations", function() {
// Test 1: Add two numbers
it("should return 5 when 2 and 3 are added", function() {
let sum = 2 + 3;
expect(sum).toBe(5); // Check if sum is 5
});
// Test 2: Multiply two numbers
it("should return 12 when 3 is multiplied by 4", function() {
let product = 3 * 4;
expect(product).toBe(12); // Check if product is 12
});
// Test 3: Subtract two numbers
it("should return 1 when 4 is subtracted by 3", function() {
let difference = 4 - 3;
expect(difference).toBe(1); // Check if difference is 1
});
// Test 4: Divide two numbers
it("should return 2 when 10 is divided by 5", function() {
let quotient = 10 / 5;
expect(quotient).toBe(2); // Check if quotient is 2
});
// Test 5: Check if a number is greater than another number
it("should verify that 10 is greater than 5", function() {
let a = 10;
let b = 5;
expect(a).toBeGreaterThan(b); // Check if a > b
});
});
Step 5: Run Jasmine Tests
- In your Command Prompt or PowerShell, run Jasmine tests with:
npx jasmine
Matchers:
Some common Jasmine matchers include:
toBe
: Strict equality comparison (===
).toEqual
: Deep equality comparison.toBeTruthy
: Checks if a value is truthy.toBeFalsy
: Checks if a value is falsy.toContain
: Checks if an array or string contains a certain value.toThrow
: Checks if a function throws an error.
Spies:
Jasmine also provides support for spies to monitor the behaviour of functions, allowing you to track calls, arguments, and the results returned by the function.
describe("A spy example", function() {
let myFunc;
beforeEach(function() {
myFunc = jasmine.createSpy('myFunc');
});
it("tracks the function call", function() {
myFunc();
expect(myFunc).toHaveBeenCalled(); // Checks if the function was called
});
});
Output:
niaz@niaz-Latitude-E5450:~$ npx jasmine
Randomized with seed 70985
Started
.....
5 specs, 0 failures
Finished in 0.015 seconds
Randomized with seed 70985 (jasmine --random=true --seed=70985)
Advantages:
- Readable syntax: Jasmine’s BDD syntax is easy to read and understand, even for people who aren’t familiar with the code.
- Standalone: No dependencies on external libraries make it lightweight.
- Support for async testing: Jasmine can handle asynchronous code using
done()
callback or promises.