Skip to content

Commit da9e32e

Browse files
authored
Fix CI tests (#294)
* Update package-lock.json file * Fix broken tests in blank.spec.js Blank binary file was not getting serialized as { type: "Buffer", data: [] } because read(file) was returning a string instead of Buffer. * Fix webpack error when running browser tests Webpack 4 cannot parse module containing optional chaining operator. See https://stackoverflow.com/questions/59972341/how-to-make-webpack-accept-optional-chaining-without-babel * Fix tests in invalid.spec.js when running in Chrome * Change statusCode to status * Set the --openssl-legacy-provider flag when running browser tests Recent versions of node upgraded to OpenSSL 3.0, which deprecated some older crypto hashing algorithms including md4. Webpack 4 hard codes the use of md4 ins some places, so we have to set this flag until we can upgrade Webpack. * Use isomorphic-fetch instead of node-fetch Browser tests were failing as polyfill.js was loading a node package in a browser context. * Increase minimum version of node to 17 This was when the --openssl-legacy-provider option was introduced. Prior versions of node do not recognize this option. * Add chokidar@3 as an explicit dev dependency Browser tests are failing in the CI environment with: chokidar@3: Error: Cannot find module 'chokidar' Don't know why but this thread might be relevant: facebook/create-react-app#10811 * Skip test assertion due to conflict on Windows See also #286 * Explicitly define browsers and plugins in karma.conf.js * Replace karma-edge-launcher with newer package
1 parent 95f5948 commit da9e32e

File tree

12 files changed

+411
-299
lines changed

12 files changed

+411
-299
lines changed

.github/workflows/CI-CD.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
- macos-latest
2626
- windows-latest
2727
node:
28-
- 14
28+
- 17
2929
- lts/*
3030
- current
3131

karma.conf.js

Lines changed: 65 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,70 @@
44

55
"use strict";
66

7-
const { karmaConfig } = require("@jsdevtools/karma-config");
7+
const nodeUtil = require("util");
8+
const { buildConfig } = require("@jsdevtools/karma-config");
89
const { host } = require("@jsdevtools/host-environment");
910

10-
module.exports = karmaConfig({
11-
sourceDir: "lib",
12-
fixtures: "test/fixtures/**/*.js",
13-
browsers: {
14-
chrome: host.ci ? host.os.linux : true,
15-
firefox: host.ci ? host.os.linux : true,
16-
// TODO these were disabled as unstable. should we remove saucelabs or what?
17-
// safari: host.ci ? host.os.linux : host.os.mac, // SauceLabs in CI
18-
edge: host.ci ? host.os.linux : host.os.windows, // SauceLabs in CI
19-
// ie: host.ci ? host.os.windows : false, // IE needs to run by itself, due to Babel transforms
20-
},
21-
});
11+
module.exports = (karma) => {
12+
13+
const browsers = [];
14+
const CI = isCI();
15+
browsers.push(CI ? "ChromeHeadless" : "Chrome");
16+
browsers.push(CI ? "FirefoxHeadless" : "Firefox");
17+
host.os.mac && browsers.push("Safari");
18+
host.os.windows && browsers.push("Edge");
19+
20+
const plugins = [
21+
require("@jsdevtools/karma-host-environment"),
22+
require("karma-verbose-reporter"),
23+
require("karma-mocha"),
24+
require("karma-webpack"),
25+
]
26+
plugins.push(require("karma-chrome-launcher"))
27+
plugins.push(require("karma-firefox-launcher"))
28+
host.os.mac && plugins.push(require("karma-safari-launcher"))
29+
host.os.windows && plugins.push(require("@chiragrupani/karma-chromium-edge-launcher"))
30+
plugins.push(require("karma-coverage-istanbul-reporter"))
31+
32+
const config = buildConfig({
33+
sourceDir: "lib",
34+
fixtures: "test/fixtures/**/*.js",
35+
browsers: {
36+
chrome: true,
37+
firefox: true,
38+
safari: host.os.mac,
39+
edge: host.os.windows,
40+
ie: false
41+
},
42+
config: {
43+
browsers: browsers,
44+
plugins: plugins
45+
}
46+
});
47+
48+
if (config.logLevel !== karma.LOG_DISABLE) {
49+
console.debug("Karma Config:\n", nodeUtil.inspect(config, {
50+
depth: 10,
51+
colors: true,
52+
compact: false,
53+
}));
54+
}
55+
56+
karma.set(config);
57+
};
58+
59+
function isCI() {
60+
let CI = environmentFlag("CI");
61+
let karmaCI = environmentFlag("KARMA_CI");
62+
63+
return Boolean(CI || karmaCI || host.ci);
64+
};
65+
66+
function environmentFlag(name) {
67+
let value = environmentVariable(name);
68+
return !["", "false", "off", "no"].includes(value);
69+
}
70+
71+
function environmentVariable(name) {
72+
return (process.env[name] || "").trim().toLowerCase();
73+
}

