The Difference between toBe(true), toBeTrue() and toBeTruthy() in the Jasmine Framework

Reema Alzohairi
3 min readFeb 13, 2020

--

Using the Jasmine framework, I have wrote several unit tests. Those tests tested different function outputs. In certain cases, the function output was a boolean value. Looking for the proper test function from Jasmine, I stumbled upon not one, not two but three functions that seemed really appropriate.

At first glance, those three functions, which are toBe(true), toBeTrue() and toBeTruthy(), seem like they do the exact same thing, don’t they? But the question is do they really do the same thing? In this post, I’ll breakdown how those 3 functions differ from one another when writing unit tests using the Jasmine framework.

Photo by Caspar Camille Rubin on Unsplash

What is unit testing?

Unit testing is a crucial type of testing that ensures that different units of code are proper for usage. The basic idea for it is all about testing the actual output of a particular function against what it is expected to be returned and reporting any failures. For unit testing, several testing frameworks exist and provide different tools to help you form solid unit tests easily using the framework’s API.

The Jasmine Framework

The Jasmine framework is an example of a testing framework for JavaScript, which is defined as the following by the official site:

A behavior-driven development framework for testing JavaScript code.

Jasmine is used tightly with the angular framework, which is how I learned about it. This framework provides several functions, called matchers, that compare between an actual value and an expected value in the following syntax:

expect(actual).matcherFunction(expected)

So, What’s the Difference between toBe(true), toBeTrue() and toBeTruthy() matcher functions?

toBe(true)

  • Matcher function performs the test: actual === expected
  • Syntax: expect(flag).toBe(true | false)

This test passes only if flag has the value true, in the case of expect(flag).toBe(true). Truthy values will not pass this test, only the actual value true will.

The following table shows the truthy and falsy values in JavaScript, where truthy values is every value that will be coerced to true in a boolean context and falsy values is every value that will be coerced to false in a boolean context.

Table 1: Truthy and Falsy values in JavaScript

In the same way, the test expect(flag).toBe(false) will only pass if the flag has the value false. Falsy values will not pass this test.

toBeTrue()

  • Matcher function performs the test: (actual === true || is(actual, 'Boolean') && actual.valueOf())
  • Syntax: expect(flag).toBeTrue()

This function behaves the same way as toBe(true) but handles an additional case.

  • toBe(true) ➜ only handles a primitive boolean type.
  • ToBeTrue() handles both a primitive boolean type and a Boolean object.

So, expect(flag).toBeTrue() will only pass if flag is a primitive boolean type with the value true OR if flag is a boolean object with a value of true.

toBeTruthy()

  • Matcher function performs the test: !!actual
  • Syntax: expect(flag).toBeTruthy()

Note: The !! operator is used to implicitly coerce any value to a boolean value.

This test passes only if flag has the value true after coercion. In other words, if it’s a truthy value, where the truthy values are shown in Table 1.

--

--