Featured image of post Jasmine in a Nutshell

Jasmine in a Nutshell

A Behavior Driven Development (BDD) JavaScript Testing Framework

Jasmine in a Nutshell: The Friendly JavaScript Testing Framework

If JavaScript testing frameworks were people, Jasmine would be that super chill friend who’s always there when you need them, doesn’t ask too many questions, and makes your life easier.

Jasmine is one of the most widely used testing frameworks for JavaScript.

It’s clean, simple, and requires minimal setup. Plus, it’s designed with behavior-driven development (BDD) in mind, so your tests are easy to read and understand.

Let’s take a deep dive into Jasmine and see why it’s such a popular choice for unit testing JavaScript applications.


Why Jasmine?

Jasmine has been around for a while, and there’s a reason developers keep coming back to it.

  • No Dependencies – Unlike some testing frameworks (cough Mocha cough), Jasmine doesn’t require extra libraries like Chai or Sinon.
  • Easy-to-Read Syntax – You define tests with describe() and it(), making them super readable.
  • Built-in Spies – It comes with spying and mocking capabilities right out of the box.
  • Runs Anywhere – Works in browsers, Node.js, and with test runners like Karma.

Installing Jasmine

Setting up Jasmine is pretty painless. If you’re using Node.js, just install it via npm:

1
npm install --save-dev jasmine

Then, initialize it:

1
npx jasmine init

If you’re using it in a browser, just grab the standalone package from the Jasmine GitHub page.


Writing Tests with Jasmine

Jasmine follows a simple structure:

  • describe() – Defines a test suite (a group of related tests).
  • it() – Defines an individual test case.
  • expect() – Asserts the expected outcome.

Here’s an example:

1
2
3
4
5
6
describe("Math Operations", function() {
    it("should add two numbers correctly", function() {
        let sum = 2 + 3;
        expect(sum).toBe(5);
    });
});

Pretty readable, right? Even someone who’s never tested code before can understand what’s going on.


Matchers: The Heart of Jasmine Assertions

Jasmine comes with a set of built-in matchers that help you write test assertions:

MatcherDescription
toBe(value)Checks if two values are exactly the same
toEqual(value)Checks if two objects have equivalent values
toBeDefined()Ensures a variable is not undefined
toBeNull()Checks if a value is null
toContain(value)Checks if an array or string contains a value
toThrow()Ensures a function throws an error

Example:

1
2
3
4
5
6
7
8
describe("Matchers in Jasmine", function() {
    it("should use different matchers", function() {
        let numbers = [1, 2, 3];
        expect(numbers).toContain(2);
        expect(numbers.length).toBe(3);
        expect(null).toBeNull();
    });
});

Spies: Fake It Till You Make It

Sometimes, you need to spy on a function to check if it was called or modify its behavior. Jasmine makes this easy.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
describe("Spy example", function() {
    it("should track calls to a function", function() {
        let calculator = {
            add: function(a, b) { return a + b; }
        };
        
        spyOn(calculator, "add");
        
        calculator.add(2, 3);
        expect(calculator.add).toHaveBeenCalled();
        expect(calculator.add).toHaveBeenCalledWith(2, 3);
    });
});

This is super useful when testing dependencies without actually calling them.


Asynchronous Testing

Jasmine supports async functions via done() or async/await.

Using done():

1
2
3
4
5
6
7
8
describe("Async test with done", function() {
    it("should wait for async operation", function(done) {
        setTimeout(function() {
            expect(true).toBe(true);
            done();
        }, 1000);
    });
});

Using async/await:

1
2
3
4
5
6
describe("Async test with async/await", function() {
    it("should wait for async function", async function() {
        let result = await Promise.resolve("Hello Jasmine");
        expect(result).toBe("Hello Jasmine");
    });
});

Async support makes Jasmine a solid choice for testing APIs, Promises, and async functions.


Running Jasmine Tests

To run tests in Node.js, simply execute:

1
npx jasmine

For browser-based projects, just open SpecRunner.html and see the results in your favorite browser.


Jasmine vs. Other Testing Frameworks

FeatureJasmineMochaJest
BDD SyntaxYesNoYes
Built-in MatchersYesNo (needs Chai)Yes
Built-in SpiesYesNo (needs Sinon)Yes
Async SupportYesYesYes
Runs in BrowserYesNoNo

Jasmine is a great all-in-one solution, whereas Mocha and Jest require additional libraries for spying and assertions.


Key Ideas

ConceptSummary
JasmineBDD-style JavaScript testing framework
MatchersBuilt-in assertions for easy test verification
SpiesAllows function tracking and mocking
Async SupportSupports Promises, async/await, and callbacks
Browser & NodeWorks in both environments

References