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:
chaisinonvitestsupertestjestjasmineplaywrightcypressnode:assertuvu/assertbun:testaws-cdk-lib/assertionsJest, 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.
Use expect(…), assert.*, or a completed should chain such as value.should.equal(2) or
value.should.be.true.
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 = "";
});
});
const expect = require("chai").expect;
describe("Assertions", function() {
it("tests something", function() {
const result = "";
expect(result).to.be.a("string");
});
});
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;
});
});
const chai = require("chai");
chai.should();
describe("Object validation", function() {
it("checks an object", function() {
const user = {};
user.should.be.an("object");
});
});
A standalone .should access does not count as an assertion. Complete the chain with an assertion method or property.
Use functions from the node:assert module to check the expected result.
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");
});
import test from "node:test";
import assert from "node:assert/strict";
test("returns an administrator", () => {
const user = findUser("alice");
assert.equal(user.role, "administrator");
});
Use SuperTest’s expect() method to check the HTTP response.
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");
});
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);
});
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.
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" });
});
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",
});
});
Query methods such as findResources, getResourceId, and toJSON only inspect the template, so they do not
count as assertions.
Use a Cypress assertion such as should() to check the application state after an interaction.
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();
});
});
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");
});
});