diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 2802a161..a0729fae 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -29,5 +29,13 @@ module.exports = { "@typescript-eslint/no-var-requires": "off", }, }, + { + files: ["./spec/**/*"], + env: { + browser: false, + }, + extends: ["plugin:jest/recommended"], + plugins: ["jest"], + }, ], }; diff --git a/jest.config.js b/jest.config.js index 3e74f815..7d3bde02 100644 --- a/jest.config.js +++ b/jest.config.js @@ -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: "/spec/tsconfig.spec.json", + tsconfig: "/spec/tsconfig.json", }, }, + setupFiles: ["/spec/setup.ts"], }; + +export default config; diff --git a/spec/localization/localization.spec.ts b/spec/localization/localization.spec.ts index 4c62e45a..a050cf71 100644 --- a/spec/localization/localization.spec.ts +++ b/spec/localization/localization.spec.ts @@ -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 = fs.readJSONSync(path.join(localizationPath, "en.json")); - const de: Record = fs.readJSONSync(path.join(localizationPath, "de.json")); - it("should have the same keys.", () => { const deKeys = Object.keys(de); const enKeys = Object.keys(en); diff --git a/spec/rolls/check-evaluation.spec.ts b/spec/rolls/check-evaluation.spec.ts index bde1ddbf..5e5e954c 100644 --- a/spec/rolls/check-evaluation.spec.ts +++ b/spec/rolls/check-evaluation.spec.ts @@ -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."); }); }); diff --git a/spec/setup.ts b/spec/setup.ts new file mode 100644 index 00000000..9b10aba6 --- /dev/null +++ b/spec/setup.ts @@ -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(); diff --git a/spec/tsconfig.spec.json b/spec/tsconfig.json similarity index 100% rename from spec/tsconfig.spec.json rename to spec/tsconfig.json diff --git a/spec/tsconfig.spec.json.license b/spec/tsconfig.json.license similarity index 100% rename from spec/tsconfig.spec.json.license rename to spec/tsconfig.json.license diff --git a/tsconfig.json b/tsconfig.json index b8fbff11..7a7a51ce 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,7 +7,8 @@ "moduleResolution": "node", "forceConsistentCasingInFileNames": true, "strict": true, - "noUncheckedIndexedAccess": true + "noUncheckedIndexedAccess": true, + "resolveJsonModule": true }, "include": ["src"] }