Why expect a component to be truthful and not specific
This is a common question.
When aligning the component with Angular-cli, it creates the first test.
It looks something like this:
it('should create', () => {
expect(component).toBeTruthy();
});
How come this is validating Truthy and not Defined? What's the difference?
Thank you in advance:)
source to share
True source code:
getJasmineRequireObj().toBeTruthy = function() {
function toBeTruthy() {
return {
compare: function(actual) {
return {
pass: !!actual
};
}
};
}
return toBeTruthy;
};
Defined source code:
getJasmineRequireObj().toBeDefined = function() {
function toBeDefined() {
return {
compare: function(actual) {
return {
pass: (void 0 !== actual)
};
}
};
}
So this is a comparison between !!actual
and void 0 !== actual
.
void 0
is the same undefined
AFAIK, and for me, although they are pretty much the same, toBeDefined
is a safer way to check for certain values โโin some cases with an edge.
For example:
expect(0).toBeTruthy()
will evaluate to false / fail
expect(0).toBeDefined()
will evaluate to true / success
More @trichetriche is mentioned in these comments .
However, this will not affect your case.
source to share