Testim Copilot is here | Learn More

Jasmine Testing: How to Get Started Quickly and Easily

Testing has become a vital part of software development to guarantee the quality of the delivered code. Without tests, you'd…

Testim
By Testim,

Testing has become a vital part of software development to guarantee the quality of the delivered code. Without tests, you’d have no clue about whether the code behaves in the way you expected it to behave. There are many libraries you can use to write tests, and to guarantee that the code behaves as expected, Jasmine testing with behavior driven development is a great option.

The DevOps movement has grown, which means many development and operations teams operate as one team. As a result, writing tests have become even more important. DevOps helps to provide test automation as part of continuous integration pipelines, which means that developers receive faster feedback about the quality of their code. (This link explains why you may want to apply test automation.)

This post will teach you about Jasmine’s features and about behavior-driven testing. It’ll guide you through the Jasmine testing application program interface (API) with a couple of examples. But before we get started with that, let’s discuss the importance of testing.

Why Is Testing Important?

Because testing helps you to guarantee the quality of your code, it’s an important part of the software development life cycle. It also helps you satisfy your customers and deliver projects on time. Instead of fixing bugs at the end of a project, you can test for bugs early in the software development cycle.

Many frameworks and libraries can help you with writing tests: Jest, Chai, Mocha, and Jasmine, to name a few. This article will cover testing with the Jasmine library and how you can get started with some simple examples.

Jasmine has derived a rich API from Jest, which makes it easy to use in combination with Jasmine’s natural language syntax for writing tests. Besides that, Jasmine supports behavior-driven development (BDD), which makes it stand out among other testing libraries.

Next, let’s learn why behavior-driven development is an important part of Jasmine testing.

Introducing Jasmine Testing and Behavior-Driven Development

The Jasmine library is a popular one for writing tests. People often associate Jasmine with behavior-driven development. With Jasmine, instead of writing tests during the last phase of a project, you’ll write tests early on in the development cycle, based on the specific behaviors you’ve defined. Let’s explore how Jasmine and BDD fit together.

When using BDD for writing tests, you should start off by discussing the behaviors of the software you want to build. A person with business knowledge writes down the specific behaviors they expect from the software. Next, a developer asks questions to make the requirements more clear and put them in a development perspective.

From those requirements, the developer writes down test cases to implement in the test suite. Later on in the project, the team will implement the test cases using the Jasmine library when the code is finished.

Next, let’s look at an example of behavior-driven development.

What Does BDD Look Like?

Let’s assume a businessperson wants to implement a reminder system for a certain piece of software. The developer will write down the following test cases based on the questions the businessperson asks. This is a possible result:

  • The reminder system should add a due date for the reminder.
  • It should send an email with a reminder.
  • It should flag the reminder as completed.

By writing down the above tests, the developer better understands how the requirements should work and which tests the software needs to pass. When the team has successfully implemented all the tests, the feature is complete, and the team can move on to the next feature.

Jasmine Features

Jasmine offers a rich library that’s easy to use. Here are some of the features it offers.

  • Test launcher: You can launch Jasmine tests from your browser or command line interface.
  • Assertion functions: Jasmine has a rich API of assertion functions so you can test if a result is as expected.
  • Progress information: You can see test progress and the testing results while executing the tests.
  • Ability to use general testing concepts: Jasmine can use techniques such as mocking, spying, and stubbing to isolate logic. These concepts are important for writing unit tests.
  • Fast test execution: Jasmine has no external dependencies.
  • Ability to use natural language: You don’t need programming language to describe your tests and results.

What about disadvantages? Jasmine has one: It can’t generate a code coverage report. This is a useful feature to know what percentage of your code is covered by tests.

Learn more about the test automation pyramid and where unit tests fit into this pyramid at Testim’s blog.

Next, let’s take a look at some examples of tests written with Jasmine.

Jasmine Testing Examples

This section will cover how you write tests and which hooks are available. It’ll also display some features of the assertions in the API.

Writing Jasmine Tests

The structure for writing tests with the Jasmine library is pretty straightforward. Other testing libraries, including Jest, also use it.

describe('suite', () => {
  it('should do X', () => {
    expect(true).toBe(true)
  })
})

As you can see, you start by defining a describe block that holds one or multiple test cases. After that, you can define a test case by using the it keyword. The it keyword expects a test description followed by a function to execute. Every test case contains assertions that start with the expect keyword.

Jasmine Hooks

Jasmine offers the beforeEach and afterEach hooks. A hook is a function that you can place inside a describe block to add custom programming. In the case of a beforeEach hook, the program will execute this logic before running the defined test cases that the describe block holds. It’s a useful testing concept because it allows you to prepare an environment, database, or any other required setup to successfully execute the tests.

Jasmine Spies

A spy allows you to keep track of all calls and arguments for a specific function without blocking or mocking the call. This means the function gets executed as normal, but you can use the call date for assertions.

First, you need to create a spy with the spyOn function.

spyOn(foo, 'setFoo')

Next, you need to call the function in your test case.

foo.setFoo(234)

Now, you can use the rich Jasmine API to make assertions with the call data. You can check if the program called the function and if the program used the right argument.

expect(foo.setFoo).toHaveBeenCalled()
expect(foo.setFoo).toHaveBeenCalledWith(234)

It’s also possible to track multiple calls for the function. This is useful in case a function gets called multiple times. For example, you can find out whether the function has been called three times.

expect(foo.setFoo.calls.length).toEqual(3)

You can find much more in the Jasmine documentation.

Next up, let’s explore the different assertions you can use.

Jasmine Assertions

Let’s take a look at the expectations API the Jasmine library offers. Jasmine comes from Jest, which means Jasmine has a rich and easy-to-use assertions API. The API uses natural language to make test cases more readable.

To give an example, let’s check if a test’s output equals your expected output.

expect(a).toEqual(output)

Notice that you can read the assertions as “I expect a to equal the value of output.” This way of writing tests makes the assertions much more readable. Here are some examples of the assertions you can use:

expect(a.foo).toBeDefined()
expect(a.foo).toBeUndefined()
expect(a.foo).toBeNull()
expect(a.foo).toBeTruthy()
expect(a.foo).toBeFalsy()

In the above examples, you can test for specific values, including undefined, null, true, or false. It’s also possible to expect a function to throw an error.

expect(function).toThrow()

Also, you can use rich assertions, such as toBeGreaterThan. This function checks if a value is larger than the value you expect.

expect(x).toBeGreaterThan(5)

You can find all information about mocks, assertions, matches, and much more in the Jasmine documentation.

Conclusion: Writing Tests With Jasmine

Writing tests with the Jasmine library is fairly easy. Once you understand the testing structure and how you can use hooks to your advantage to prepare the testing environment, you can quickly write tests.

As previously stated, Jasmine comes from Jest and therefore has a rich assertions API that follows natural language. This means your tests are easy for you and other developers to understand.

Good luck writing your first tests with Jasmine!

This post was written by Michiel Mulders. Michiel is a passionate blockchain developer who loves writing technical content. Besides that, he loves learning about marketing, UX psychology, and entrepreneurship. When he’s not writing, he’s probably enjoying a Belgian beer!