-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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 async stack traces for circus #6281
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,33 +43,42 @@ const handler: EventHandler = (event, state): void => { | |
} | ||
case 'add_hook': { | ||
const {currentDescribeBlock} = state; | ||
const {fn, hookType: type, timeout} = event; | ||
const {asyncError, fn, hookType: type, timeout} = event; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i was thinking that maybe we can name it somehow differently? not in this PR, but maybe something to change in the future There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, it's just so we can keep the stack across async boundaries There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Open to naming suggestions |
||
const parent = currentDescribeBlock; | ||
currentDescribeBlock.hooks.push({fn, parent, timeout, type}); | ||
currentDescribeBlock.hooks.push({asyncError, fn, parent, timeout, type}); | ||
break; | ||
} | ||
case 'add_test': { | ||
const {currentDescribeBlock} = state; | ||
const {fn, mode, testName: name, timeout} = event; | ||
const test = makeTest(fn, mode, name, currentDescribeBlock, timeout); | ||
test.mode === 'only' && (state.hasFocusedTests = true); | ||
const {asyncError, fn, mode, testName: name, timeout} = event; | ||
const test = makeTest( | ||
fn, | ||
mode, | ||
name, | ||
currentDescribeBlock, | ||
timeout, | ||
asyncError, | ||
); | ||
if (test.mode === 'only') { | ||
state.hasFocusedTests = true; | ||
} | ||
currentDescribeBlock.tests.push(test); | ||
break; | ||
} | ||
case 'hook_failure': { | ||
const {test, describeBlock, error, hook} = event; | ||
const {type} = hook; | ||
const {asyncError, type} = hook; | ||
|
||
if (type === 'beforeAll') { | ||
invariant(describeBlock, 'always present for `*All` hooks'); | ||
addErrorToEachTestUnderDescribe(describeBlock, error); | ||
addErrorToEachTestUnderDescribe(describeBlock, error, asyncError); | ||
} else if (type === 'afterAll') { | ||
// Attaching `afterAll` errors to each test makes execution flow | ||
// too complicated, so we'll consider them to be global. | ||
state.unhandledErrors.push(error); | ||
state.unhandledErrors.push([error, asyncError]); | ||
} else { | ||
invariant(test, 'always present for `*Each` hooks'); | ||
test.errors.push(error); | ||
test.errors.push([error, asyncError]); | ||
} | ||
break; | ||
} | ||
|
@@ -87,7 +96,11 @@ const handler: EventHandler = (event, state): void => { | |
break; | ||
} | ||
case 'test_fn_failure': { | ||
event.test.errors.push(event.error); | ||
const { | ||
error, | ||
test: {asyncError}, | ||
} = event; | ||
event.test.errors.push([error, asyncError]); | ||
break; | ||
} | ||
case 'run_start': { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,12 +7,13 @@ | |
* @flow strict-local | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should probably import the one in jest-jasmine rather than copy it... |
||
*/ | ||
|
||
import type {DiffOptions} from 'jest-diff/src/diff_strings.js'; | ||
import type {DiffOptions} from 'jest-diff/src/diff_strings'; | ||
import type {Event, State} from 'types/Circus'; | ||
|
||
import {printExpected, printReceived} from 'jest-matcher-utils'; | ||
import chalk from 'chalk'; | ||
import diff from 'jest-diff'; | ||
import prettyFormat from 'pretty-format'; | ||
|
||
type AssertionError = {| | ||
actual: ?string, | ||
|
@@ -34,31 +35,40 @@ const assertOperatorsMap = { | |
const humanReadableOperators = { | ||
deepEqual: 'to deeply equal', | ||
deepStrictEqual: 'to deeply and strictly equal', | ||
equal: 'to be equal', | ||
notDeepEqual: 'not to deeply equal', | ||
notDeepStrictEqual: 'not to deeply and strictly equal', | ||
notEqual: 'to not be equal', | ||
notStrictEqual: 'not be strictly equal', | ||
strictEqual: 'to strictly be equal', | ||
}; | ||
|
||
export default (event: Event, state: State) => { | ||
switch (event.name) { | ||
case 'test_done': { | ||
let assert; | ||
try { | ||
// Use indirect require so that Metro Bundler does not attempt to | ||
// bundle `assert`, which does not exist in React Native. | ||
// eslint-disable-next-line no-useless-call | ||
assert = require.call(null, 'assert'); | ||
event.test.errors = event.test.errors.map(error => { | ||
return error instanceof assert.AssertionError | ||
? assertionErrorMessage(error, {expand: state.expand}) | ||
: error; | ||
}); | ||
} catch (error) { | ||
// We are running somewhere where `assert` isn't available, like a | ||
// browser or React Native. Since assert isn't available, presumably | ||
// none of the errors we get through this event listener will be | ||
// `AssertionError`s, so we don't need to do anything. | ||
break; | ||
} | ||
event.test.errors = event.test.errors.map(errors => { | ||
let error; | ||
if (Array.isArray(errors)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this part is really hairy (even though I removed the react native hack). Ideas welcome! |
||
const [originalError, asyncError] = errors; | ||
|
||
if (originalError == null) { | ||
error = asyncError; | ||
} else if (!originalError.stack) { | ||
error = asyncError; | ||
|
||
error.message = originalError.message | ||
? originalError.message | ||
: `thrown: ${prettyFormat(originalError, {maxDepth: 3})}`; | ||
} else { | ||
error = originalError; | ||
} | ||
} else { | ||
error = errors; | ||
} | ||
return error.code === 'ERR_ASSERTION' | ||
? {message: assertionErrorMessage(error, {expand: state.expand})} | ||
: errors; | ||
}); | ||
} | ||
} | ||
}; | ||
|
@@ -76,12 +86,15 @@ const getOperatorName = (operator: ?string, stack: string) => { | |
return ''; | ||
}; | ||
|
||
const operatorMessage = (operator: ?string, negator: boolean) => | ||
typeof operator === 'string' | ||
? operator.startsWith('!') || operator.startsWith('=') | ||
? `${negator ? 'not ' : ''}to be (operator: ${operator}):\n` | ||
: `${humanReadableOperators[operator] || operator} to:\n` | ||
const operatorMessage = (operator: ?string) => { | ||
const niceOperatorName = getOperatorName(operator, ''); | ||
// $FlowFixMe: we default to the operator itself, so holes in the map doesn't matter | ||
const humanReadableOperator = humanReadableOperators[niceOperatorName]; | ||
|
||
return typeof operator === 'string' | ||
? `${humanReadableOperator || niceOperatorName} to:\n` | ||
: ''; | ||
}; | ||
|
||
const assertThrowingMatcherHint = (operatorName: string) => { | ||
return ( | ||
|
@@ -114,13 +127,13 @@ const assertMatcherHint = (operator: ?string, operatorName: string) => { | |
}; | ||
|
||
function assertionErrorMessage(error: AssertionError, options: DiffOptions) { | ||
const {expected, actual, message, operator, stack} = error; | ||
const {expected, actual, generatedMessage, message, operator, stack} = error; | ||
const diffString = diff(expected, actual, options); | ||
const negator = | ||
typeof operator === 'string' && | ||
(operator.startsWith('!') || operator.startsWith('not')); | ||
const hasCustomMessage = !error.generatedMessage; | ||
const hasCustomMessage = !generatedMessage; | ||
const operatorName = getOperatorName(operator, stack); | ||
const trimmedStack = stack | ||
.replace(message, '') | ||
.replace(/AssertionError(.*)/g, ''); | ||
|
||
if (operatorName === 'doesNotThrow') { | ||
return ( | ||
|
@@ -130,7 +143,7 @@ function assertionErrorMessage(error: AssertionError, options: DiffOptions) { | |
chalk.reset(`Instead, it threw:\n`) + | ||
` ${printReceived(actual)}` + | ||
chalk.reset(hasCustomMessage ? '\n\nMessage:\n ' + message : '') + | ||
stack.replace(/AssertionError(.*)/g, '') | ||
trimmedStack | ||
); | ||
} | ||
|
||
|
@@ -141,19 +154,19 @@ function assertionErrorMessage(error: AssertionError, options: DiffOptions) { | |
chalk.reset(`Expected the function to throw an error.\n`) + | ||
chalk.reset(`But it didn't throw anything.`) + | ||
chalk.reset(hasCustomMessage ? '\n\nMessage:\n ' + message : '') + | ||
stack.replace(/AssertionError(.*)/g, '') | ||
trimmedStack | ||
); | ||
} | ||
|
||
return ( | ||
assertMatcherHint(operator, operatorName) + | ||
'\n\n' + | ||
chalk.reset(`Expected value ${operatorMessage(operator, negator)}`) + | ||
chalk.reset(`Expected value ${operatorMessage(operator)}`) + | ||
` ${printExpected(expected)}\n` + | ||
chalk.reset(`Received:\n`) + | ||
` ${printReceived(actual)}` + | ||
chalk.reset(hasCustomMessage ? '\n\nMessage:\n ' + message : '') + | ||
(diffString ? `\n\nDifference:\n\n${diffString}` : '') + | ||
stack.replace(/AssertionError(.*)/g, '') | ||
trimmedStack | ||
); | ||
} |
This comment was marked as resolved.
Sorry, something went wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stderr.replace(/thrown|Failed/, 'REPLACED');
😛