Skip to content

Commit

Permalink
Use modern pathToFileURL in tests
Browse files Browse the repository at this point in the history
Also delete some unneeded utils.
  • Loading branch information
domenic committed Nov 26, 2023
1 parent 1ee4d61 commit 59ebd7e
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 49 deletions.
37 changes: 0 additions & 37 deletions lib/jsdom/utils.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
"use strict";
const path = require("path");
const whatwgURL = require("whatwg-url");
const { domSymbolTree } = require("./living/helpers/internal-constants");
const SYMBOL_TREE_POSITION = require("symbol-tree").TreePosition;

exports.toFileUrl = function (fileName) {
// Beyond just the `path.resolve`, this is mostly for the benefit of Windows,
// where we need to convert "\" to "/" and add an extra "/" prefix before the
// drive letter.
let pathname = path.resolve(process.cwd(), fileName).replace(/\\/g, "/");
if (pathname[0] !== "/") {
pathname = "/" + pathname;
}

// path might contain spaces, so convert those to %20
return "file://" + encodeURI(pathname);
};

/**
* Define a set of properties on an object, by copying the property descriptors
* from the original object.
Expand All @@ -31,29 +17,6 @@ exports.define = function define(object, properties) {
}
};

/**
* Define a list of constants on a constructor and its .prototype
*
* - `Constructor` {Function} the constructor to define the constants on
* - `propertyMap` {Object} key/value map of properties to define
*/
exports.addConstants = function addConstants(Constructor, propertyMap) {
for (const property in propertyMap) {
const value = propertyMap[property];
addConstant(Constructor, property, value);
addConstant(Constructor.prototype, property, value);
}
};

function addConstant(object, property, value) {
Object.defineProperty(object, property, {
configurable: false,
enumerable: true,
writable: false,
value
});
}

exports.mixin = (target, source) => {
const keys = Reflect.ownKeys(source);
for (let i = 0; i < keys.length; ++i) {
Expand Down
8 changes: 2 additions & 6 deletions test/api/from-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const path = require("path");
const { assert } = require("chai");
const { describe, it } = require("mocha-sugar-free");
const { pathToFileURL } = require("url");

const { JSDOM } = require("../..");

Expand Down Expand Up @@ -81,12 +82,7 @@ describe("API: JSDOM.fromFile()", () => {
});

describe("url option defaulting", () => {
// Manually construct it as much as possible to avoid logic in the tests
let pathWithLeadingSlash = path.resolve(__dirname);
if (!pathWithLeadingSlash.startsWith("/")) {
pathWithLeadingSlash = "/" + pathWithLeadingSlash;
}
const testURL = "file://" + pathWithLeadingSlash.replace(/\\/g, "/") + "/fixtures/from-file/test.html";
const testURL = pathToFileURL(path.resolve(__dirname, "fixtures/from-file/test.html")).href;

it("should default to a file URL derived from the filename", () => {
return fromFixtureFile("test.html").then(dom => {
Expand Down
9 changes: 3 additions & 6 deletions test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const https = require("https");
const enableDestroy = require("server-destroy");
const { JSDOM } = require("..");
const { Canvas } = require("../lib/jsdom/utils");
const { pathToFileURL } = require("url");

function toPathname(dirname, relativePath) {
let pathname = path.resolve(dirname, relativePath).replace(/\\/g, "/");
Expand All @@ -15,13 +16,9 @@ function toPathname(dirname, relativePath) {
return pathname;
}

function toFileUrl(dirname, relativePath) {
return "file://" + toPathname(dirname, relativePath);
}

exports.toFileUrl = dirname => {
return function (relativePath) {
return toFileUrl(dirname, relativePath);
return pathToFileURL(path.resolve(dirname, relativePath)).href;
};
};

Expand All @@ -38,7 +35,7 @@ exports.load = dirname => {
const file = path.resolve(dirname, "files/" + name + ".html");

if (!options.url) {
options.url = toFileUrl(dirname, file);
options.url = pathToFileURL(path.resolve(dirname, file)).href;
}

const contents = fileCache[file] || fs.readFileSync(file, "utf8");
Expand Down

0 comments on commit 59ebd7e

Please sign in to comment.