Skip to content

Commit

Permalink
Implement consoleRedirect: true
Browse files Browse the repository at this point in the history
This is a shortcut option to redirect jsdom's sandboxed console to
node's console.
  • Loading branch information
badeball committed Apr 30, 2017
1 parent bf0137a commit 64ca79d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
18 changes: 15 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
var jsdom = require("jsdom");

var jsdomBrowser = function (baseBrowserDecorator) {
var jsdomBrowser = function (baseBrowserDecorator, config) {
baseBrowserDecorator(this);

this.name = "jsdom";

this._start = function (url) {
var virtualConsole = null;

if (jsdom.JSDOM) { // Indicate jsdom >= 10.0.0 and a new API
if (config.redirectConsole) {
virtualConsole = new jsdom.VirtualConsole().sendTo(console);
}

jsdom.JSDOM.fromURL(url, {
resources: "usable",
runScripts: "dangerously"
runScripts: "dangerously",
virtualConsole: virtualConsole
});
} else {
if (config.redirectConsole) {
virtualConsole = jsdom.createVirtualConsole().sendTo(console);
}

jsdom.env({
url: url,
virtualConsole: virtualConsole,
features : {
FetchExternalResources: ["script", "iframe"],
ProcessExternalResources: ["script"]
Expand All @@ -26,7 +38,7 @@ var jsdomBrowser = function (baseBrowserDecorator) {
};
};

jsdomBrowser.$inject = ["baseBrowserDecorator"];
jsdomBrowser.$inject = ["baseBrowserDecorator", "config.jsdomLauncher"];

module.exports = {
"launcher:jsdom": ["type", jsdomBrowser]
Expand Down
33 changes: 33 additions & 0 deletions test/console-redirection-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
let { createKarmaTest, waitToExit, WriteableBuffer } = require("./test-helper");

describe("with redirectConsole: true", function () {
it("should redirect console from jsdom to node", async function () {
let process = await createKarmaTest({ redirectConsole: true }, function () {
console.log("foo bar");
});

let writable = process.stdout.pipe(new WriteableBuffer());

await waitToExit(process);

if (writable.getContents().toString().indexOf("foo bar") === -1) {
throw new Error("Expected stdout to contain 'foo bar'");
}
});
});

describe("with redirectConsole: false (default)", function () {
it("should omit console output from jsdom", async function () {
let process = await createKarmaTest({}, function () {
console.log("foo bar");
});

let writable = process.stdout.pipe(new WriteableBuffer());

await waitToExit(process);

if (writable.getContents().toString().indexOf("foo bar") !== -1) {
throw new Error("Expected stdout to not contain 'foo bar'");
}
});
});
21 changes: 21 additions & 0 deletions test/test-helper/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ let { randomBytes } = require("crypto");

let { exec, spawn } = require("./child_process");

let { Writable } = require("stream");

function generateRandomFilePath () {
return join(
tmpdir(),
Expand Down Expand Up @@ -55,7 +57,26 @@ function waitToExit (process) {
});
}

class WriteableBuffer extends Writable {
constructor (options) {
super(options);

this.chunks = [];
}

_write (chunk, encoding, callback) {
this.chunks.push(chunk);

callback();
}

getContents () {
return Buffer.concat(this.chunks);
}
}

module.exports = {
createKarmaTest,
waitToExit,
WriteableBuffer
};

0 comments on commit 64ca79d

Please sign in to comment.