Skip to content

Commit

Permalink
Fix CI tests (#294)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jamesmoschou committed Jan 11, 2023
1 parent 95f5948 commit da9e32e
Show file tree
Hide file tree
Showing 12 changed files with 411 additions and 299 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CI-CD.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- macos-latest
- windows-latest
node:
- 14
- 17
- lts/*
- current

Expand Down
78 changes: 65 additions & 13 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,70 @@

"use strict";

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

module.exports = karmaConfig({
sourceDir: "lib",
fixtures: "test/fixtures/**/*.js",
browsers: {
chrome: host.ci ? host.os.linux : true,
firefox: host.ci ? host.os.linux : true,
// TODO these were disabled as unstable. should we remove saucelabs or what?
// safari: host.ci ? host.os.linux : host.os.mac, // SauceLabs in CI
edge: host.ci ? host.os.linux : host.os.windows, // SauceLabs in CI
// ie: host.ci ? host.os.windows : false, // IE needs to run by itself, due to Babel transforms
},
});
module.exports = (karma) => {

const browsers = [];
const CI = isCI();
browsers.push(CI ? "ChromeHeadless" : "Chrome");
browsers.push(CI ? "FirefoxHeadless" : "Firefox");
host.os.mac && browsers.push("Safari");
host.os.windows && browsers.push("Edge");

const plugins = [
require("@jsdevtools/karma-host-environment"),
require("karma-verbose-reporter"),
require("karma-mocha"),
require("karma-webpack"),
]
plugins.push(require("karma-chrome-launcher"))
plugins.push(require("karma-firefox-launcher"))
host.os.mac && plugins.push(require("karma-safari-launcher"))
host.os.windows && plugins.push(require("@chiragrupani/karma-chromium-edge-launcher"))
plugins.push(require("karma-coverage-istanbul-reporter"))

const config = buildConfig({
sourceDir: "lib",
fixtures: "test/fixtures/**/*.js",
browsers: {
chrome: true,
firefox: true,
safari: host.os.mac,
edge: host.os.windows,
ie: false
},
config: {
browsers: browsers,
plugins: plugins
}
});

if (config.logLevel !== karma.LOG_DISABLE) {
console.debug("Karma Config:\n", nodeUtil.inspect(config, {
depth: 10,
colors: true,
compact: false,
}));
}

karma.set(config);
};

function isCI() {
let CI = environmentFlag("CI");
let karmaCI = environmentFlag("KARMA_CI");

return Boolean(CI || karmaCI || host.ci);
};

function environmentFlag(name) {
let value = environmentVariable(name);
return !["", "false", "off", "no"].includes(value);
}

function environmentVariable(name) {
return (process.env[name] || "").trim().toLowerCase();
}
2 changes: 1 addition & 1 deletion lib/refs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { ono } = require("@jsdevtools/ono");
const $Ref = require("./ref");
const url = require("./util/url");

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

module.exports = $Refs;
Expand Down
18 changes: 9 additions & 9 deletions lib/resolvers/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ module.exports = {
* @param {object} file - An object containing information about the referenced file
* @param {string} file.url - The full URL of the referenced file
* @param {string} file.extension - The lowercased file extension (e.g. ".txt", ".html", etc.)
* @returns {Promise<string>}
* @returns {Promise<Buffer>}
*/
read (file) {
let u = url.parse(file.url);
Expand All @@ -89,7 +89,7 @@ module.exports = {
* @param {object} httpOptions - The `options.resolve.http` object
* @param {number} [redirects] - The redirect URLs that have already been followed
*
* @returns {Promise<string>}
* @returns {Promise<Buffer>}
* The promise resolves with the raw downloaded data, or rejects if there is an HTTP error.
*/
function download (u, httpOptions, redirects) {
Expand All @@ -99,25 +99,25 @@ function download (u, httpOptions, redirects) {

return get(u, httpOptions)
.then((res) => {
if (res.statusCode >= 400) {
throw ono({ status: res.statusCode }, `HTTP ERROR ${res.statusCode}`);
if (res.status >= 400) {
throw ono({ status: res.statusCode }, `HTTP ERROR ${res.status}`);
}
else if (res.statusCode >= 300) {
else if (res.status >= 300) {
if (redirects.length > httpOptions.redirects) {
throw new ResolverError(ono({ status: res.statusCode },
throw new ResolverError(ono({ status: res.status },
`Error downloading ${redirects[0]}. \nToo many redirects: \n ${redirects.join(" \n ")}`));
}
else if (!res.headers.location) {
throw ono({ status: res.statusCode }, `HTTP ${res.statusCode} redirect with no location header`);
throw ono({ status: res.status }, `HTTP ${res.status} redirect with no location header`);
}
else {
// console.log('HTTP %d redirect %s -> %s', res.statusCode, u.href, res.headers.location);
// console.log('HTTP %d redirect %s -> %s', res.status, u.href, res.headers.location);
let redirectTo = url.resolve(u, res.headers.location);
return download(redirectTo, httpOptions, redirects);
}
}
else {
return res.text();
return res.body ? res.arrayBuffer().then(buf => Buffer.from(buf)) : Buffer.alloc(0);
}
})
.catch((err) => {
Expand Down
2 changes: 1 addition & 1 deletion lib/util/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const nodePath = require("path");
const projectDir = nodePath.resolve(__dirname, "..", "..");

let isWindows = /^win/.test(globalThis.process?.platform),
let isWindows = /^win/.test(globalThis.process ? globalThis.process.platform : undefined),
forwardSlashPattern = /\//g,
protocolPattern = /^(\w{2,}):\/\//i,
url = module.exports,
Expand Down
Loading

0 comments on commit da9e32e

Please sign in to comment.