Sandbox of akashic environment for unit test with jest
Installations
npm install @yasshi2525/jest-environment-akashic
Developer Guide
Typescript
Yes
Module System
CommonJS
Node Version
20.10.0
NPM Version
10.2.3
Score
51
Supply Chain
51.8
Quality
75.7
Maintenance
100
Vulnerability
97.6
License
Releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (98.91%)
JavaScript (1.09%)
Developer
yasshi2525
Download Statistics
Total Downloads
451
Last Day
2
Last Week
14
Last Month
35
Last Year
451
GitHub Statistics
12 Commits
1 Watching
4 Branches
1 Contributors
Bundle Size
960.59 kB
Minified
239.87 kB
Minified + Gzipped
Package Meta Information
Latest Version
0.1.4
Package Id
@yasshi2525/jest-environment-akashic@0.1.4
Unpacked Size
27.77 kB
Size
8.71 kB
File Count
9
NPM Version
10.2.3
Node Version
20.10.0
Publised On
04 Sept 2024
Total Downloads
Cumulative downloads
Total Downloads
451
Last day
0%
2
Compared to previous day
Last week
250%
14
Compared to previous week
Last month
400%
35
Compared to previous month
Last year
0%
451
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
概要
目的
Akashic Engine を利用したコードのユニットテスト記述・実行支援を目的に、サンドボックス環境を提供します.
特徴
本ライブラリを利用すると、Akashic Engineの機能が混在するクラス・関数に対しても個々の動作を確認しやすくなります.
例えば「自作ロジックを組み込んだ g.Sprite
のクリック時の動作」をテストコードとして記述しやすくなります.
また、動作中のスナップショットを画像出力することで、意図した通りにテストされているか目視確認もできます.
Q: どうして動作確認をテストコードとして記述するの?
A: 仕様変更時も変更していない従来のコードが正しく動作するか、目視に頼らず自動的にテストできるからです. テストを自動化することで動作確認の手間を減らし、ゲーム開発を効率的に進められます.
Q: すでにあるテスト支援ライブラリ (@akashic-games/headless-akashic
) との違いは?
A: @akashic-games/headless-akashic
を使うとAkashic Engineを用いたゲームスクリプトをテストできます.
しかし、これはゲーム全体を一からスタートさせるため、ゲームの途中部分からの動作確認や一部機能だけのテストがしづらい側面があります.
そこで、特定の処理や特定の条件下での動きといった個々の動作確認をしやすくするため、 Akashic Engineが動作する使い捨ての軽量環境を提供します. 動作環境は自動で再構築されるため、面倒な初期化処理をその都度書く必要はありません.
一方、図形や画像のレンダリングも行われるため、これを画像出力することで目視で動作確認しながらテストコードを記述できます.
インストール方法・利用方法
akashic init
して構築したプロジェクトで、下記のコマンドを実行してください.
1npm install --dev @yasshi2525/jest-environment-akashic
jest.config.js
に下記を追記してください.
1module.exports = { 2 // ... 3 testEnvironment: "@yasshi2525/jest-environment-akashic" 4 // ... 5}
tsconfig.json
の compilerOptions.types[]
に下記を追記してください.
1{ 2 "compilerOptions": { 3 "types": [ 4 "@yasshi2525/jest-environment-akashic-global" 5 ] 6 } 7}
以上の手順で、テストコード (spec
ディレクトリ配下のコード) で下記のグローバル変数が利用できます.
g
,g.game
: 実際のAkashic Engineの実行環境のものが格納されます.scene
: 空のg.Scene
. テスト単位 (test
またはit
メソッド単位) でリセットされます. 追加したエンティティやハンドラの削除処理を記述する必要はありません.screenshot(filename: string)
: 現在の画面を画像として出力します. ファイルは./tmp/screenshot
以下に出力されます.
また、ゲーム進行を制御する以下の @akashic-games/headless-akashic
の機能がグローバル変数として利用可能です.
gameContext
:GameContext<3>
gameClient
:GameClient<3>
サンプル
TypeScript
TypeScript
テスト対象
src/button.ts
1type ButtonStatus = "ON" | "OFF"; 2 3/** 4 * 押下すると ON, 離すと OFF になるボタン 5 */ 6export class Button extends g.FilledRect { 7 private _status: ButtonStatus = "OFF"; 8 constructor(opts: g.FilledRectParameterObject) { 9 super(opts); 10 // 押下されると ON 11 this.onPointDown.add(() => this.on()); 12 // 離されると OFF 13 this.onPointUp.add(() => this.off()); 14 } 15 16 /** 17 * ON状態にします 18 */ 19 on(): void { 20 this._status = "ON"; 21 this.opacity = 0.5; 22 this.modified(); 23 } 24 25 /** 26 * OFF状態にします 27 */ 28 off(): void { 29 this._status = "OFF"; 30 this.opacity = 1; 31 this.modified(); 32 } 33 34 /** 35 * ボタンの状態を取得します 36 */ 37 get status(): string { 38 return this._status; 39 } 40}
テストコード
1import {Button} from "../src/button"; 2 3describe("button", () => { 4 it ("on()を実行するとON状態になる", async () => { 5 const button = new Button({ scene, width: 100, height: 100, cssColor: "red", touchable: true }); 6 scene.append(button); 7 await gameContext.step(); 8 button.on(); 9 expect(button.status).toEqual("ON"); 10 await gameContext.step(); 11 screenshot("on.png"); 12 }); 13 it("off()を実行するとOFF状態になる", async () => { 14 const button = new Button({ scene, width: 100, height: 100, cssColor: "red", touchable: true }); 15 scene.append(button); 16 await gameContext.step(); 17 button.on(); 18 button.off(); 19 expect(button.status).toEqual("OFF"); 20 await gameContext.step(); 21 screenshot("off.png"); 22 }); 23 it("押下するとON状態になる", async () => { 24 const button = new Button({ scene, width: 100, height: 100, cssColor: "red", touchable: true }); 25 scene.append(button); 26 await gameContext.step(); 27 gameClient.sendPointDown(50, 50, 1); 28 await gameContext.step(); 29 expect(button.status).toEqual("ON"); 30 await gameContext.step(); 31 screenshot("down.png"); 32 }); 33 it("離すとOFF状態になる", async () => { 34 const button = new Button({ scene, width: 100, height: 100, cssColor: "red", touchable: true }); 35 scene.append(button); 36 await gameContext.step(); 37 gameClient.sendPointDown(50, 50, 1); 38 await gameContext.step(); 39 gameClient.sendPointUp(50, 50, 1); 40 await gameContext.step(); 41 expect(button.status).toEqual("OFF"); 42 await gameContext.step(); 43 screenshot("up.png"); 44 }); 45});
オプション
jest.config.js
に下記を追記することで、設定を変更することができます.
1module.exports = { 2 // ... 3 testEnvironmentOptions: { 4 screenshotDir: "<スクリーンショットの出力先>", // 任意, デフォルト値 "./tmp/screenshot" 5 } 6 // ... 7}
既知の問題点
テスト実行後にファイルが存在する
テスト実行時まれに(強制終了時などのタイミングで)下記のファイルが残存します.
もう一度テストを実行すると削除されるので、恐れ入りますがファイル残存時は再テスト実行をお願いします.
1.main.fake-*.js 2.game.fake-*.json
Author
@yasshi2525 (X)
License
MIT License
No vulnerabilities found.
No security vulnerabilities found.