Giter VIP home page Giter VIP logo

screeps-jest's People

Contributors

brisberg avatar dependabot[bot] avatar eduter avatar patrickhousley avatar rodweb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

screeps-jest's Issues

Bump version of package

Just FYI, make sure you bump the version number in package.json each time you do a release.

By default, yarn and npm will compare the contents on the package.json files on upgrade and if the version hasn't been increased, it will decide that it does not need to download a new version from GitHub.

The version should be at least 1.1.0 as of now.

Jest v27 incompatible?

When running with [email protected] and [email protected], the below error is thrown.

    TypeError: Cannot read property 'fn' of undefined

      at Object.mockRoomPositionConstructor (node_modules/screeps-jest/src/mocking.js:133:53)
      at TestEnvironment.setup (node_modules/screeps-jest/src/TestEnvironment.js:13:19)
      at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)
      at runJest (node_modules/@jest/core/build/runJest.js:387:19)
      at _run10000 (node_modules/@jest/core/build/cli/index.js:408:7)
      at runCLI (node_modules/@jest/core/build/cli/index.js:261:3)

Fail when using Jest@26

Fails when using the latest version of Jest (26.1.0).

Jest now access a few internal calls on mocked functions which trigger the "unmocked properties" safe guard.

Unexpected access to unmocked property "Memory._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:66:23)
          at Array.forEach (<anonymous>)
TypeError: globalMock.mockClear is not a function

      at Object.keys.forEach.key (node_modules/jest-runtime/build/index.js:939:24)
          at Array.forEach (<anonymous>)

Interesting issue with reproducing this with your test cases. Pulling the repo and upgrading to Jest 26 still causes all the tests to pass. However, if you SKIP the test about mockGlobal where allowUndefined is true, the other tests fail with the above errors.

I am guessing that something is not being cleaned up correctly between cases, as those tests should be independent.

Project fails to compile above TypeScript 3.8.x

Upgrading the version of TypeScript used to compile this project past 3.8.x results in the following error on yarn build:

$ tsc
src/mocking.ts:141:13 - error TS2589: Type instantiation is excessively deep and possibly infinite.

141         id: this.id,
                ~~~~~~~


Found 1 error.

Reproduction steps:

git checkout master
yarn install
yarn add -D [email protected]
yarn build

Unexpected access to unmocked property "Game._isMockFunction".

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

Include the function mockConstructor()

type Constructor<T> = new(...args: any[]) => T;

function mockConstructor<C extends Constructor<any>>(
  className: string,
  fn: (...args: ConstructorParameters<C>) => DeepPartial<InstanceType<C>> = (...args: any) => args
) {
  (global as any)[className] = jest.fn(
    (...args: ConstructorParameters<C>) => mockInstanceOf<InstanceType<C>>(fn(...args))
  );
}

mockConstructor<RoomPositionConstructor>('RoomPosition', (x: number, y: number, roomName: string) => ({ x, y, roomName }));

Mock constructors out-of-the-box, so that extending prototype doesn't throw

Something like this, in TestEnvironment.ts, should be enough:

[
  'ConstructionSite',
  'Creep',
  'Flag',
  'Mineral',
  'Nuke',
  'OwnedStructure',
  'Resource',
  'Room',
  'RoomObject',
  'RoomVisual',
  'Source',
  'Structure',
  'StructureContainer',
  'StructureController',
  'StructureExtension',
  'StructureExtractor',
  'StructureFactory',
  'StructureInvaderCore',
  'StructureLab',
  'StructureLink',
  'StructureKeeperLair',
  'StructureNuker',
  'StructureObserver',
  'StructurePortal',
  'StructurePowerBank',
  'StructurePowerSpawn',
  'StructureRampart',
  'StructureRoad',
  'StructureSpawn',
  'StructureStorage',
  'StructureTerminal',
  'StructureTower',
  'StructureWall',
  'Spawning',
  'Tombstone'
].forEach(className => {
  this.global[className] = { prototype: {} };
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.