2

I'm currently developing a TypeScript/Phaser (2.6.2) project and attempting to run unit tests, set up to use Mocha and Chai (with ts-node/register and jsdom-global/register) on a Node environment with node-canvas installed using the installation instructions for Windows.

The project builds using webpack and runs in the browser correctly, as does Compodoc, and tests that do not require Phaser are also able to execute and pass. Example class/test:

phaser.game.class.ts:

import { Phaser } from 'phaser'

export class Game extends Phaser.Game {
    constructor() { super(1280, 720, Phaser.AUTO, 'game') }
}

phaser.game.class.spec.ts:

import 'mocha'
import { expect } from 'chai'
import { Game } from './phaser.game.class'

describe('Game', () => {
    it('should be defined', () => {
        expect(Game).to.exist
    })
})

However when running tests that do import Phaser I receive an error:

 context.drawImage(magenta, 0, 0);
            ^
 TypeError: Expected object, number and number

Looking at this issue I believe it may be related to Phaser using a different canvas build to that which is installed with node-canvas, but I cannot force the use of a certain node-canvas version (as is described there) as ultimately this will be run in a browser - is this the case, and is there any method to ensure the canvas version just for the tests?

Alternatively I've looked into using phaser-mock and proxyquire (which I would prefer for simply testing isolated class logic), but am experiencing other issues and am unable to find an example implementation combining the two.

Any information would be much appreciated!

Callan Heard
  • 727
  • 1
  • 8
  • 18
  • I have a similar problem too. I'm using Jest to unit test my Phaser 3 Typescript classes. But I can't really use Phaser 3 inside the jest environment. – Prolight May 08 '21 at 00:23
  • I ended up not importing or extending Phaser in much of my logic/classes and isolating the Phaser imports to a few root files to then still run tests on the majority of the codebase - not much of a solution but hope it helps, you may also want to look at https://stackoverflow.com/q/59567372/2030247 and https://stackoverflow.com/q/55690117/2030247 – Callan Heard May 09 '21 at 07:47

0 Answers0