From 64ca79d84d1718638eba5db707be08b24c7109a2 Mon Sep 17 00:00:00 2001 From: Jonas Amundsen Date: Sun, 30 Apr 2017 23:26:55 +0200 Subject: [PATCH] Implement consoleRedirect: true This is a shortcut option to redirect jsdom's sandboxed console to node's console. --- index.js | 18 ++++++++++++++--- test/console-redirection-test.js | 33 ++++++++++++++++++++++++++++++++ test/test-helper/index.js | 21 ++++++++++++++++++++ 3 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 test/console-redirection-test.js diff --git a/index.js b/index.js index 8a717c1..e1f4874 100755 --- a/index.js +++ b/index.js @@ -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"] @@ -26,7 +38,7 @@ var jsdomBrowser = function (baseBrowserDecorator) { }; }; -jsdomBrowser.$inject = ["baseBrowserDecorator"]; +jsdomBrowser.$inject = ["baseBrowserDecorator", "config.jsdomLauncher"]; module.exports = { "launcher:jsdom": ["type", jsdomBrowser] diff --git a/test/console-redirection-test.js b/test/console-redirection-test.js new file mode 100644 index 0000000..6604d08 --- /dev/null +++ b/test/console-redirection-test.js @@ -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'"); + } + }); +}); diff --git a/test/test-helper/index.js b/test/test-helper/index.js index 5d0550e..9bf53a6 100644 --- a/test/test-helper/index.js +++ b/test/test-helper/index.js @@ -8,6 +8,8 @@ let { randomBytes } = require("crypto"); let { exec, spawn } = require("./child_process"); +let { Writable } = require("stream"); + function generateRandomFilePath () { return join( tmpdir(), @@ -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 };