2024-04-23 14:19:49 +02:00

62 lines
2.3 KiB
Plaintext

== 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 one of the following assertion libraries is imported but no assertion is used in a test:
- ``++chai++``
- ``++sinon++``
- ``++vitest++``
- ``++supertest++``
[source,javascript,diff-id=1,diff-type=noncompliant]
----
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.
[source,javascript,diff-id=1,diff-type=compliant]
----
const expect = require("chai").expect;
describe("Assertions", function() {
it("test something", function() {
const str = "";
expect(str).to.be.a("string");
});
});
----
== Resources
=== Documentation
* Chai.js Documentation - https://www.chaijs.com/api/[API Reference]
* Sinon.js Documentation - https://sinonjs.org/releases/latest/assertions/[Assertions]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]