Skip to content

Commit 0ab0d6f

Browse files
authored
Fix browser support bug (#111)
* Add checks for availability of `process` to ensure browser compat. * Add browser tests using jasmine-browser-runner
1 parent b326475 commit 0ab0d6f

File tree

8 files changed

+1200
-25
lines changed

8 files changed

+1200
-25
lines changed

package-lock.json

Lines changed: 1117 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
"test:lint": "npx eslint .",
3333
"test:browserslist": "mkdir test-browserslist && cd test-browserslist && npm init -y && npm i ../../baseline-browser-mapping browserslist &&jq '. += {\"browserslist\":[\"baseline widely available with downstream\"]}' package.json >p && mv p package.json && npx browserslist && cd ../ && rm -rf test-browserslist",
3434
"test:jasmine": "npx jasmine",
35-
"test": "npm run build && npm run fix-cli-permissions && rm -rf test-browserslist && npm run test:format && npm run test:lint && npx jasmine && npm run test:browserslist",
35+
"test:jasmine-browser": "npx jasmine-browser-runner runSpecs --config ./spec/support/jasmine-browser.js",
36+
"test": "npm run build && npm run fix-cli-permissions && rm -rf test-browserslist && npm run test:format && npm run test:lint && npm run test:jasmine && npm run test:jasmine-browser && npm run test:browserslist",
3637
"build": "rm -rf dist; npx prettier . --write; rollup -c; rm -rf ./dist/scripts/expose-data.d.ts ./dist/cli.d.ts",
3738
"refresh-downstream": "npx tsx scripts/refresh-downstream.ts",
3839
"refresh-static": "npx tsx scripts/refresh-static.ts",
@@ -48,6 +49,7 @@
4849
"@types/node": "^22.15.17",
4950
"eslint-plugin-new-with-error": "^5.0.0",
5051
"jasmine": "^5.8.0",
52+
"jasmine-browser-runner": "^3.0.0",
5153
"jasmine-spec-reporter": "^7.0.0",
5254
"prettier": "^3.5.3",
5355
"rollup": "^4.44.0",

spec/support/jasmine-browser.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
export default {
2+
srcDir: "src",
3+
// srcFiles should usually be left empty when using ES modules, because you'll
4+
// explicitly import sources from your specs.
5+
srcFiles: [],
6+
specDir: ".",
7+
specFiles: ["spec/tests/index.browser.js"],
8+
// helpers: ["spec/helpers/**/*.?(m)js"],
9+
esmFilenameExtension: ".js",
10+
// Set to true if you need to load module src files instead of loading via the spec files.
11+
modulesWithSideEffectsInSrcFiles: false,
12+
// Allows the use of top-level await in src/spec/helper files. This is off by
13+
// default because it makes files load more slowly.
14+
enableTopLevelAwait: false,
15+
env: {
16+
stopSpecOnExpectationFailure: false,
17+
stopOnSpecFailure: false,
18+
random: true,
19+
// Fail if a suite contains multiple suites or specs with the same name.
20+
forbidDuplicateNames: true,
21+
},
22+
23+
// For security, listen only to localhost. You can also specify a different
24+
// hostname or IP address, or remove the property or set it to "*" to listen
25+
// to all network interfaces.
26+
listenAddress: "localhost",
27+
28+
// The hostname that the browser will use to connect to the server.
29+
hostname: "localhost",
30+
31+
browser: {
32+
name: "headlessChrome",
33+
},
34+
useConsoleReporter: "true",
35+
};

spec/support/jasmine.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export default {
22
spec_dir: "spec",
33
helpers: ["helpers/**/*.?(m)js"],
4-
spec_files: ["tests/*.js"],
4+
spec_files: ["tests/*.node.js"],
55
env: {
66
stopSpecOnExpectationFailure: false,
77
random: true,
File renamed without changes.

spec/tests/index.browser.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { getCompatibleVersions, getAllVersions } from "../../dist/index.js";
2+
3+
describe("baseline-browser-mapping in browser", function () {
4+
it("should load getCompatibleVersions", function () {
5+
expect(typeof getCompatibleVersions).toBe("function");
6+
});
7+
8+
it("should load getAllVersions", function () {
9+
expect(typeof getAllVersions).toBe("function");
10+
});
11+
12+
it("getCompatibleVersions should return an array", function () {
13+
const versions = getCompatibleVersions();
14+
expect(Array.isArray(versions)).toBe(true);
15+
});
16+
17+
it("getAllVersions should return an object", function () {
18+
const versions = getAllVersions();
19+
expect(typeof versions).toBe("object");
20+
expect(versions).not.toBeNull();
21+
});
22+
});
File renamed without changes.

src/index.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import {
66
} from "./scripts/expose-data.js";
77

88
try {
9-
if (typeof process.loadEnvFile === "function") {
9+
if (
10+
typeof process !== "undefined" &&
11+
typeof process.loadEnvFile === "function"
12+
) {
1013
process.loadEnvFile();
1114
}
1215
} catch (e) {
@@ -22,8 +25,10 @@ export function _resetHasWarned() {
2225
const checkUpdate = (targetDate: Date, lastUpdatedOverride?: number) => {
2326
if (
2427
hasWarned ||
25-
process.env.BROWSERSLIST_IGNORE_OLD_DATA ||
26-
process.env.BASELINE_BROWSER_MAPPING_IGNORE_OLD_DATA
28+
(typeof process !== "undefined" &&
29+
process.env &&
30+
(process.env.BROWSERSLIST_IGNORE_OLD_DATA ||
31+
process.env.BASELINE_BROWSER_MAPPING_IGNORE_OLD_DATA))
2732
) {
2833
return;
2934
}
@@ -144,7 +149,13 @@ const kaiOSWarning = (options: Options | AllVersionsOptions) => {
144149
"KaiOS is a downstream browser and can only be included if you include other downstream browsers. Please ensure you use `includeDownstreamBrowsers: true`.",
145150
),
146151
);
147-
process.exit(1);
152+
if (typeof process !== "undefined" && process.exit) {
153+
process.exit(1);
154+
} else {
155+
throw new Error(
156+
"KaiOS configuration error: process.exit is not available",
157+
);
158+
}
148159
}
149160
};
150161

@@ -469,7 +480,13 @@ export function getCompatibleVersions(userOptions?: Options): BrowserVersion[] {
469480
"You cannot use targetYear and widelyAvailableOnDate at the same time. Please remove one of these options and try again.",
470481
),
471482
);
472-
process.exit(1);
483+
if (typeof process !== "undefined" && process.exit) {
484+
process.exit(1);
485+
} else {
486+
throw new Error(
487+
"Configuration error: targetYear and widelyAvailableOnDate cannot be used together",
488+
);
489+
}
473490
} else if (options.widelyAvailableOnDate) {
474491
targetDate = new Date(options.widelyAvailableOnDate);
475492
} else if (options.targetYear) {

0 commit comments

Comments
 (0)