eduter / screeps-jest

Helper functions and environment for unit testing your Screeps code with Jest.
14 stars 4 forks source link

Unexpected access to unmocked property "Game._isMockFunction". #19

Closed aksu560 closed 3 years ago

aksu560 commented 3 years ago

When mocking the Game global, Jest errors out with:

● Test suite failed to run

    Unexpected access to unmocked property "Game._isMockFunction".
    Did you forget to mock it?
    If you intended for it to be undefined, you can explicitly set it to undefined (recommended) or set "allowUndefinedAccess" argument to true.

      at Object.get (node_modules/screeps-jest/src/mocking.js:64:23)
          at Array.forEach (<anonymous>)

jest version: 26.6.3 ts-jest version: 26.5.6

Reproduction: src/DungeonMaster/index.ts

export default class DungeonMaster {
    sessionNumber: number;

    constructor() {
        this.sessionNumber = Game.time;
    };

    public run_session(): void {
        console.log("DungeonMaster: Session " + this.sessionNumber + " started.");
    }
}

src/DungeonMaster/index.spec.ts

import { mockGlobal } from 'screeps-jest';
import DungeonMaster from 'DungeonMaster';
import { expect } from '@jest/globals';

describe('DungeonMaster', () => {
    describe('#constructor', () => {
        it('should create a new DungeonMaster instance', () => {
            mockGlobal<Game>('Game', {
                time: 123
            });
            const instance = new DungeonMaster();
            expect(instance).toBeInstanceOf(DungeonMaster);
        });
    });
});

run npm test

aksu560 commented 3 years ago

Issue was old version of screeps-jest. I blindly followed the readme, and hence got version 1.0.0. Upgrading to 1.3.1 fixes the issue.

eduter commented 3 years ago

Right, I see now that PR #6 fixed that for version 1.2.0