Skip to content

Commit

Permalink
chore: Reduce test memory footprint to reduce flakey tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eonarheim committed Mar 17, 2024
1 parent d75ff1a commit d9904dc
Show file tree
Hide file tree
Showing 39 changed files with 237 additions and 9 deletions.
3 changes: 3 additions & 0 deletions src/engine/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ export class Engine<TKnownScenes extends string = any> implements CanInitialize,

return value;
}
static InstanceCount = 0;

/**
* Anything run under scope can use `useEngine()` to inject the current engine
Expand Down Expand Up @@ -969,6 +970,7 @@ O|===|* >________________>\n\
this._initialize(options);

(window as any).___EXCALIBUR_DEVTOOL = this;
Engine.InstanceCount++;
}

private _handleWebGLContextLost = (e: Event) => {
Expand Down Expand Up @@ -1131,6 +1133,7 @@ O|===|* >________________>\n\
this.screen.dispose();
this.graphicsContext.dispose();
this.graphicsContext = null;
Engine.InstanceCount--;
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/engine/Util/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import { Future } from './Future';
*/
export function getPosition(el: HTMLElement): Vector {
// do we need the scroll too? technically the offset method before did that
const rect = el.getBoundingClientRect();
return vec(rect.x + window.scrollX, rect.y + window.scrollY);
if (el) {
const rect = el.getBoundingClientRect();
return vec(rect.x + window.scrollX, rect.y + window.scrollY);
}
return Vector.Zero;
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/spec/ActorSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ describe('A game actor', () => {
parent.addChild(child);

expect(child.scene).toBe(engine.currentScene);
engine.dispose();
});

it('should create actor with valid default options', () => {
Expand Down Expand Up @@ -632,6 +633,7 @@ describe('A game actor', () => {

it('once killed is not drawn', async () => {
engine.stop();
engine.dispose();
engine = null;
engine = TestUtils.engine({ width: 100, height: 100 });
await TestUtils.runToReady(engine);
Expand Down Expand Up @@ -688,6 +690,10 @@ describe('A game actor', () => {
});

it('can be drawn with a z-index', async () => {
engine.stop();
engine.dispose();
engine = null;

engine = TestUtils.engine({
width: 100,
height: 100,
Expand Down Expand Up @@ -726,6 +732,10 @@ describe('A game actor', () => {
});

it('can have a graphic drawn at an opacity', async () => {
engine.stop();
engine.dispose();
engine = null;

engine = TestUtils.engine({
width: 62,
height: 64,
Expand Down
4 changes: 4 additions & 0 deletions src/spec/ArcadeSolverSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ describe('An ArcadeSolver', () => {

// give player right velocity
ex.Physics.acc = ex.Vector.Zero;

game.dispose();
});

it('should cancel collision contacts where there is no more overlap', () => {
Expand Down Expand Up @@ -319,5 +321,7 @@ describe('An ArcadeSolver', () => {
for (let i = 0; i < 40; i++) {
clock.step(16);
}

game.dispose();
});
});
8 changes: 8 additions & 0 deletions src/spec/CameraSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ describe('A camera', () => {
});

it('should be center screen by default (when loading not complete)', () => {
engine.dispose();
engine = null;
engine = TestUtils.engine({
viewport: {width: 100, height: 100},
resolution: {width: 1000, height: 1200 }
Expand All @@ -60,6 +62,8 @@ describe('A camera', () => {
});

it('should run strategies on initialize for the first frame', () => {
engine.dispose();
engine = null;
engine = TestUtils.engine({
viewport: {width: 100, height: 100},
resolution: {width: 1000, height: 1200 }
Expand All @@ -74,6 +78,8 @@ describe('A camera', () => {
});

it('should update viewport on initialize for the first frame', () => {
engine.dispose();
engine = null;
engine = TestUtils.engine({
viewport: {width: 100, height: 100},
resolution: {width: 1000, height: 1200 }
Expand All @@ -89,6 +95,8 @@ describe('A camera', () => {
});

it('should be center screen by default (when loading complete)', () => {
engine.dispose();
engine = null;
engine = TestUtils.engine({
viewport: {width: 100, height: 100},
resolution: {width: 1000, height: 1200 }
Expand Down
2 changes: 1 addition & 1 deletion src/spec/CanvasSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,6 @@ describe('A Canvas Graphic', () => {
expect(sut.width).toBe(50);
expect(sut.height).toBe(50);
await expectAsync(engine.canvas).toEqualImage('src/spec/images/GraphicsCanvasSpec/centered.png');

engine.dispose();
});
});
3 changes: 3 additions & 0 deletions src/spec/CollisionShapeSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe('Collision Shape', () => {

afterEach(() => {
engine.stop();
engine.dispose();
engine = null;
});

Expand Down Expand Up @@ -442,6 +443,7 @@ describe('Collision Shape', () => {

afterEach(() => {
engine.stop();
engine.dispose();
engine = null;
});

Expand Down Expand Up @@ -830,6 +832,7 @@ describe('Collision Shape', () => {

afterEach(() => {
engine.stop();
engine.dispose();
engine = null;
});

Expand Down
3 changes: 3 additions & 0 deletions src/spec/CoroutineSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ describe('A Coroutine', () => {
await expectAsync(postdraw).toBeResolved();
await expectAsync(postframe).toBeResolved();
});
engine.dispose();
});


Expand All @@ -184,6 +185,7 @@ describe('A Coroutine', () => {
clock.step(100);
await expectAsync(result).toBeResolved();
});
engine.dispose();
});

it('can wait for a promise', async () => {
Expand All @@ -207,6 +209,7 @@ describe('A Coroutine', () => {
clock.step(100);
await expectAsync(result).toBeResolved();
});
engine.dispose();
});

it('can throw error', async () => {
Expand Down
1 change: 1 addition & 0 deletions src/spec/DebugSystemSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe('DebugSystem', () => {

afterEach(() => {
engine.stop();
engine.dispose();
engine = null;
});

Expand Down
6 changes: 6 additions & 0 deletions src/spec/DefaultLoaderSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ describe('A DefaultLoader', () => {
engine = TestUtils.engine();
});

afterEach(() => {
engine.stop();
engine.dispose();
engine = null;
});

it('exists', () => {
expect(ex.DefaultLoader).toBeDefined();
});
Expand Down
9 changes: 9 additions & 0 deletions src/spec/DirectorSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe('A Director', () => {
expect(sut.getSceneInstance('scene2')).not.toBe(null);
expect(sut.getSceneInstance('scene3')).not.toBe(null);
expect(sut.getSceneInstance('scene4')).not.toBe(null);
engine.dispose();
});

it('can be constructed with a varied loaders', () => {
Expand All @@ -42,6 +43,7 @@ describe('A Director', () => {
expect(sut.getSceneInstance('scene2')).not.toBe(null);
expect(sut.getSceneInstance('scene3')).not.toBe(null);
expect(sut.getSceneInstance('scene4')).not.toBe(null);
engine.dispose();
});

it('can configure start, non deferred', async () => {
Expand Down Expand Up @@ -92,6 +94,7 @@ describe('A Director', () => {
expect(sut.currentTransition).toBe(fadeIn);
expect(sut.currentSceneName).toBe('scene1');
expect(sut.currentScene).toBe(scene1);
engine.dispose();
});

it('will draw a start scene transition', async () => {
Expand Down Expand Up @@ -131,6 +134,7 @@ describe('A Director', () => {
expect(sut.currentScene).toBe(scene1);

await expectAsync(engine.canvas).toEqualImage('/src/spec/images/DirectorSpec/fadein.png');
engine.dispose();
});

it('will run the loader cycle on a scene only once', async () => {
Expand All @@ -152,6 +156,7 @@ describe('A Director', () => {
await sut.maybeLoadScene('scene1');

expect(loaderSpy).toHaveBeenCalledTimes(1);
engine.dispose();
});

it('can remove a scene', () => {
Expand All @@ -177,6 +182,7 @@ describe('A Director', () => {
sut.remove('scene4');
expect(sut.getSceneDefinition('scene4')).toBe(undefined);
expect(sut.getSceneInstance('scene4')).toBe(undefined);
engine.dispose();
});

it('cant remove an active scene', () => {
Expand All @@ -192,6 +198,7 @@ describe('A Director', () => {

expect(() => sut.remove('root')).toThrowError('Cannot remove a currently active scene: root');
expect(() => sut.remove(sut.rootScene)).toThrowError('Cannot remove a currently active scene: root');
engine.dispose();
});

it('can add a scene that was already deleted', async () => {
Expand All @@ -215,6 +222,7 @@ describe('A Director', () => {

await sut.goto('scene1');
expect(sut.currentScene).toBe(newScene);
engine.dispose();
});

it('can goto a scene', async () => {
Expand Down Expand Up @@ -243,5 +251,6 @@ describe('A Director', () => {
await sut.goto('scene4');
expect(sut.currentSceneName).toBe('scene4');
expect(sut.currentScene).toBeInstanceOf(MyScene);
engine.dispose();
});
});
Loading

0 comments on commit d9904dc

Please sign in to comment.