Why is this an issue?

An assertion is a statement within the unit test that checks whether a particular condition is true or false. It defines the expected behavior of the unit being tested. Assertions are used to express the test’s expected outcome, and they are the criteria against which the actual output of the unit is evaluated.

When the unit test is executed, the assertions are evaluated. If all the assertions in the test pass, it means the unit is functioning correctly for that specific set of inputs. If any of the assertions fail, it indicates that there is a problem with the unit’s implementation, and the test case helps identify the issue.

Without assertions, a unit test doesn’t actually verify anything, making it ineffective in catching potential bugs or regressions. It will always pass, regardless of the implementation of the unit. This can lead to a false sense of security, as you may believe that your code is working correctly when it might not be.

This rule raises an issue when a test uses no assertion, although one of the following assertion libraries is available:

Jest, Jasmine, Cypress, and Playwright do not need to be imported: having them in your project is enough. The other libraries must be imported.

Because aws-cdk-lib/assertions provides assertions only, an AWS CDK test must also use a test framework such as node:test, Jest, or Vitest.

How to fix it in Chai

Use expect(…​), assert.*, or a completed should chain such as value.should.equal(2) or value.should.be.true.

Code examples

Noncompliant code example

const expect = require("chai").expect;

describe("No assertions", function() {
  it("does not test anything", function() { // Noncompliant: The unit test doesn't assert anything
    const result = "";
  });
});

Compliant solution

const expect = require("chai").expect;

describe("Assertions", function() {
  it("tests something", function() {
    const result = "";
    expect(result).to.be.a("string");
  });
});

Noncompliant code example

const chai = require("chai");
chai.should();

describe("Object validation", function() {
  it("checks an object", function() { // Noncompliant: The unit test doesn't assert anything
    const user = {};
    user.should;
  });
});

Compliant solution

const chai = require("chai");
chai.should();

describe("Object validation", function() {
  it("checks an object", function() {
    const user = {};
    user.should.be.an("object");
  });
});

How does this work?

A standalone .should access does not count as an assertion. Complete the chain with an assertion method or property.

How to fix it in Node.js

Use functions from the node:assert module to check the expected result.

Code examples

Noncompliant code example

import test from "node:test";
import assert from "node:assert/strict";

test("returns an administrator", () => { // Noncompliant: The unit test doesn't assert anything
  const user = findUser("alice");
});

Compliant solution

import test from "node:test";
import assert from "node:assert/strict";

test("returns an administrator", () => {
  const user = findUser("alice");
  assert.equal(user.role, "administrator");
});

How to fix it in SuperTest

Use SuperTest’s expect() method to check the HTTP response.

Code examples

Noncompliant code example

import test from "node:test";
import request from "supertest";
import app from "./app.js";

test("returns the health status", async () => { // Noncompliant: The unit test doesn't assert anything
  await request(app).get("/health");
});

Compliant solution

import test from "node:test";
import request from "supertest";
import app from "./app.js";

test("returns the health status", async () => {
  await request(app).get("/health").expect(200);
});

How to fix it in AWS CDK

Use an assertion on a template created with Template.fromStack, Template.fromJSON, or Template.fromString. Assertions on Annotations.fromStack and Tags.fromStack are recognized as well.

Code examples

Noncompliant code example

import { test } from "@jest/globals";
import { Stack } from "aws-cdk-lib";
import { Bucket } from "aws-cdk-lib/aws-s3";
import { Template } from "aws-cdk-lib/assertions";

test("provisions an application logs bucket", () => { // Noncompliant: The unit test doesn't assert anything
  const stack = new Stack();
  new Bucket(stack, "ApplicationLogs", { bucketName: "application-logs" });
});

Compliant solution

import { test } from "@jest/globals";
import { Stack } from "aws-cdk-lib";
import { Bucket } from "aws-cdk-lib/aws-s3";
import { Template } from "aws-cdk-lib/assertions";

test("provisions an application logs bucket", () => {
  const stack = new Stack();
  new Bucket(stack, "ApplicationLogs", { bucketName: "application-logs" });

  Template.fromStack(stack).hasResourceProperties("AWS::S3::Bucket", {
    BucketName: "application-logs",
  });
});

How does this work?

Query methods such as findResources, getResourceId, and toJSON only inspect the template, so they do not count as assertions.

How to fix it in Cypress

Use a Cypress assertion such as should() to check the application state after an interaction.

Code examples

Noncompliant code example

describe("login", () => {
  it("signs in a user", () => { // Noncompliant: The unit test doesn't assert anything
    cy.visit("/login");
    cy.get("[name=email]").type("alice@example.com");
    cy.get("[name=password]").type("secret");
    cy.contains("Sign in").click();
  });
});

Compliant solution

describe("login", () => {
  it("signs in a user", () => {
    cy.visit("/login");
    cy.get("[name=email]").type("alice@example.com");
    cy.get("[name=password]").type("secret");
    cy.contains("Sign in").click();

    cy.contains("Welcome, Alice").should("be.visible");
  });
});

Resources

Documentation