@jsdevtools/karma-host-environment
Version:
Access host info (OS, browser version, environment variables) in browser tests
39 lines • 1.43 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.karmaPlugin = void 0;
const fs = require("fs");
const temp = require("temp");
const host_to_pojo_1 = require("./host-to-pojo");
// Dependency injection for Karma
karmaPlugin.$inject = ["config.files"];
/**
* Configures Karma to serve a JavaScript file that exposes the `host` global variable.
*/
function karmaPlugin(files) {
// Create a copy of the `host` object that's safe to serialize as JSON
let hostPOJO = host_to_pojo_1.hostToPOJO();
// Build a script that exposes a global `host` object in the browser.
// The host-environment library automatically checks for a global `host` object,
// calls its toJSON() method, and merges the results with itself.
let script = `
(function () {
window.host = {
karma: ${JSON.stringify(hostPOJO, undefined, 2)},
env: ${JSON.stringify(hostPOJO.env, undefined, 2)}
};
}());`;
// Save the JavaScript file as a temp file
temp.track();
let file = temp.openSync({ prefix: "karma-host-environment-", suffix: ".js" });
fs.writeSync(file.fd, script);
fs.closeSync(file.fd);
// Inject the JavaScript file into Karma's file list
files.unshift({
pattern: file.path,
included: true,
served: true,
watched: false,
});
}
exports.karmaPlugin = karmaPlugin;
//# sourceMappingURL=karma-plugin.js.map
;