Skip to content

Commit e55ce71

Browse files
committed
Avoid top-level await in path.js
The current configuration of karma uses Webpack 4, which can't parse modules with top-level await.
1 parent f0cb47a commit e55ce71

File tree

1 file changed

+38
-14
lines changed

1 file changed

+38
-14
lines changed

test/utils/path.js

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
11
import { fileURLToPath } from "url";
2+
import nodePath from "path";
3+
import nodeUrl from "url";
24
import { host } from "@jsdevtools/host-environment";
35

46
const isWindows = /^win/.test(globalThis.process ? globalThis.process.platform : undefined);
57
const getPathFromOs = filePath => isWindows ? filePath.replace(/\\/g, "/") : filePath;
68

7-
let helpers;
8-
if (host.node) {
9-
helpers = await filesystemPathHelpers();;
10-
}
11-
else {
12-
helpers = urlPathHelpers();
13-
}
14-
export default helpers;
9+
const pathHelpers = {
10+
filesystem: filesystemPathHelpers(),
11+
url: urlPathHelpers()
12+
};
1513

1614
/**
1715
* Helper functions for getting local filesystem paths in various formats
1816
*/
19-
async function filesystemPathHelpers () {
20-
const nodePath = await import("path");
21-
const nodeUrl = await import("url");
17+
function filesystemPathHelpers () {
2218

23-
const testsDir = nodePath.resolve(fileURLToPath(new URL(".", import.meta.url)), "..");
19+
if (host.node) {
20+
const testsDir = nodePath.resolve(fileURLToPath(new URL(".", import.meta.url)), "..");
2421

25-
// Run all tests from the "test" directory
26-
process.chdir(testsDir);
22+
// Run all tests from the "test" directory
23+
process.chdir(testsDir);
24+
}
2725

2826
const path = {
2927
/**
@@ -85,6 +83,10 @@ async function filesystemPathHelpers () {
8583
* Helper functions for getting URLs in various formats
8684
*/
8785
function urlPathHelpers () {
86+
if (host.node) {
87+
return
88+
}
89+
8890
// Get the URL of the "test" directory
8991
let filename = document.querySelector('script[src*="/fixtures/"]').src;
9092
let testsDir = filename.substr(0, filename.indexOf("/fixtures/")) + "/";
@@ -149,3 +151,25 @@ function urlPathHelpers () {
149151

150152
return path;
151153
}
154+
155+
export default {
156+
rel (file) {
157+
return host.node ? pathHelpers.filesystem.rel(...arguments) : pathHelpers.url.rel(...arguments)
158+
},
159+
160+
abs (file) {
161+
return host.node ? pathHelpers.filesystem.abs(...arguments) : pathHelpers.url.abs(...arguments)
162+
},
163+
164+
unixify (file) {
165+
return host.node ? pathHelpers.filesystem.unixify(...arguments) : pathHelpers.url.unixify(...arguments)
166+
},
167+
168+
url (file) {
169+
return host.node ? pathHelpers.filesystem.url(...arguments) : pathHelpers.url.url(...arguments)
170+
},
171+
172+
cwd () {
173+
return host.node ? pathHelpers.filesystem.cwd(...arguments) : pathHelpers.url.cwd(...arguments)
174+
}
175+
}

0 commit comments

Comments
 (0)