more support for vitest

This commit is contained in:
meepen 2024-03-28 16:47:45 -04:00
parent 10a3d97076
commit 4fe69a29e9
7 changed files with 1275 additions and 7 deletions

2
.gitignore vendored
View File

@ -33,4 +33,4 @@ public/images/character/*/
src/data/battle-anim-raw-data*.ts
src/data/battle-anim-data.ts
src/**/*.js
coverage

1226
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -8,19 +8,23 @@
"build": "vite build",
"preview": "vite preview",
"test": "vitest run",
"test:watch": "vitest watch"
"test:cov": "vitest run --coverage",
"test:watch": "vitest watch --coverage"
},
"devDependencies": {
"@vitest/coverage-istanbul": "^1.4.0",
"axios": "^1.6.2",
"axios-cache-interceptor": "^1.3.2",
"eslint": "^8.25.0",
"happy-dom": "^14.3.9",
"jsdom": "^24.0.0",
"json-beautify": "^1.1.1",
"phaser3spectorjs": "^0.0.8",
"pokenode-ts": "^1.20.0",
"typescript": "^5.0.3",
"vite": "^4.5.0",
"vite-plugin-fs": "^0.4.4",
"vitest": "^1.4.0"
"vitest": "^1.4.0",
"vitest-canvas-mock": "^0.3.3"
},
"dependencies": {
"@material/material-color-utilities": "^0.2.7",
@ -35,5 +39,10 @@
"imports": {
"#app": "./src/main.js",
"#app/*": "./src/*"
},
"jest": {
"setupFiles": [
"jest-canvas-mock"
]
}
}

5
src/test/game.ts Normal file
View File

@ -0,0 +1,5 @@
import Phaser from "phaser";
export default new Phaser.Game({
type: Phaser.HEADLESS,
});

2
src/test/vitest.setup.ts Normal file
View File

@ -0,0 +1,2 @@
import "vitest-canvas-mock";
import "#app/test/game";

View File

@ -1,11 +1,22 @@
import { expect, describe, it } from "vitest";
import { randomString } from "./utils";
import Phaser from "phaser";
describe("utils", () => {
describe("randomString", () => {
it("should return a string of the specified length", () => {
const str = randomString(10);
expect(str.length).toBe(10);
});
it("should work with seed", () => {
const state = Phaser.Math.RND.state();
const str1 = randomString(10, true);
Phaser.Math.RND.state(state);
const str2 = randomString(10, true);
expect(str1).toBe(str2);
});
});
});

View File

@ -4,7 +4,26 @@ import { defineConfig } from 'vite';
export default defineConfig(({ mode }) => {
return {
test: {
environment: 'happy-dom',
setupFiles: ['./src/test/vitest.setup.ts'],
environment: 'jsdom',
deps: {
optimizer: {
web: {
include: ['vitest-canvas-mock'],
}
}
},
threads: false,
environmentOptions: {
jsdom: {
resources: 'usable',
},
},
coverage: {
provider: 'istanbul',
reportsDirectory: 'coverage',
reporters: ['text-summary', 'html'],
},
},
plugins: [/*fs()*/],
server: { host: '0.0.0.0', port: 8000 },