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:)

+3


source to share


1 answer


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)
        };
      }
    };
  }

      

https://github.com/jasmine/jasmine/blob/4097718b6682f643833f5435b63e4f590f22919f/lib/jasmine-core/jasmine.js#L2908

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.

+1


source







All Articles