question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support checking the prototype of objects

See original GitHub issue

Using ES6 classes a lot, I often construct more complex inheritance chains. At some point, I want to make sure children classes actually extend some parent class. As far as I’ve seen, there is no way to determine whether an object is the prototype of another.
It would be great to have core methods available for that:

class Foo {}
class Bar extends Foo {}
class Quz extends Bar {}

expect(Bar).to.extend(Foo);
expect(Quz).to.inheritFrom(Foo);
expect(Foo).to.be.prototypeOf(Bar);
expect(Foo).to.not.be.prototypeOf(Quz);

The inheritFrom assertion would be most useful as Quz does not directly extend Foo but still inherits from it, that is a little awkward to test otherwise.
I know you could just check for an inherited method, but chances are that the child class does define (or overwrite) that method by itself.

I feel like especially the last one should be part of Chai itself. For now I’m using expect(Foo.isPrototypeOf(Bar)).to.be.true but I wanted to leave this as a feature request.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
keithamuscommented, Jun 9, 2018

We’re going to put a bunch of work into Chai 5 which will include writing a bunch of new assertions that closely mimick the native API features. So we’ll continue to have an instanceof assertion, but we’ll likely add an .extend assertion which can check classes that extend a super class. This has been added to our project board over here: https://github.com/chaijs/chai/projects/2. So I’ll close this issue as we’re tracking this for the chai 5 refactor.

0reactions
meebercommented, Jan 8, 2018

I’d prefer to have a separate assertion for this rather than overloading instanceof.

Regarding direct/indirect inheritance, the options seem to be:

  1. Have two separate assertions: .extend (which allows direct inheritance only) and .inherit (which allows direct and indirect inheritance)
  2. Have a single aliased assertion with an optional flag: .extend and .inherit both allow direct and indirect inheritance unless the directly flag is set (e.g., .directly.extend and directly.inherit), in which case only direct inheritance is allowed.

I’m leaning toward the second option, but haven’t formed a strong opinion yet.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Object prototypes - Learn web development | MDN
Prototypes are the mechanism by which JavaScript objects inherit features from one another. In this article, we explain what a prototype is, how ......
Read more >
Prototype Testing: A Step by Step Guide (2022) | Blog
Learn how to validate your designs and test your prototype. Discover the best prototype testing practices to create awesome products that ...
Read more >
javascript - Check if an object has a user defined prototype?
The initial value is just a simple object with a special property ( constructor ). You could test whether this property exists (...
Read more >
Prototype methods, objects without __proto__
Here the consequences are not terrible. But in other cases we may be storing objects instead of strings in obj , and then...
Read more >
A Beginner's Guide to JavaScript's Prototype - freeCodeCamp
Objects are key/value pairs. The most common way to create an object is with curly braces {} and you add properties and methods...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found