EightBittr fully supports testing its inheriting classes.
You can use its settings constructor argument to pass in test-friendly settings for its modules.
From the core modules, you'll generally want to override:
- AudioPlayer: Set
createSoundtosinon.createStubInstance(AudioElementSound)so that it doesn't slow or crash tests by creating<audio>elements. - FrameTicker: Set
timingto clock values pointing to a @sinonjs/fake-timers clock, allowing tests to programmatically jump through time. - ItemsHolder: Set
storageto ItemsHoldr'screateStoragemock so each test has its own local storage. - PixelDrawer: Set
framerateSkiptoInfinityso that it doesn't slow tests down redrawing its canvas.
import { createClock } from "@sinonjs/fake-timers";
import { AudioElementSound } from "audioplayr";
import { createStorage } from "itemsholdr";
import { createStubInstance } from "sinon";
import { MyGame } from "./MyGame";
export const setupMyGame = () => {
const storage = createStorage();
const clock = createClock();
const game = new MyGame({
components: {
audioPlayer: {
createSound: () => createStubInstance(AudioElementSound),
},
frameTicker: {
timing: {
cancelFrame: clock.clearTimeout,
getTimestamp: () => clock.now,
requestFrame: (callback) =>
clock.setTimeout(() => {
callback(clock.now);
}, 1),
},
},
itemsHolder: { storage },
pixelDrawer: { framerateSkip: Infinity },
},
height: 160,
width: 240,
});
return { clock, game, storage };
};You can then call methods on the game's modules and sections in your tests to trigger in-game logic:
const { game } = setupMyGame();
game.actors.add("Box", 16, 24);