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 one of the following assertion libraries is imported but no assertion is used in a test:
-
chai
-
sinon
-
vitest
-
supertest
const expect = require("chai").expect;
describe("No assertions", function() {
it("don't test anything", function() { // Noncompliant: The unit test doesn't assert anything
const str = "";
});
});
To write effective unit tests, you should define the expected behavior of the unit using assertions, allowing you to catch bugs early and ensure
the reliability of your codebase.
const expect = require("chai").expect;
describe("Assertions", function() {
it("test something", function() {
const str = "";
expect(str).to.be.a("string");
});
});