2

How can I test a property against a regular expression in chai? Bonus points: I actually want to test a property of an object returned by a promise using chai-as-promised (but I guess if I know the non-promise way chai-as-promised should just work the same way).

My function

function foo() { 
  return Promise.resolve({ bar: 'baz' }); 
}

My test (my idea):

// Non-Promise-way
foo().should.match.property('bar', /baz/);    

// Promise-way
foo().should.eventually.match.property('bar', /baz/);

(but there is no match.property)

Lukas
  • 9,752
  • 15
  • 76
  • 120
  • Is this question about the correct way to check for a property or about how to check that property with a regex? – Simon Hänisch Aug 10 '16 at 12:16
  • The combination of it. And I just found out it's `foo().should.eventually.have.property('bar').and.match(/baz/);`. Someone also answered this but then deleted it. I now see I need to update my question though. – Lukas Aug 10 '16 at 12:17
  • I deleted it because I didn't do a match, but an equal check instead. – Simon Hänisch Aug 10 '16 at 12:20
  • Correct your answer and I'll accept it :) – Lukas Aug 10 '16 at 12:22

1 Answers1

3

From the Chai API docs I derived this:

foo().should.have.property('bar').and.match(/baz/);

Or for the promise style:

foo().should.eventually.have.property('bar').and.match(/baz/);
Simon Hänisch
  • 4,740
  • 2
  • 30
  • 42