mercredi 17 janvier 2018

Jasmine matcher composition

I am trying to familiar how to write concise matches for complex objects. Typical use case is to validate a request in form of object.

Right now I am using Jasmine, but it could be a different JS framework. I hope I could easily to switch to an alternative, because there is no too much code in my project.

Jasmine's expect method works well for simple values. Once I try to validate an object with various matches troubles appear.

Example object:

{ email: "xxx@gmail.com", name: "xxx", session: "234234" }

I would be satisfied with following one expression matching everything:

jasmine.expect(obj).toEqual({ 
  email: jasmine.optional(jasmine.email()), 
  name: /^[A-z]{3,40}$/, 
  magicPi: jasmine.is(314),
  session: jasmine.noKeyInDictionary()      
})

I am a java developer, so frankly speaking I am looking for a Hamcrest alternative in JavaScript world.

Aucun commentaire:

Enregistrer un commentaire