Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 /** | |
| 6 * @fileoverview A mocha adapter for BrowserTests. To use, include mocha.js and | |
| 7 * mocha_adapter.js in a WebUIBrowserTest's extraLibraries array. | |
| 8 */ | |
| 9 | |
| 10 /** | |
| 11 * Initializes a mocha reporter for the BrowserTest framework. | |
| 12 * @constructor | |
| 13 * @param {Runner} runner | |
|
James Hawkins
2015/06/01 20:18:17
nit: Please provide more details about what runner
michaelpg
2015/06/01 21:23:01
Done.
| |
| 14 */ | |
| 15 function BrowserTestReporter(runner) { | |
| 16 var passes = 0; | |
| 17 var failures = 0; | |
| 18 | |
| 19 // Increment passes for each passed test. | |
| 20 runner.on('pass', function(test) { | |
| 21 passes++; | |
| 22 }); | |
| 23 | |
| 24 // Report failures. Mocha only catches "assert" failures, because "expect" | |
| 25 // failures are caught by test_api.js. | |
| 26 runner.on('fail', function(test, err) { | |
| 27 failures++; | |
| 28 var message = 'Mocha test failed: ' + test.fullTitle() + '\n'; | |
| 29 | |
| 30 // We don't need tracing past the first line pointing to mocha.js. | |
| 31 var stack = err.stack.split('\n'); | |
| 32 for (var i = 0; i < stack.length; i++) { | |
| 33 if (stack[i].indexOf('(mocha.js:') != -1) | |
| 34 break; | |
| 35 } | |
| 36 // Include the stack up to the first function from mocha.js. | |
| 37 if (i < stack.length) | |
| 38 message += stack.slice(0, i + 1).join('\n'); | |
| 39 else | |
| 40 message += err.stack; | |
| 41 | |
| 42 console.error(message); | |
| 43 }); | |
| 44 | |
| 45 // Report the results to the test API. | |
| 46 runner.on('end', function() { | |
| 47 if (failures == 0) { | |
| 48 testDone(); | |
| 49 return; | |
| 50 } | |
| 51 testDone([ | |
| 52 false, | |
| 53 'Test Errors: ' + failures + '/' + (passes + failures) + | |
| 54 ' tests had failed assertions.' | |
| 55 ]); | |
| 56 }); | |
| 57 } | |
| 58 | |
| 59 // Configure mocha. | |
| 60 mocha.setup({ | |
| 61 // Use TDD interface instead of BDD. | |
| 62 ui: 'tdd', | |
| 63 // Use custom reporter to interface with BrowserTests. | |
| 64 reporter: BrowserTestReporter, | |
| 65 }); | |
| OLD | NEW |