| 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, which registers |
| 12 * event listeners on the given Runner. |
| 13 * @constructor |
| 14 * @param {Runner} runner Runs the tests and provides hooks for test results |
| 15 * (see Runner.prototype in mocha.js). |
| 16 */ |
| 17 function BrowserTestReporter(runner) { |
| 18 var passes = 0; |
| 19 var failures = 0; |
| 20 |
| 21 // Increment passes for each passed test. |
| 22 runner.on('pass', function(test) { |
| 23 passes++; |
| 24 }); |
| 25 |
| 26 // Report failures. Mocha only catches "assert" failures, because "expect" |
| 27 // failures are caught by test_api.js. |
| 28 runner.on('fail', function(test, err) { |
| 29 failures++; |
| 30 var message = 'Mocha test failed: ' + test.fullTitle() + '\n'; |
| 31 |
| 32 // Remove unhelpful mocha lines from stack trace. |
| 33 var stack = err.stack.split('\n'); |
| 34 for (var i = 0; i < stack.length; i++) { |
| 35 if (stack[i].indexOf('mocha.js:') == -1) |
| 36 message += stack[i] + '\n'; |
| 37 } |
| 38 |
| 39 console.error(message); |
| 40 }); |
| 41 |
| 42 // Report the results to the test API. |
| 43 runner.on('end', function() { |
| 44 if (failures == 0) { |
| 45 testDone(); |
| 46 return; |
| 47 } |
| 48 testDone([ |
| 49 false, |
| 50 'Test Errors: ' + failures + '/' + (passes + failures) + |
| 51 ' tests had failed assertions.' |
| 52 ]); |
| 53 }); |
| 54 } |
| 55 |
| 56 // Configure mocha. |
| 57 mocha.setup({ |
| 58 // Use TDD interface instead of BDD. |
| 59 ui: 'tdd', |
| 60 // Use custom reporter to interface with BrowserTests. |
| 61 reporter: BrowserTestReporter, |
| 62 }); |
| OLD | NEW |