Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: assertInstanceOf #5749

Closed
wants to merge 7 commits into from

Conversation

RobDWaller
Copy link
Contributor

New test assertion to allow a developer to assert they have the correct object instance. Useful if there is a polymorphic relationship of some kind.

For example:

class Person {
    age: number;

    constructor(age: number) {
        this.age = age;
    }
}

class Adult extends Person {}

class Child extends Person {}

function definePerson(age: number): Person {
    if (age >= 18) {
        return new Adult(age);
    }

    return new Child(age);
}

console.log(definePerson(17) instanceof Child);
// Output: true
console.log(definePerson(18) instanceof Adult);
// Output: true

@CLAassistant
Copy link

CLAassistant commented May 22, 2020

CLA assistant check
All committers have signed the CLA.

Copy link
Member

@ry ry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It this more useful than assert(a instanceof B) ? It's not even shorter to type:

> "assertInstanceOf(a, B)".length
22
> "assert(a instanceof B)".length
22

@RobDWaller
Copy link
Contributor Author

I appreciate the feedback, it's a fair point, and we could make it assertInstance(). I would argue the interface has value because it is more explicit which makes things easier and more obvious for developers, particularly those with less experience writing tests. It makes it clear you can do this sort of assertion, the assert() method alone does not.

@ry
Copy link
Member

ry commented May 29, 2020

@RobDWaller Thanks for the patch but I'm going to close without merge. I think we shouldn't expand the asserts too much.

@ry ry closed this May 29, 2020
@RobDWaller
Copy link
Contributor Author

Thanks @ry understand your position.

I do feel some developers will get benefit from additional assertions though, so will have a look into creating a third party extension they can use if they desire.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants