refactor: improve testing setup

This commit is contained in:
Johannes Loher 2022-02-13 18:34:03 +01:00
parent 0c8f1e8bda
commit de060b381e
8 changed files with 48 additions and 30 deletions

View file

@ -29,5 +29,13 @@ module.exports = {
"@typescript-eslint/no-var-requires": "off",
},
},
{
files: ["./spec/**/*"],
env: {
browser: false,
},
extends: ["plugin:jest/recommended"],
plugins: ["jest"],
},
],
};

View file

@ -2,11 +2,15 @@
//
// SPDX-License-Identifier: MIT
export default {
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
const config = {
preset: "ts-jest",
globals: {
"ts-jest": {
tsconfig: "<rootDir>/spec/tsconfig.spec.json",
tsconfig: "<rootDir>/spec/tsconfig.json",
},
},
setupFiles: ["<rootDir>/spec/setup.ts"],
};
export default config;

View file

@ -2,14 +2,10 @@
//
// SPDX-License-Identifier: MIT
import * as fs from "fs-extra";
import * as path from "path";
import en from "../../lang/en.json";
import de from "../../lang/de.json";
describe("English and german localization files", () => {
const localizationPath = "./lang/";
const en: Record<string, unknown> = fs.readJSONSync(path.join(localizationPath, "en.json"));
const de: Record<string, unknown> = fs.readJSONSync(path.join(localizationPath, "de.json"));
it("should have the same keys.", () => {
const deKeys = Object.keys(de);
const enKeys = Object.keys(en);

View file

@ -5,39 +5,21 @@
import evaluateCheck from "../../src/rolls/check-evaluation";
class StubGame {
constructor() {
this.i18n = {
localize: (key: string) => key,
};
}
i18n: {
localize: (key: string) => string;
};
}
const game = new StubGame();
Object.defineProperties(globalThis, {
game: { value: game },
Game: { value: StubGame },
});
describe("evaluateCheck with no dice", () => {
it("should throw an error.", () => {
expect(() => evaluateCheck([], 10)).toThrow("DS4.ErrorInvalidNumberOfDice");
expect(() => evaluateCheck([], 10)).toThrow("Invalid number of dice.");
});
});
describe("evaluateCheck with more dice than required by the checkTargetNumber", () => {
it("should throw an error.", () => {
expect(() => evaluateCheck([10, 10], 10)).toThrow("DS4.ErrorInvalidNumberOfDice");
expect(() => evaluateCheck([10, 10], 10)).toThrow("Invalid number of dice.");
});
});
describe("evaluateCheck with less dice than required by the checkTargetNumber", () => {
it("should throw an error.", () => {
expect(() => evaluateCheck([10], 21)).toThrow("DS4.ErrorInvalidNumberOfDice");
expect(() => evaluateCheck([10], 21)).toThrow("Invalid number of dice.");
});
});

27
spec/setup.ts Normal file
View file

@ -0,0 +1,27 @@
// SPDX-FileCopyrightText: 2022 Johannes Loher
//
// SPDX-License-Identifier: MIT
import en from "../lang/en.json";
function setupStubs() {
class StubGame {
constructor() {
this.i18n = {
localize: (key: string) => (key in en ? en[key as keyof typeof en] : key),
};
}
i18n: {
localize: (key: string) => string;
};
}
const game = new StubGame();
Object.defineProperties(globalThis, {
game: { value: game },
Game: { value: StubGame },
});
}
setupStubs();

View file

@ -7,7 +7,8 @@
"moduleResolution": "node",
"forceConsistentCasingInFileNames": true,
"strict": true,
"noUncheckedIndexedAccess": true
"noUncheckedIndexedAccess": true,
"resolveJsonModule": true
},
"include": ["src"]
}