tj / should.js

BDD style assertions for node.js -- test framework agnostic
MIT License
2.75k stars 194 forks source link

Use `Proxy` if available to detect false positives #161

Closed lydell closed 9 years ago

lydell commented 10 years ago
foo.should.be.empty
bar.should.be.a.string // oops! Possible false positive that can slip through.

By wrapping the return value of the should function in a Proxy such errors could be caught.

Something like this:

function proxy(obj) {
  if (typeof Proxy === "function") {
    return new Proxy(obj, {
      get: function(target, prop) {
        if (prop in obj) {
          return target[prop]
        } else {
          throw new Error("No such thang")
        }
      }
    })
  } else {
    return obj
  }
}
alsotang commented 10 years ago

Good suggestion. But seems proxy in ES5 is not such simple? As I know, in ES5 it's not easy to catch a undefined property of a certain object.

lydell commented 10 years ago

Proxy is an ES6 feature. There is nothing we can do in ES5. The idea of the proxy function is to only use a Proxy if available. Otherwise there should be no difference.

btd commented 9 years ago

Move discussion to https://github.com/shouldjs/should.js/issues/35