2

Our Mocha test suite has this line:

model.getResourceDependencies.should.be.a.Function;

the test code uses the should library

as you can see the above expression is neither an assignment nor an invocation, or is it?

How does this work? Is there some sort of underlying mechanism onPropertyRead() or something like that so that the should library can execute something even if no function is explicitly called?

By the way, it's damn near impossible to remember any of the should or chai APIs.

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
  • ES5 [getter](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/get)? – user3707125 Jun 14 '16 at 00:37
  • 1
    Just FYI should.js separate getters and assertion calls, in your case .Function() in last versions will be assertion function call. It helps with linters essentially and to do not have false positive when you use wrong name of getter, like .string, but actually it should be .String for instance. – den bardadym Jun 14 '16 at 07:03
  • Related: [How does the chai expect function work?](https://stackoverflow.com/q/31605391/1048572) – Bergi Jun 26 '17 at 15:35

1 Answers1

4

should.js uses ES5 getter. https://github.com/shouldjs/should.js/blob/9.0.2/lib/should.js#L105

chai uses it too. https://github.com/chaijs/chai/blob/3.5.0/lib/chai/interface/should.js#L35

In general, such behavior is available with ES5 getter or ES6 Proxy (and Object.prototype.__noSuchMethod__ in old days)

den bardadym
  • 2,747
  • 3
  • 25
  • 27
hakatashi
  • 9,006
  • 2
  • 22
  • 22
  • The key mechanism appears to be a JavaScript construct, [`Object.defineProperty()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty) – Michael Osofsky Nov 21 '17 at 22:22