lib/refs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { ono } = require("@jsdevtools/ono");
44
const $Ref = require("./ref");
55
const url = require("./util/url");
66

7-
const isWindows = /^win/.test(globalThis.process?.platform);
7+
const isWindows = /^win/.test(globalThis.process ? globalThis.process.platform : undefined);
88
const getPathFromOs = filePath => isWindows ? filePath.replace(/\\/g, "/") : filePath;
99

1010
module.exports = $Refs;

lib/resolvers/http.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ module.exports = {
6868
* @param {object} file - An object containing information about the referenced file
6969
* @param {string} file.url - The full URL of the referenced file
7070
* @param {string} file.extension - The lowercased file extension (e.g. ".txt", ".html", etc.)
71-
* @returns {Promise<string>}
71+
* @returns {Promise<Buffer>}
7272
*/
7373
read (file) {
7474
let u = url.parse(file.url);
@@ -89,7 +89,7 @@ module.exports = {
8989
* @param {object} httpOptions - The `options.resolve.http` object
9090
* @param {number} [redirects] - The redirect URLs that have already been followed
9191
*
92-
* @returns {Promise<string>}
92+
* @returns {Promise<Buffer>}
9393
* The promise resolves with the raw downloaded data, or rejects if there is an HTTP error.
9494
*/
9595
function download (u, httpOptions, redirects) {
@@ -99,25 +99,25 @@ function download (u, httpOptions, redirects) {
9999

100100
return get(u, httpOptions)
101101
.then((res) => {
102-
if (res.statusCode >= 400) {
103-
throw ono({ status: res.statusCode }, `HTTP ERROR ${res.statusCode}`);
102+
if (res.status >= 400) {
103+
throw ono({ status: res.statusCode }, `HTTP ERROR ${res.status}`);
104104
}
105-
else if (res.statusCode >= 300) {
105+
else if (res.status >= 300) {
106106
if (redirects.length > httpOptions.redirects) {
107-
throw new ResolverError(ono({ status: res.statusCode },
107+
throw new ResolverError(ono({ status: res.status },
108108
`Error downloading ${redirects[0]}. \nToo many redirects: \n ${redirects.join(" \n ")}`));
109109
}
110110
else if (!res.headers.location) {
111-
throw ono({ status: res.statusCode }, `HTTP ${res.statusCode} redirect with no location header`);
111+
throw ono({ status: res.status }, `HTTP ${res.status} redirect with no location header`);
112112
}
113113
else {
114-
// console.log('HTTP %d redirect %s -> %s', res.statusCode, u.href, res.headers.location);
114+
// console.log('HTTP %d redirect %s -> %s', res.status, u.href, res.headers.location);
115115
let redirectTo = url.resolve(u, res.headers.location);
116116
return download(redirectTo, httpOptions, redirects);
117117
}
118118
}
119119
else {
120-
return res.text();
120+
return res.body ? res.arrayBuffer().then(buf => Buffer.from(buf)) : Buffer.alloc(0);
121121
}
122122
})
123123
.catch((err) => {

lib/util/url.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const nodePath = require("path");
44
const projectDir = nodePath.resolve(__dirname, "..", "..");
55

6-
let isWindows = /^win/.test(globalThis.process?.platform),
6+
let isWindows = /^win/.test(globalThis.process ? globalThis.process.platform : undefined),
77
forwardSlashPattern = /\//g,
88
protocolPattern = /^(\w{2,}):\/\//i,
99
url = module.exports,

0 commit comments

Comments
 (0)