Chromium Code Reviews| Index: chrome/test/data/webui/mocha_adapter.js |
| diff --git a/chrome/test/data/webui/mocha_adapter.js b/chrome/test/data/webui/mocha_adapter.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..cb99fb7c7015bcdd3bb6d4066da35d2c0e1fa789 |
| --- /dev/null |
| +++ b/chrome/test/data/webui/mocha_adapter.js |
| @@ -0,0 +1,62 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +/** |
| + * @fileoverview A mocha adapter for BrowserTests. To use, include mocha.js and |
| + * mocha_adapter.js in a WebUIBrowserTest's extraLibraries array. |
| + */ |
| + |
| +/** |
| + * Initializes a mocha reporter for the BrowserTest framework, which registers |
| + * event listeners on the given Runner. |
| + * @constructor |
| + * @param {Runner} runner Runs the tests and provides hooks for test results |
| + * (see Runner.prototype in mocha.js). |
| + */ |
| +function BrowserTestReporter(runner) { |
| + var passes = 0; |
| + var failures = 0; |
| + |
| + // Increment passes for each passed test. |
| + runner.on('pass', function(test) { |
| + passes++; |
| + }); |
| + |
| + // Report failures. Mocha only catches "assert" failures, because "expect" |
| + // failures are caught by test_api.js. |
| + runner.on('fail', function(test, err) { |
| + failures++; |
| + var message = 'Mocha test failed: ' + test.fullTitle() + '\n'; |
| + |
| + // Remove unhelpful mocha lines from stack trace. |
| + var stack = err.stack.split('\n'); |
| + for (var i = 0; i < stack.length; i++) { |
| + if (!stack[i].includes('mocha.js:')) |
|
Dan Beam
2015/06/02 20:44:33
nit: why includes() instead of .indexOf() != -1?
michaelpg
2015/06/02 21:30:25
It's just nicer, so why not?
Dan Beam
2015/06/02 21:32:27
JSC_INEXISTENT_PROPERTY: Property contains never d
michaelpg
2015/06/02 21:38:49
Acknowledged.
|
| + message += stack[i] + '\n'; |
|
michaelpg
2015/06/02 19:32:59
Changed because mocha.js lines can occur first in
|
| + } |
| + |
| + console.error(message); |
| + }); |
| + |
| + // Report the results to the test API. |
| + runner.on('end', function() { |
| + if (failures == 0) { |
| + testDone(); |
| + return; |
| + } |
| + testDone([ |
| + false, |
| + 'Test Errors: ' + failures + '/' + (passes + failures) + |
| + ' tests had failed assertions.' |
| + ]); |
| + }); |
| +} |
| + |
| +// Configure mocha. |
| +mocha.setup({ |
| + // Use TDD interface instead of BDD. |
| + ui: 'tdd', |
| + // Use custom reporter to interface with BrowserTests. |
| + reporter: BrowserTestReporter, |
| +}); |