Files
blockly/tests/jsunit/mocha_jsunit_test_runner.js
Neil Fraser 4e2f8e6e02 Use SPDX licences.
This is a followup to #3127.
At the time, SPDX licenses were pending approval by Google.
2020-02-11 13:27:20 -08:00

100 lines
2.5 KiB
JavaScript

/**
* @license
* Copyright 2019 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview A mocha test runner that mimics the behaviour of
* goog.testing.junit
* @author samelh@google.com (Sam El-Husseini)
*/
'use strict';
/**
* Setup mocha
*/
mocha.setup({
ui: 'tdd'
});
// Add mocha div
var mochaDiv = document.createElement('div');
mochaDiv.id = 'mocha';
document.body.appendChild(mochaDiv);
var mochaCss = document.createElement('link');
mochaCss.setAttribute('href', 'https://unpkg.com/mocha@5.2.0/mocha.css');
mochaCss.setAttribute('rel', 'stylesheet');
document.head.appendChild(mochaCss);
/**
* Begin by discovering all of the test methods, test methods are any
* function on the window object that begins with the prefix `test`
*/
var allMethods = Object.getOwnPropertyNames(window);
var allTests = [];
for (var i = 0, method; i < allMethods.length, method = allMethods[i]; i++) {
if (method.indexOf('test') === 0 && method !== 'test' &&
typeof window[method] === 'function') {
allTests.push(method);
}
}
/**
* Split test methods into various suites by grouping them based on the
* test name. Tests the begin with the same prefix are grouped together
* into a suite.
*/
var suites = {};
for (var i = 0, method; i < allTests.length, method = allTests[i]; i++) {
var testName = method.substr(5);
var underscore = testName.indexOf('_');
var suiteName = underscore > -1 ? testName.substr(0, underscore) : 'test';
if (!suites.hasOwnProperty(suiteName)) {
suites[suiteName] = [];
}
suites[suiteName].push(method);
}
/**
* Setup chai fail method
*/
function fail() {
chai.fail();
}
/**
* Wrap all unit tests into mocha test cases. Slot them into the different
* suite groups that we found.
*/
suite('jsunit tests', function() {
for (var i = 0, suiteKeys = Object.keys(suites), suiteName;
i < suiteKeys.length, suiteName = suiteKeys[i]; i++) {
suite(suiteName, function() {
for (var j = 0, tests = suites[suiteName], method;
j < tests.length, method = tests[j]; j++) {
test(method, function() {
window[this.test.title]();
});
}
});
}
});
/**
* Create a div for failure results, and run the mocha tests.
*/
var failureDiv = document.createElement('div');
failureDiv.id = 'failureCount';
failureDiv.style = 'display:none';
failureDiv.setAttribute('tests_failed', 'unset');
document.body.appendChild(failureDiv);
mocha.run(function(failures) {
failureDiv.setAttribute('tests_failed', failures);
});