From 0fc83b8fc4e33fa03b978caf8f36cc71f4e2aac5 Mon Sep 17 00:00:00 2001 From: Aaron Madsen Date: Mon, 19 Jun 2017 10:03:30 -0600 Subject: [PATCH 1/2] Add check to see if we really are in node even if document exists --- lib/utils.js | 2 +- test/is-node.js | 92 +++++++++++++++++++++++++++++++++ test/support/error.tap | 24 +++++++++ test/support/global-document.js | 1 + 4 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 test/is-node.js create mode 100644 test/support/error.tap create mode 100644 test/support/global-document.js diff --git a/lib/utils.js b/lib/utils.js index 556afc8..93c277b 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -643,7 +643,7 @@ exports.getError = function(err) { exports.stackTraceFilter = function() { var slash = '/' - , is = typeof document === 'undefined' + , is = (typeof document === 'undefined' || /(node|iojs)/.test(((process || {}).release || {}).name)) ? { node: true } : { browser: true } , cwd = is.node diff --git a/test/is-node.js b/test/is-node.js new file mode 100644 index 0000000..b067331 --- /dev/null +++ b/test/is-node.js @@ -0,0 +1,92 @@ +var tap = require('tap') +var fs = require('fs') +var path = require('path') +var spawn = require('child_process').spawn + +var test = tap.test + +test('Should successfully report an error', function(t){ + var testOut = '' + var expected = '\ntest/error.js\n\r 1) test/error.js\n\n 0 passing (169.786ms)\n 1 failing\n\n 1) test/error.js test/error.js:\n test/error.js\n \n\n' + + // Create a child process for tap-mocha-reporter + var cp = spawn( + process.argv0, + [ + require.resolve('../'), 'spec' + ], + { + stdio: ['pipe', 'pipe', 'pipe', 'ipc'], + env: { + // pretend to be a Continuous Integration server to disable colors + // in the output string for easier comparison / reporting + CI: 'testing' + } + } + ) + + // pipe TAP output containing an error + fs.createReadStream(path.join(__dirname, 'support', 'error.tap')) + .pipe(cp.stdin) + + cp.stderr + .on('data', function(data){ + console.error(data.toString()) + }) + + cp.stdout + .on('error', function(err){ + t.fail('Error while reading error.tap', err) + cp.kill() + }) + .on('data', function(data){ + testOut += data + }) + .on('end', function(){ + t.equal(testOut, expected, 'Reported the error as expected') + t.end() + }) +}) + +test('Should successfully report an error when running in node and global `document` is a mock', function(t){ + var testOut = '' + var expected = '\ntest/error.js\n\r 1) test/error.js\n\n 0 passing (169.786ms)\n 1 failing\n\n 1) test/error.js test/error.js:\n test/error.js\n \n\n' + + // Create a child process for tap-mocha-reporter + var cp = spawn( + process.argv0, + [ + '-r', require.resolve('./support/global-document.js'), + require.resolve('../'), 'spec' + ], + { + stdio: ['pipe', 'pipe', 'pipe', 'ipc'], + env: { + // pretend to be a Continuous Integration server to disable colors + // in the output string for easier comparison / reporting + CI: 'testing' + } + } + ) + + // pipe TAP output containing an error + fs.createReadStream(path.join(__dirname, 'support', 'error.tap')) + .pipe(cp.stdin) + + cp.stderr + .on('data', function(data){ + console.error(data.toString()) + }) + + cp.stdout + .on('error', function(err){ + console.error(err); + }) + .on('data', function(data){ + testOut += data + }) + .on('end', function(){ + t.equal(testOut, expected, 'Reported the error as expected') + t.end() + }) +}) \ No newline at end of file diff --git a/test/support/error.tap b/test/support/error.tap new file mode 100644 index 0000000..d040db9 --- /dev/null +++ b/test/support/error.tap @@ -0,0 +1,24 @@ +TAP version 13 + # Subtest: test/error.js + 1..0 +not ok 1 - test/error.js # time=155.087ms + --- + timeout: 30000 + file: test/error.js + results: + ok: false + count: 0 + pass: 0 + plan: + start: 1 + end: 0 + skipAll: true + exitCode: 1 + command: /Users/aaron.madsen/.node/bin/node + arguments: + - test/error.js + ... + +1..1 +# failed 1 of 1 tests +# time=169.786ms diff --git a/test/support/global-document.js b/test/support/global-document.js new file mode 100644 index 0000000..b0b4f42 --- /dev/null +++ b/test/support/global-document.js @@ -0,0 +1 @@ +global.document = {} From 33bb719bf55bb4d6e8f28f863c110c7448c5e6bb Mon Sep 17 00:00:00 2001 From: Aaron Madsen Date: Mon, 19 Jun 2017 19:38:39 -0600 Subject: [PATCH 2/2] Check if process is defined --- lib/utils.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/utils.js b/lib/utils.js index 93c277b..887f1fa 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -630,6 +630,22 @@ exports.getError = function(err) { return err || exports.undefinedError(); }; +/** + * Determine if we are running in a node-like environment (even when certain browser + * APIs have been mocked.) + */ +function runningInNode() { + if (typeof document === 'undefined') { + return true + } + + if (typeof process !== 'undefined') { + // NOTE: electron has 'node' in this location, so we won't go out of our way for it + return /(node|iojs)/.test(((process || {}).release || {}).name) + } + + return false +} /** * @summary @@ -643,7 +659,7 @@ exports.getError = function(err) { exports.stackTraceFilter = function() { var slash = '/' - , is = (typeof document === 'undefined' || /(node|iojs)/.test(((process || {}).release || {}).name)) + , is = runningInNode() ? { node: true } : { browser: true } , cwd = is.node