This project is read-only.

TypeScript Fakes

Topics: General
Jan 10, 2013 at 9:45 PM
Edited Jan 11, 2013 at 1:56 PM

Are there any plans to generate fakes from TypeScript files?

For example, given interface:


module foo {
  interface IWriter {


Compiled JavaScript file (say "myFile.fake.js" )

// JavaScript
var fake = { foo: { IWriter:undefined }}; = function IWriter() {
    this.writeToFileCount = 0;
}; = function () {


And usage:

module footests {

 var instanceThatUsesIWriter = { write: (writer:foo.IWriter) => {} };

 var fakeWriter = new;

 assertAreEqual(fakeWriter.writeToFileCount, 1);



We would find this pretty useful because, now with TypeScript, the SUT normally does not have any coupling with concrete types; rather most work is done via interfaces. But unfortunately when it comes to testing we have a problem, as there is no real representation of interfaces in the generated code.

Any thoughts from anyone?


PS: Obligatory thanks to the TS team for producing the most important innovation in Web technology since HTTP!

Jan 11, 2013 at 8:51 PM

Interfaces are not meant to be 'newed'. It's like asking for a music you can eat.

Instead you can do:

var fakeWriter = { writeToFile: function() { this.writeToCount++; } };

Jan 11, 2013 at 9:44 PM

@mihailik, interesting analogy, but no, music does not make a good diet!

The mock writer is used purely to test expectations, it is not a real type because its implementation is fake, so IMO it is perfectly fine to new it.

The example is equivalent to the following example using Rhino Mocks in C#:

  IWriter mockWriter = Rhino.Mocks.MockRepository.GenerateStub<IWriter>();


In the end users of the fake library can write their own versions of MockRespository.GenerateStub or GenerateMock so the actual instantiation of the fake will happen behind the scenes.

In any case, IMO, it's a minor point.

Let me know if I didn't understand you.