Jump to content

Mocha + Typescript


staff0rd
 Share

Recommended Posts

I'm using Typescript with Phaser and I'd like to get some command-line tests running.  I think I should be able to do this with node/mocha, but I'm having difficulty.

I've got the following files;

MyClass.ts

/// <reference path="node_modules/phaser/typescript/phaser.d.ts" />
export class MyClass {
    d: Phaser.Sprite;
    constructor() {
        this.d = new Phaser.Sprite(new Phaser.Game, 10, 10);
    }
    win() : boolean {
        return true;
    }
}

test.ts

/// <reference path="../typings/mocha/mocha.d.ts" />
import MyModule = require('../MyClass'); 
    
describe('MyClass', () => {   
    var subject : MyModule.MyClass;

    beforeEach(function () {
        subject = new MyModule.MyClass();
    });

    describe('#win', () => {
        it('should pass', () => {
            var result : boolean = subject.win();
            if (result !== true) {
                throw new Error('Expected true but was ' + result);
            }
        });
    });
});

I'm using ts-node to execute typescript in node so I execute mocha as follows;

mocha --compilers ts:ts-node/register

Compilation is successful, however the tests then fail at runtime because Phaser is not defined;

  MyClass
    #win
      1) "before each" hook for "should pass"


  0 passing (47ms)
  1 failing

  1) MyClass "before each" hook for "should pass":
     ReferenceError: Phaser is not defined
      at new MyClass (c:\Users\stafford\Documents\git\ts-node-test\MyClass.ts:5:22)
      at Context.<anonymous> (c:\Users\stafford\Documents\git\ts-node-test\test\test.ts:8:19)
      at callFn (C:\Users\stafford\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:286:21)
      at Hook.Runnable.run (C:\Users\stafford\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:279:7)
      at next (C:\Users\stafford\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:297:10)
      at Immediate._onImmediate (C:\Users\stafford\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:319:5)

I thought I would then have to do something like this or similar;

import Phaser = require('phaser');

But this then breaks compilation with the error phaser.d.ts is not a module.

Has anyone had success testing in this way?  What am I doing wrong?  Or, perhaps if this is not possible, does anyone have another method of testing typescript objects that reference Phaser?

There's a small repro here if anyone's interested.

Link to comment
Share on other sites

I was able to get a bit further, and I pulled ts-node out of it as it's not required.

I added a vendor.d.ts file to pull in the phaser reference and then I could import it.  Then I got a document is undefined error so I installed jsdom and setup global.document.

Now I'm getting the following from phaser.js - TypeError: Cannot set property 'fillStyle' of undefined.

Repro is updated, full error here;

c:\git\ts-node-test>npm test

> [email protected] pretest c:\git\ts-node-test
> tsc test/test.ts MyClass.ts --module commonjs


> [email protected] test c:\git\ts-node-test
> mocha

c:\git\ts-node-test\node_modules\phaser\build\phaser.js:19994
    canvas.context.fillStyle = "rgba(10, 20, 30, 0.5)";
                             ^

TypeError: Cannot set property 'fillStyle' of undefined
    at Function.PIXI.CanvasTinter.checkInverseAlpha (c:\git\ts-node-test\node_modules\phaser\build\phaser.js:19994:30)
    at Object.<anonymous> (c:\git\ts-node-test\node_modules\phaser\build\phaser.js:20025:54)
    at Object.<anonymous> (c:\git\ts-node-test\node_modules\phaser\build\phaser.js:22660:4)
    at Module._compile (module.js:397:26)
    at Object.Module._extensions..js (module.js:404:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (c:\git\ts-node-test\test\test.js:6:14)
    at Module._compile (module.js:397:26)
    at Object.Module._extensions..js (module.js:404:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at c:\git\ts-node-test\node_modules\mocha\lib\mocha.js:216:27
    at Array.forEach (native)
    at Mocha.loadFiles (c:\git\ts-node-test\node_modules\mocha\lib\mocha.js:213:14)
    at Mocha.run (c:\git\ts-node-test\node_modules\mocha\lib\mocha.js:453:10)
    at Object.<anonymous> (c:\git\ts-node-test\node_modules\mocha\bin\_mocha:393:18)
    at Module._compile (module.js:397:26)
    at Object.Module._extensions..js (module.js:404:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:429:10)
    at startup (node.js:139:18)
    at node.js:999:3
npm ERR! Test failed.  See above for more details.

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...