Search code examples
angularunit-testingjasmine

How to say "any [type of data]" in angular Jasmine unit test


I'd like to write a test that looks something like this:

expect(myService.myMethod()).toEqual({
      theDate: any Date,
      title: 'My Title',
...
    });

Is there a way in Jasmine to expect against type? So: any string, any number, any Date, etc? Is that legal, or a bad idea?


Solution

  • You can use jasmine.any(Class) to match against any type.

    i.e.,

    expect(new Date())
      .toEqual(jasmine.any(Date));
    

    See https://jasmine.github.io/api/4.4/jasmine.html for more info.