Important: This documentation is about an older version. It's relevant only to the release noted, many of the features and functions have been updated or replaced. Please view the current version.

Open source

toBeTruthy()

The toBeTruthy() method asserts that a value is truthy in JavaScript. A value is truthy if it converts to true when evaluated in a boolean context.

Syntax

JavaScript
expect(actual).toBeTruthy();
expect(actual).not.toBeTruthy();

Returns

TypeDescription
voidNo return value

Description

The toBeTruthy() method checks if a value is truthy. In JavaScript, all values are truthy except for the following falsy values:

  • false
  • 0
  • -0
  • 0n (BigInt)
  • "" (empty string)
  • null
  • undefined
  • NaN

Usage

JavaScript
import { expect } from 'https://jslib.k6.io/k6-testing/0.6.1/index.js';

export default function () {
  expect(true).toBeTruthy();
  expect(1).toBeTruthy();
  expect(-1).toBeTruthy();
  expect('hello').toBeTruthy();
  expect(' ').toBeTruthy(); // Non-empty string
  expect(42).toBeTruthy();
  expect(3.14).toBeTruthy();
  expect(Infinity).toBeTruthy();
  expect(-Infinity).toBeTruthy();
}