Skip to content

Commit

Permalink
mess with error lines
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Jul 16, 2019
1 parent 4fc6cbb commit af3e109
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
2 changes: 1 addition & 1 deletion e2e/__tests__/__snapshots__/failures.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ FAIL __tests__/duringTests.test.js
35 |
36 | test('done(non-error)', done => {
at Object.<anonymous>.done (__tests__/duringTests.test.js:33:8)
at Object.<anonymous> (__tests__/duringTests.test.js:33:8)
● done(non-error)
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-jasmine2/src/jasmineAsyncInstall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function promisifyLifeCycleFunction(
// didn't return a promise.
const asyncJestLifecycle = function(done: DoneFn) {
const wrappedFn = isGeneratorFn(fn) ? co.wrap(fn) : fn;
const returnValue = wrappedFn.call({}) as Promise<any>;
const returnValue = wrappedFn.call({});

if (isPromise(returnValue)) {
returnValue.then(done.bind(null, null), (error: Error) => {
Expand Down
10 changes: 3 additions & 7 deletions packages/jest-jasmine2/src/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,9 @@ export default class Jasmine2Reporter implements Reporter {
private _addMissingMessageToStack(stack: string, message?: string) {
// Some errors (e.g. Angular injection error) don't prepend error.message
// to stack, instead the first line of the stack is just plain 'Error'
const ERROR_REGEX = /^Error\s*\n/;
if (
stack &&
message &&
ERROR_REGEX.test(stack) &&
stack.indexOf(message) === -1
) {
const ERROR_REGEX = /^Error:?\s\n*/;

if (stack && message && !stack.includes(message)) {
return message + stack.replace(ERROR_REGEX, '\n');
}
return stack;
Expand Down
18 changes: 13 additions & 5 deletions packages/jest-message-util/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export const formatExecError = (
const separated = separateMessageFromStack(stack || '');
stack = separated.stack;

if (separated.message.indexOf(trim(message)) !== -1) {
if (separated.message.includes(trim(message))) {
// Often stack trace already contains the duplicate of the message
message = separated.message;
}
Expand Down Expand Up @@ -331,6 +331,14 @@ export const formatResultsErrors = (
.join('\n');
};

const removeBlankErrorLine = (str: string) =>
str
.split('\n')
// Lines saying just `Error:` are useless
.filter(line => !/^Error:?\s*/.test(line))
.join('\n')
.trim();

// jasmine and worker farm sometimes don't give us access to the actual
// Error object, so we have to regexp out the message from the stack string
// to format it.
Expand All @@ -344,13 +352,13 @@ export const separateMessageFromStack = (content: string) => {
// If the error is a plain "Error:" instead of a SyntaxError or TypeError we
// remove the prefix from the message because it is generally not useful.
const messageMatch = content.match(
/^(?:Error: )?([\s\S]*?(?=\n\s*at\s.*\:\d*\:\d*)|\s*.*)([\s\S]*)$/,
/^(?:Error: )?([\s\S]*?(?=\n\s*at\s.*:\d*:\d*)|\s*.*)([\s\S]*)$/,
);
if (!messageMatch) {
// For flow
// For typescript
throw new Error('If you hit this error, the regex above is buggy.');
}
const message = messageMatch[1];
const stack = messageMatch[2];
const message = removeBlankErrorLine(messageMatch[1]);
const stack = removeBlankErrorLine(messageMatch[2]);
return {message, stack};
};

0 comments on commit af3e109

Please sign in to comment.