From 4414a4f7e4044005a9ae1ea77fad5b0bdeb0c26a Mon Sep 17 00:00:00 2001 From: Daan Vanden Bosch Date: Wed, 25 Dec 2019 00:53:17 +0100 Subject: [PATCH] Improved application startup test. --- jest.config.js | 2 +- package.json | 1 + src/application/index.test.ts | 8 ++++---- src/core/Logger.ts | 14 ++++++++++++-- yarn.lock | 25 +++++++++++++++++++++++++ 5 files changed, 43 insertions(+), 7 deletions(-) diff --git a/jest.config.js b/jest.config.js index 2c040c69..1a220470 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,7 +1,7 @@ module.exports = { preset: "ts-jest", moduleDirectories: ["node_modules"], - setupFiles: ["./test/src/setup.js"], + setupFiles: ["./test/src/setup.js", "jest-canvas-mock"], roots: ["./src", "./test"], moduleNameMapper: { "\\.(css|gif|jpg|png|svg|ttf)$": "/src/__mocks__/static_files.js", diff --git a/package.json b/package.json index d6a9a9a0..ff15565b 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "fork-ts-checker-webpack-plugin": "^3.1.1", "html-webpack-plugin": "^3.2.0", "jest": "^24.9.0", + "jest-canvas-mock": "^2.2.0", "mini-css-extract-plugin": "^0.9.0", "monaco-editor-webpack-plugin": "^1.8.1", "optimize-css-assets-webpack-plugin": "^5.0.3", diff --git a/src/application/index.test.ts b/src/application/index.test.ts index d39d077a..206fc2f9 100644 --- a/src/application/index.test.ts +++ b/src/application/index.test.ts @@ -1,17 +1,17 @@ import { initialize_application } from "./index"; import { DisposableThreeRenderer } from "../core/rendering/Renderer"; -import { LogManager, LogHandler, LogLevel } from "../core/Logger"; +import { LogHandler, LogLevel, LogManager } from "../core/Logger"; import { FileSystemHttpClient } from "../../test/src/core/FileSystemHttpClient"; import { timeout } from "../../test/src/utils"; for (const path of [undefined, "/viewer", "/quest_editor", "/hunt_optimizer"]) { const with_path = path == undefined ? "without specific path" : `with path ${path}`; - test(`Initialization and shutdown ${with_path} should succeed without throwing errors or logging with level Warn or above.`, async () => { + test(`Initialization and shutdown ${with_path} should succeed without throwing or logging errors.`, async () => { const logged_errors: string[] = []; const handler: LogHandler = ({ level, message }) => { - if (level >= LogLevel.Warn) { + if (level >= LogLevel.Error) { logged_errors.push(message); } }; @@ -29,7 +29,7 @@ for (const path of [undefined, "/viewer", "/quest_editor", "/hunt_optimizer"]) { expect(app).toBeDefined(); expect(logged_errors).toEqual([]); - await timeout(2000); + await timeout(1000); app.dispose(); diff --git a/src/core/Logger.ts b/src/core/Logger.ts index 5e63c158..8f712275 100644 --- a/src/core/Logger.ts +++ b/src/core/Logger.ts @@ -143,12 +143,22 @@ export class LogManager { static with_default_handler(handler: LogHandler, f: () => T): T { const orig_handler = this.default_handler; + let is_promise = false; try { this.default_handler = handler; - return f(); + const r = f(); + + if (r instanceof Promise) { + is_promise = true; + return (r.finally(() => (this.default_handler = orig_handler)) as unknown) as T; + } else { + return r; + } } finally { - this.default_handler = orig_handler; + if (!is_promise) { + this.default_handler = orig_handler; + } } } } diff --git a/yarn.lock b/yarn.lock index 09d70946..b71ddb13 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1612,6 +1612,11 @@ color-convert@^1.9.0, color-convert@^1.9.1: dependencies: color-name "1.1.3" +color-convert@~0.5.0: + version "0.5.3" + resolved "https://registry.yarnpkg.com/color-convert/-/color-convert-0.5.3.tgz#bdb6c69ce660fadffe0b0007cc447e1b9f7282bd" + integrity sha1-vbbGnOZg+t/+CwAHzER+G59ygr0= + color-name@1.1.3: version "1.1.3" resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.3.tgz#a7d0558bd89c42f795dd42328f740831ca53bc25" @@ -1947,6 +1952,11 @@ cssesc@^3.0.0: resolved "https://registry.yarnpkg.com/cssesc/-/cssesc-3.0.0.tgz#37741919903b868565e1c09ea747445cd18983ee" integrity sha512-/Tb/JcjK111nNScGob5MNtsntNM1aCNUDipB/TkwZFhyDrrE47SOx/18wF2bbjgc3ZzCSKW1T5nt5EbFoAz/Vg== +cssfontparser@^1.2.1: + version "1.2.1" + resolved "https://registry.yarnpkg.com/cssfontparser/-/cssfontparser-1.2.1.tgz#f4022fc8f9700c68029d542084afbaf425a3f3e3" + integrity sha1-9AIvyPlwDGgCnVQghK+69CWj8+M= + cssnano-preset-default@^4.0.7: version "4.0.7" resolved "https://registry.yarnpkg.com/cssnano-preset-default/-/cssnano-preset-default-4.0.7.tgz#51ec662ccfca0f88b396dcd9679cdb931be17f76" @@ -4029,6 +4039,14 @@ javascript-lp-solver@0.4.17: resolved "https://registry.yarnpkg.com/javascript-lp-solver/-/javascript-lp-solver-0.4.17.tgz#dae77b9202af23e4da656e72836e4d2b3c3a6eee" integrity sha512-kJtkZ0RRaCUx7cKu/EDSvjftbZSaLMx3KQ1NL3YS7ZfXhC7bqLtjY1pkkn9cP3cii8HghD3VpyvBFILXyBP07A== +jest-canvas-mock@^2.2.0: + version "2.2.0" + resolved "https://registry.yarnpkg.com/jest-canvas-mock/-/jest-canvas-mock-2.2.0.tgz#45fbc58589c6ce9df50dc90bd8adce747cbdada7" + integrity sha512-DcJdchb7eWFZkt6pvyceWWnu3lsp5QWbUeXiKgEMhwB3sMm5qHM1GQhDajvJgBeiYpgKcojbzZ53d/nz6tXvJw== + dependencies: + cssfontparser "^1.2.1" + parse-color "^1.0.0" + jest-changed-files@^24.9.0: version "24.9.0" resolved "https://registry.yarnpkg.com/jest-changed-files/-/jest-changed-files-24.9.0.tgz#08d8c15eb79a7fa3fc98269bc14b451ee82f8039" @@ -5464,6 +5482,13 @@ parse-asn1@^5.0.0: pbkdf2 "^3.0.3" safe-buffer "^5.1.1" +parse-color@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/parse-color/-/parse-color-1.0.0.tgz#7b748b95a83f03f16a94f535e52d7f3d94658619" + integrity sha1-e3SLlag/A/FqlPU15S1/PZRlhhk= + dependencies: + color-convert "~0.5.0" + parse-json@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/parse-json/-/parse-json-4.0.0.tgz#be35f5425be1f7f6c747184f98a788cb99477ee0"