blob: 52faffa2dd58435d7c53cacfe6c2af3307beddb9 [file]
// Copyright 2022 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import * as path from 'path';
import Mocha from 'mocha'; // not esm
import 'source-map-support/register'; // support source maps in the runner
import CIReporter from './reporter';
/**
* Gather the tests from the test root directories, and run them.
* @returns void promise.
*/
export function run(): Promise<void> {
// Create the mocha test
const mocha = new Mocha({
ui: 'tdd',
color: true,
// support source maps in the test file
require: ['source-map-support/register'],
});
if (process.env.TEST_XML_OUTPUT) {
// on CI, output XUnit so that we can read things in nicely
mocha.reporter(CIReporter, { output: process.env.TEST_XML_OUTPUT });
}
const testsRoot = path.resolve(__dirname, '..', '..', '..');
mocha.addFile(path.resolve(testsRoot, 'all-tests.js'));
return new Promise((c, e) => {
try {
// Run the mocha test
mocha.run((failures) => {
if (failures > 0) {
e(Error(`${failures} tests failed.`));
} else {
c();
}
});
} catch (err) {
console.error(err);
e(err instanceof Error ? err : new Error(String(err)));
}
});
}