Add unit testing support with vitest (#18)

* update loose files and add vitest

* update test scripts

* more support for vitest

* more test support

* update vscode settings
This commit is contained in:
Devin Korb 2024-03-30 20:47:47 -04:00 committed by GitHub
parent 4e911a9be9
commit 53def01b51
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 3232 additions and 41 deletions

View File

@ -1,6 +0,0 @@
# don't ever lint node_modules
node_modules
# don't lint build output (make sure it's set to your correct build folder name)
dist
index.html
.eslintrc.cjs

17
.eslintrc Normal file
View File

@ -0,0 +1,17 @@
{
"env": {
"browser": true,
"es2021": true
},
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"overrides": [
{
"files": ["src/**/*.ts"],
"extends": "eslint:recommended"
}
],
"rules": {}
}

View File

@ -1,13 +0,0 @@
module.exports = {
env: {
browser: true,
es2021: true,
},
extends: 'eslint:recommended',
overrides: [],
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
},
rules: {},
}

3
.gitignore vendored
View File

@ -15,6 +15,7 @@ dist-ssr
# Editor directories and files # Editor directories and files
.vscode/* .vscode/*
!.vscode/extensions.json !.vscode/extensions.json
!.vscode/settings.json
.idea .idea
.DS_Store .DS_Store
*.suo *.suo
@ -32,3 +33,5 @@ public/images/pokemon/icons/input/output/*
public/images/character/*/ public/images/character/*/
src/data/battle-anim-raw-data*.ts src/data/battle-anim-raw-data*.ts
src/data/battle-anim-data.ts src/data/battle-anim-data.ts
coverage

4
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,4 @@
{
"javascript.preferences.importModuleSpecifierEnding": "minimal",
"typescript.preferences.importModuleSpecifierEnding": "minimal"
}

View File

@ -1,11 +0,0 @@
{
"compilerOptions": {
"target": "ES2020",
"module": "ES2020",
"moduleResolution": "node",
"resolveJsonModule": true,
"checkJs": true,
"esModuleInterop": true,
"strictNullChecks": false
}
}

3109
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -6,17 +6,25 @@
"scripts": { "scripts": {
"start": "vite", "start": "vite",
"build": "vite build", "build": "vite build",
"preview": "vite preview" "preview": "vite preview",
"test": "vitest run",
"test:cov": "vitest run --coverage",
"test:watch": "vitest watch --coverage"
}, },
"devDependencies": { "devDependencies": {
"@vitest/coverage-istanbul": "^1.4.0",
"axios": "^1.6.2", "axios": "^1.6.2",
"axios-cache-interceptor": "^1.3.2", "axios-cache-interceptor": "^1.3.2",
"eslint": "^8.25.0", "eslint": "^8.25.0",
"jsdom": "^24.0.0",
"json-beautify": "^1.1.1", "json-beautify": "^1.1.1",
"phaser3spectorjs": "^0.0.8",
"pokenode-ts": "^1.20.0", "pokenode-ts": "^1.20.0",
"typescript": "^5.0.3", "typescript": "^5.0.3",
"vite": "^4.5.0", "vite": "^4.5.0",
"vite-plugin-fs": "^0.4.4" "vite-plugin-fs": "^0.4.4",
"vitest": "^1.4.0",
"vitest-canvas-mock": "^0.3.3"
}, },
"dependencies": { "dependencies": {
"@material/material-color-utilities": "^0.2.7", "@material/material-color-utilities": "^0.2.7",
@ -24,5 +32,12 @@
"json-stable-stringify": "^1.1.0", "json-stable-stringify": "^1.1.0",
"phaser": "^3.70.0", "phaser": "^3.70.0",
"phaser3-rex-plugins": "^1.1.84" "phaser3-rex-plugins": "^1.1.84"
},
"engines": {
"node": ">=18.0.0"
},
"imports": {
"#app": "./src/main.js",
"#app/*": "./src/*"
} }
} }

5
src/test/phaser.setup.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/phaser.setup";

22
src/utils.test.ts Normal file
View File

@ -0,0 +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);
});
});
});

19
tsconfig.json Normal file
View File

@ -0,0 +1,19 @@
{
"compilerOptions": {
"target": "ES2020",
"module": "ES2020",
"moduleResolution": "Bundler",
"resolveJsonModule": true,
"esModuleInterop": true,
"strictNullChecks": false,
"sourceMap": true,
"rootDir": "./src",
"baseUrl": "./src",
"paths": {
"#app/*": ["*.ts"],
"#app": ["."]
},
"outDir": "./build",
"noEmit": true
}
}

View File

@ -8,10 +8,11 @@ export default defineConfig(({ mode }) => {
clearScreen: false, clearScreen: false,
build: { build: {
minify: 'esbuild', minify: 'esbuild',
sourcemap: true
}, },
esbuild: { esbuild: {
pure: mode === 'production' ? [ 'console.log' ] : [], pure: mode === 'production' ? [ 'console.log' ] : [],
keepNames: true, keepNames: true,
} },
} }
}) })

40
vitest.config.js Normal file
View File

@ -0,0 +1,40 @@
import { defineConfig } from 'vite';
// import fs from 'vite-plugin-fs';
export default defineConfig(({ mode }) => {
return {
test: {
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 },
clearScreen: false,
build: {
minify: 'esbuild',
sourcemap: true
},
esbuild: {
pure: mode === 'production' ? [ 'console.log' ] : [],
keepNames: true,
},
}
})