Skip to content

Commit

Permalink
Improve test error messages from flutter
Browse files Browse the repository at this point in the history
For some reason the error message from flutter SDK might contain no useful information. Basically it just says that test failed and you should see the logs. Logs itself are provided as content of `print` event. This commit adds special processing for this behavior - it parses actual error message out of print event.
  • Loading branch information
dorny committed Mar 31, 2021
1 parent 2c87efa commit ea36be4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
7 changes: 5 additions & 2 deletions __tests__/__snapshots__/dart-json.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,11 @@ The test description was:
pass updateShouldNotify
════════════════════════════════════════════════════════════════════════════════════════════════════",
"line": 112,
"message": "Test failed. See exception logs above.
The test description was: pass updateShouldNotify",
"message": "The following TestFailure object was thrown running a test:
Expected: <2>
Actual: <1>
Unexpected number of calls
",
"path": "test/value_listenable_provider_test.dart",
},
"name": "pass updateShouldNotify",
Expand Down
17 changes: 15 additions & 2 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

17 changes: 16 additions & 1 deletion src/parsers/dart-json/dart-json-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,14 @@ export class DartJsonParser implements TestParser {
}

const {trackedFiles} = this.options
const message = test.error?.error ?? ''
const stackTrace = test.error?.stackTrace ?? ''
const print = test.print
.filter(p => p.messageType === 'print')
.map(p => p.message)
.join('\n')
const details = [print, stackTrace].filter(str => str !== '').join('\n')
const src = this.exceptionThrowSource(details, trackedFiles)
const message = this.getErrorMessage(test.error?.error ?? '', print)
let path
let line

Expand All @@ -191,6 +191,21 @@ export class DartJsonParser implements TestParser {
}
}

private getErrorMessage(message: string, print: string): string {
if (this.sdk === 'flutter') {
const uselessMessageRe = /^Test failed\. See exception logs above\.\nThe test description was:/m
const flutterPrintRe = /^ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK +\s+(.*)\s+When the exception was thrown, this was the stack:/ms
if (uselessMessageRe.test(message)) {
const match = print.match(flutterPrintRe)
if (match !== null) {
return match[1]
}
}
}

return message || print
}

private exceptionThrowSource(ex: string, trackedFiles: string[]): {path: string; line: number} | undefined {
const lines = ex.split(/\r?\n/g)

Expand Down

0 comments on commit ea36be4

Please sign in to comment.