Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check to see if we really are in node even if document exists #40

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will throw if process is not defined.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed. I have updated the pull request to use a helper function (rather than nest a ternary).
A simpler alternative would be to test for process the way the existing code tests for document and reverse the ternary - the assumption being that a browser environment is much less likely to mock the process object than a node environment is to mock document.

Also, I'll see if I can come up with a useful way to test this in a browser environment. Can you describe (or point to an example of) what that might look like?

? { node: true }
: { browser: true }
, cwd = is.node
Expand Down
92 changes: 92 additions & 0 deletions test/is-node.js
Original file line number Diff line number Diff line change
@@ -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()
})
})
24 changes: 24 additions & 0 deletions test/support/error.tap
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions test/support/global-document.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global.document = {}