UNPKG

3.66 kBJavaScriptView Raw
1"use strict";
2var _a, _b;
3Object.defineProperty(exports, "__esModule", { value: true });
4/*---------------------------------------------------------------------------------------------
5* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
6* See LICENSE.md in the project root for license terms and full copyright notice.
7*--------------------------------------------------------------------------------------------*/
8/* eslint-disable @typescript-eslint/naming-convention */
9/* eslint-disable @typescript-eslint/no-var-requires */
10/* eslint-disable no-console */
11const path = require("path");
12const fs = require("fs-extra");
13const { logBuildWarning, logBuildError, failBuild } = require("../scripts/rush/utils");
14const Base = require("mocha/lib/reporters/base");
15const Spec = require("mocha/lib/reporters/spec");
16const MochaJUnitReporter = require("mocha-junit-reporter");
17function withStdErr(callback) {
18 const originalConsoleLog = console.log;
19 console.log = console.error;
20 callback();
21 console.log = originalConsoleLog;
22}
23const isCI = process.env.CI || process.env.TF_BUILD;
24// Force rush test to fail CI builds if describe.only or it.only is used.
25// These should only be used for debugging and must not be committed, otherwise we may be accidentally skipping lots of tests.
26if (isCI) {
27 if (typeof (mocha) !== "undefined")
28 mocha.forbidOnly();
29 else
30 require.cache[require.resolve("mocha/lib/mocharc.json", { paths: (_b = (_a = require.main) === null || _a === void 0 ? void 0 : _a.paths) !== null && _b !== void 0 ? _b : module.paths })].exports.forbidOnly = true;
31}
32// This is necessary to enable colored output when running in rush test:
33Object.defineProperty(Base, "color", {
34 get: () => process.env.FORCE_COLOR !== "false" && process.env.FORCE_COLOR !== "0",
35 set: () => { },
36});
37class BentleyMochaReporter extends Spec {
38 constructor(_runner, _options) {
39 super(...arguments);
40 this._junitReporter = new MochaJUnitReporter(...arguments);
41 }
42 epilogue(...args) {
43 // Force test errors to be printed to stderr instead of stdout.
44 // This will allow rush to correctly summarize test failure when running rush test.
45 if (this.stats.failures) {
46 withStdErr(() => super.epilogue(...args));
47 }
48 else {
49 super.epilogue(...args);
50 if (0 === this.stats.passes) {
51 logBuildError("There were 0 passing tests. That doesn't seem right."
52 + "\nIf there are really no passing tests and no failures, then what was even the point?"
53 + "\nIt seems likely that tests were skipped by it.only, it.skip, or grep filters, so I'm going to fail now.");
54 failBuild();
55 }
56 }
57 if (!this.stats.pending)
58 return;
59 // Also log warnings in CI builds when tests have been skipped.
60 const currentPkgJson = path.join(process.cwd(), "package.json");
61 if (fs.existsSync(currentPkgJson)) {
62 const currentPackage = require(currentPkgJson).name;
63 if (this.stats.pending === 1)
64 logBuildWarning(`1 test skipped in ${currentPackage}`);
65 else
66 logBuildWarning(`${this.stats.pending} tests skipped in ${currentPackage}`);
67 }
68 else {
69 if (this.stats.pending === 1)
70 logBuildWarning(`1 test skipped`);
71 else
72 logBuildWarning(`${this.stats.pending} tests skipped`);
73 }
74 }
75}
76module.exports = BentleyMochaReporter;