Load all unit-tests with native import, rather than SystemJS

This commit is contained in:
Jonas Jenwald 2020-10-25 14:15:19 +01:00
parent d9084c0be2
commit 1c4495843c
3 changed files with 72 additions and 55 deletions

View File

@ -14,7 +14,6 @@
*/ */
/* globals __non_webpack_require__ */ /* globals __non_webpack_require__ */
import { setStubs, unsetStubs } from "../../examples/node/domstubs.js";
import { buildGetDocumentParams } from "./test_utils.js"; import { buildGetDocumentParams } from "./test_utils.js";
import { getDocument } from "../../src/display/api.js"; import { getDocument } from "../../src/display/api.js";
import { isNodeJS } from "../../src/shared/is_node.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. // This points to the XObject image in xobject-image.pdf.
const xobjectObjId = "img_p0_1"; const xobjectObjId = "img_p0_1";
if (isNodeJS) { if (isNodeJS) {
const { setStubs } = __non_webpack_require__(
"../../examples/node/domstubs.js"
);
setStubs(global); setStubs(global);
} }
try { try {
@ -102,6 +104,9 @@ describe("SVGGraphics", function () {
svgGfx.paintInlineImageXObject(imgData, elementContainer); svgGfx.paintInlineImageXObject(imgData, elementContainer);
} finally { } finally {
if (isNodeJS) { if (isNodeJS) {
const { unsetStubs } = __non_webpack_require__(
"../../examples/node/domstubs.js"
);
unsetStubs(global); unsetStubs(global);
} }
} }
@ -113,10 +118,10 @@ describe("SVGGraphics", function () {
function testFunc() { function testFunc() {
__non_webpack_require__("zlib"); __non_webpack_require__("zlib");
} }
// Verifies that the script loader replaces __non_webpack_require__ with
// require.
expect(testFunc.toString()).toMatch(/\srequire\(["']zlib["']\)/);
if (isNodeJS) { if (isNodeJS) {
// Verifies that the script loader replaces __non_webpack_require__ with
// require.
expect(testFunc.toString()).toMatch(/\srequire\(["']zlib["']\)/);
expect(testFunc).not.toThrow(); expect(testFunc).not.toThrow();
} else { } else {
// require not defined, require('zlib') not a module, etc. // require not defined, require('zlib') not a module, etc.

View File

@ -40,8 +40,8 @@
"use strict"; "use strict";
function initializePDFJS(callback) { async function initializePDFJS(callback) {
Promise.all( const modules = await Promise.all(
[ [
"pdfjs/display/api.js", "pdfjs/display/api.js",
"pdfjs/display/worker_options.js", "pdfjs/display/worker_options.js",
@ -83,40 +83,42 @@ function initializePDFJS(callback) {
"pdfjs-test/unit/writer_spec.js", "pdfjs-test/unit/writer_spec.js",
"pdfjs-test/unit/xml_spec.js", "pdfjs-test/unit/xml_spec.js",
].map(function (moduleName) { ].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 [
const { GlobalWorkerOptions } = modules[1]; { setPDFNetworkStreamFactory },
const { PDFNetworkStream } = modules[2]; { GlobalWorkerOptions },
const { PDFFetchStream } = modules[3]; { PDFNetworkStream },
const { isNodeJS } = modules[4]; { PDFFetchStream },
{ isNodeJS },
] = modules;
if (isNodeJS) { if (isNodeJS) {
throw new Error( throw new Error(
"The `gulp unittest` command cannot be used in Node.js environments." "The `gulp unittest` command cannot be used in Node.js environments."
); );
} }
// Set the network stream factory for unit-tests. // Set the network stream factory for unit-tests.
if ( if (
typeof Response !== "undefined" && typeof Response !== "undefined" &&
"body" in Response.prototype && "body" in Response.prototype &&
typeof ReadableStream !== "undefined" typeof ReadableStream !== "undefined"
) { ) {
displayApi.setPDFNetworkStreamFactory(function (params) { setPDFNetworkStreamFactory(function (params) {
return new PDFFetchStream(params); return new PDFFetchStream(params);
}); });
} else { } else {
displayApi.setPDFNetworkStreamFactory(function (params) { setPDFNetworkStreamFactory(function (params) {
return new PDFNetworkStream(params); return new PDFNetworkStream(params);
}); });
} }
// Configure the worker. // Configure the worker.
GlobalWorkerOptions.workerSrc = "../../build/generic/build/pdf.worker.js"; GlobalWorkerOptions.workerSrc = "../../build/generic/build/pdf.worker.js";
callback(); callback();
});
} }
(function () { (function () {
@ -197,26 +199,26 @@ function initializePDFJS(callback) {
// Sets longer timeout. // Sets longer timeout.
jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000; 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) { function extend(destination, source) {
for (const property in source) { for (const property in source) {
destination[property] = source[property]; destination[property] = source[property];
} }
return destination; return destination;
} }
function unitTestInit() {
initializePDFJS(function () {
htmlReporter.initialize();
env.execute();
});
}
if (
document.readyState === "interactive" ||
document.readyState === "complete"
) {
unitTestInit();
} else {
document.addEventListener("DOMContentLoaded", unitTestInit, true);
}
})(); })();

View File

@ -5,12 +5,22 @@
<link rel="stylesheet" type="text/css" href="../../node_modules/jasmine-core/lib/jasmine-core/jasmine.css"> <link rel="stylesheet" type="text/css" href="../../node_modules/jasmine-core/lib/jasmine-core/jasmine.css">
<script src="../../node_modules/systemjs/dist/system.js"></script>
<script src="../../systemjs.config.js"></script>
<script src="../../node_modules/jasmine-core/lib/jasmine-core/jasmine.js"></script> <script src="../../node_modules/jasmine-core/lib/jasmine-core/jasmine.js"></script>
<script src="../../node_modules/jasmine-core/lib/jasmine-core/jasmine-html.js"></script> <script src="../../node_modules/jasmine-core/lib/jasmine-core/jasmine-html.js"></script>
<script src="testreporter.js"></script> <script src="testreporter.js"></script>
<script src="jasmine-boot.js"></script>
<script defer src="../../node_modules/es-module-shims/dist/es-module-shims.js"></script>
<script type="importmap-shim">
{
"imports": {
"pdfjs/": "../../src/",
"pdfjs-lib": "../../src/pdf.js",
"pdfjs-web/": "../../web/",
"pdfjs-test/": "../"
}
}
</script>
<script src="jasmine-boot.js" type="module-shim"></script>
</head> </head>
<body> <body>
</body> </body>