eduter / screeps-jest Goto Github PK
View Code? Open in Web Editor NEWHelper functions and environment for unit testing your Screeps code with Jest.
Helper functions and environment for unit testing your Screeps code with Jest.
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.
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)
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.
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
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
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 }));
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: {} };
});
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.