I used to think assertions only served testing. In real projects, assertions appear mostly in unit tests, so I assumed that was the case. That assumption is wrong.
What is an assertion?
In programming, an assertion is a logical statement in a program (a condition that is true or false). Its purpose is to mark and validate the developer’s expected result. When execution reaches the assertion, it should be true. If not, the program stops and returns an error message. … Assertions help designers develop and understand programs.
Quoted from Wikipedia: link
Assertions are a form of program logic and a design-by-contract idea. Testing uses assertions, but we should not equate testing with assertions.
Assertions in unit tests
You see assertions all the time in tests: expect, assertThat, etc.
Jest
it('should show children elements when permission is true and when is true', () => {
const wrapper = shallow(<LAuthShow permission when>}><h1>hello</h1></LAuthShow>);
expect(wrapper.contains(<h1>hello</h1>)).toBeTruthy();
});
Java
@Test
void should_response_boolean_when_check_star_given_country() {
String country = "US";
given(ppcFeignClient.isStarDeal(country)).willReturn(true);
Boolean isStarDeal = ppcService.isStarDeal(country);
assertThat(isStarDeal).isTrue();
}
Business code
Those are typical test assertions. But you can also add assertions in business code. When unexpected situations happen, assertions can trigger actions (e.g., error logs), making debugging more efficient and complementing exceptions and stack traces.
Example
const quote = buildQuote(quoteState);
console.assert(Boolean(quote.currency), 'currency should not be non-null');
const quoteResponseDTO = (yield call(quotationApi.saveQuote, quote)).data;
Here I call the backend to store quote; quote.currency should never be null. If it is, I want a warning during dev so I can trace and fix it. Assertions fit this case.
You can wrap console.assert to add extra behavior, such as notifications:
export const assert = (condition?: boolean, message?: string, ...data: any[]): void => {
console.assert(condition, message, data);
...
showNotice('error');
}
Strengthen assertion handling
Do we need assertions in production? I think no. They are for development. So you can guard them by environment and no-op in production.
export const assert = (condition?: boolean, message?: string, ...data: any[]): void => {
if( process.env.NODE_ENV === 'prod' ){
return ;
}
console.assert(condition, message, data);
showNotice('error');
}
Assert in Spring
Spring is a mature Java framework and it also has assertions.
For example, form validation: this is more elegant than manual checks with exceptions.
public InputStream getData(String file){
Assert.hasText(file,"file input is not a valid file path");
}
Final Thoughts
In short: assertions are a debugging technique, not limited to unit tests.
You might ask: can I solve these without assertions? Yes. Assertions are just a tool. You can use logic checks or exceptions too. But assertions represent a contract: this should never happen. If it does, we should fix the root cause. Exceptions often represent expected errors like 400/404 and are more frequent. They are different.
For example, you could write an if check to log when currency is empty, but it mixes abnormal logic into normal flow. It works but feels less elegant.
The project I’m working on is a data-heavy web platform. Missing key data often causes failures, and it is hard to trace. We used lots of logs and exception handling. In some cases, assertions can improve readability and debuggability.

