Typescript: Tests: no way ensuring that super has been called.

Created on 12 Nov 2015  路  7Comments  路  Source: microsoft/TypeScript

When writing tests (using Jasmine in my case), how can one ensure that the super of a class is called?

For instance, in the following code:

class Snake extends Animal {
    constructor(name: string) { super(name); }
    move(meters = 5) {
        alert("Slithering...");
        super.move(meters);
    }
}

How can one write a test ensuring super.move() is called when Snake.move() is called?

Question

Most helpful comment

Or alternatively:

beforeAll( function () {
    spyOn( Transformer.prototype, 'translate' );

    iTranslation = new Point( 10, 20 );
    iPainter.translate( iTranslation );
});

it( 'Should call translate on its super', function() {
    expect( Transformer.prototype.translate ).toHaveBeenCalledWith( iTranslation );
});

Got it, thanks!

All 7 comments

Should be able to spy on it and then check if it was called with toHaveBeenCalled

For the record, StackOverflow/StackExchange/our Gitter room/our IRC room is probably a better venue for these sorts of questions.

@bmayen how?

I can't see super anywhere on the prototype.

@DanielRosenwasser, might be phrased as a question, but I'm really arguing this cannot be done and thus an issue.

there is no entity called super super is just an alias to the class you are extending. in this case, Animal. so as @bmayen mentioned, Animal.move is what you want to check to have been called.

@mhegazy Still not sure I understand. Animal is a class, not an instance.

Just to make it more concrete, given this code:

class Painter extends Transformer {

    //.... 

    translate( aTranslation: Point ) {
        this.context.translate( aTranslation.x , aTranslation.y );
        super.translate( aTranslation );
    }

}

The test:

describe( 'translate()', function() {

    beforeAll( function () {
        spyOn( iPainter.context, 'translate' );
        iPainter.translate( new Point( 10, 20 ) );
    });

    it( 'Should translate the context', function() {
        expect( iPainter.context.translate ).toHaveBeenCalledWith( 10, 20 );
    });

    it( 'Should call translate on its super', function() {
        // What's here???
    });        

});

What would be the line to replace What's here???

var originalTransformenrTranslate = Transformer. prototype.translate;
Transformer. prototype.translate = function (...args) {
    console.log("Transformer.translate is called!");
    originalTransformenrTranslate.apply(this, args);
}

var originalPainterTranslate = Painter. prototype.translate;
Painter. prototype.translate = function (...args) {
    console.log("Painter.translate is called!");
    originalPainterTranslate.apply(this, args);
}

Or alternatively:

beforeAll( function () {
    spyOn( Transformer.prototype, 'translate' );

    iTranslation = new Point( 10, 20 );
    iPainter.translate( iTranslation );
});

it( 'Should call translate on its super', function() {
    expect( Transformer.prototype.translate ).toHaveBeenCalledWith( iTranslation );
});

Got it, thanks!

Was this page helpful?
0 / 5 - 0 ratings