From 1c4495843cf9aa861087213ad4640b792f779195 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 25 Oct 2020 14:15:19 +0100 Subject: [PATCH] Load all unit-tests with native `import`, rather than SystemJS --- test/unit/display_svg_spec.js | 13 +++-- test/unit/jasmine-boot.js | 98 ++++++++++++++++++----------------- test/unit/unit_test.html | 16 ++++-- 3 files changed, 72 insertions(+), 55 deletions(-) diff --git a/test/unit/display_svg_spec.js b/test/unit/display_svg_spec.js index cd8f58758..bac135af6 100644 --- a/test/unit/display_svg_spec.js +++ b/test/unit/display_svg_spec.js @@ -14,7 +14,6 @@ */ /* globals __non_webpack_require__ */ -import { setStubs, unsetStubs } from "../../examples/node/domstubs.js"; import { buildGetDocumentParams } from "./test_utils.js"; import { getDocument } from "../../src/display/api.js"; import { isNodeJS } from "../../src/shared/is_node.js"; @@ -95,6 +94,9 @@ describe("SVGGraphics", function () { // This points to the XObject image in xobject-image.pdf. const xobjectObjId = "img_p0_1"; if (isNodeJS) { + const { setStubs } = __non_webpack_require__( + "../../examples/node/domstubs.js" + ); setStubs(global); } try { @@ -102,6 +104,9 @@ describe("SVGGraphics", function () { svgGfx.paintInlineImageXObject(imgData, elementContainer); } finally { if (isNodeJS) { + const { unsetStubs } = __non_webpack_require__( + "../../examples/node/domstubs.js" + ); unsetStubs(global); } } @@ -113,10 +118,10 @@ describe("SVGGraphics", function () { function testFunc() { __non_webpack_require__("zlib"); } - // Verifies that the script loader replaces __non_webpack_require__ with - // require. - expect(testFunc.toString()).toMatch(/\srequire\(["']zlib["']\)/); if (isNodeJS) { + // Verifies that the script loader replaces __non_webpack_require__ with + // require. + expect(testFunc.toString()).toMatch(/\srequire\(["']zlib["']\)/); expect(testFunc).not.toThrow(); } else { // require not defined, require('zlib') not a module, etc. diff --git a/test/unit/jasmine-boot.js b/test/unit/jasmine-boot.js index f05126d9d..2faa022b7 100644 --- a/test/unit/jasmine-boot.js +++ b/test/unit/jasmine-boot.js @@ -40,8 +40,8 @@ "use strict"; -function initializePDFJS(callback) { - Promise.all( +async function initializePDFJS(callback) { + const modules = await Promise.all( [ "pdfjs/display/api.js", "pdfjs/display/worker_options.js", @@ -83,40 +83,42 @@ function initializePDFJS(callback) { "pdfjs-test/unit/writer_spec.js", "pdfjs-test/unit/xml_spec.js", ].map(function (moduleName) { - return SystemJS.import(moduleName); + // eslint-disable-next-line no-unsanitized/method + return import(moduleName); }) - ).then(function (modules) { - const displayApi = modules[0]; - const { GlobalWorkerOptions } = modules[1]; - const { PDFNetworkStream } = modules[2]; - const { PDFFetchStream } = modules[3]; - const { isNodeJS } = modules[4]; + ); + const [ + { setPDFNetworkStreamFactory }, + { GlobalWorkerOptions }, + { PDFNetworkStream }, + { PDFFetchStream }, + { isNodeJS }, + ] = modules; - if (isNodeJS) { - throw new Error( - "The `gulp unittest` command cannot be used in Node.js environments." - ); - } - // Set the network stream factory for unit-tests. - if ( - typeof Response !== "undefined" && - "body" in Response.prototype && - typeof ReadableStream !== "undefined" - ) { - displayApi.setPDFNetworkStreamFactory(function (params) { - return new PDFFetchStream(params); - }); - } else { - displayApi.setPDFNetworkStreamFactory(function (params) { - return new PDFNetworkStream(params); - }); - } + if (isNodeJS) { + throw new Error( + "The `gulp unittest` command cannot be used in Node.js environments." + ); + } + // Set the network stream factory for unit-tests. + if ( + typeof Response !== "undefined" && + "body" in Response.prototype && + typeof ReadableStream !== "undefined" + ) { + setPDFNetworkStreamFactory(function (params) { + return new PDFFetchStream(params); + }); + } else { + setPDFNetworkStreamFactory(function (params) { + return new PDFNetworkStream(params); + }); + } - // Configure the worker. - GlobalWorkerOptions.workerSrc = "../../build/generic/build/pdf.worker.js"; + // Configure the worker. + GlobalWorkerOptions.workerSrc = "../../build/generic/build/pdf.worker.js"; - callback(); - }); + callback(); } (function () { @@ -197,26 +199,26 @@ function initializePDFJS(callback) { // Sets longer timeout. jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000; - // Replace the browser window's `onload`, ensure it's called, and then run - // all of the loaded specs. This includes initializing the `HtmlReporter` - // instance and then executing the loaded Jasmine environment. - const currentWindowOnload = window.onload; - - window.onload = function () { - if (currentWindowOnload) { - currentWindowOnload(); - } - - initializePDFJS(function () { - htmlReporter.initialize(); - env.execute(); - }); - }; - function extend(destination, source) { for (const property in source) { destination[property] = source[property]; } return destination; } + + function unitTestInit() { + initializePDFJS(function () { + htmlReporter.initialize(); + env.execute(); + }); + } + + if ( + document.readyState === "interactive" || + document.readyState === "complete" + ) { + unitTestInit(); + } else { + document.addEventListener("DOMContentLoaded", unitTestInit, true); + } })(); diff --git a/test/unit/unit_test.html b/test/unit/unit_test.html index d9528e816..befddc1e4 100644 --- a/test/unit/unit_test.html +++ b/test/unit/unit_test.html @@ -5,12 +5,22 @@ - - - + + + +