123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- 'use strict';
- Object.defineProperty(exports, '__esModule', {
- value: true
- });
- exports.default = void 0;
- function path() {
- const data = _interopRequireWildcard(require('path'));
- path = function () {
- return data;
- };
- return data;
- }
- function util() {
- const data = _interopRequireWildcard(require('util'));
- util = function () {
- return data;
- };
- return data;
- }
- function _exit() {
- const data = _interopRequireDefault(require('exit'));
- _exit = function () {
- return data;
- };
- return data;
- }
- function _jestUtil() {
- const data = require('jest-util');
- _jestUtil = function () {
- return data;
- };
- return data;
- }
- var _BaseReporter = _interopRequireDefault(require('./BaseReporter'));
- function _interopRequireDefault(obj) {
- return obj && obj.__esModule ? obj : {default: obj};
- }
- function _getRequireWildcardCache(nodeInterop) {
- if (typeof WeakMap !== 'function') return null;
- var cacheBabelInterop = new WeakMap();
- var cacheNodeInterop = new WeakMap();
- return (_getRequireWildcardCache = function (nodeInterop) {
- return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
- })(nodeInterop);
- }
- function _interopRequireWildcard(obj, nodeInterop) {
- if (!nodeInterop && obj && obj.__esModule) {
- return obj;
- }
- if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
- return {default: obj};
- }
- var cache = _getRequireWildcardCache(nodeInterop);
- if (cache && cache.has(obj)) {
- return cache.get(obj);
- }
- var newObj = {};
- var hasPropertyDescriptor =
- Object.defineProperty && Object.getOwnPropertyDescriptor;
- for (var key in obj) {
- if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) {
- var desc = hasPropertyDescriptor
- ? Object.getOwnPropertyDescriptor(obj, key)
- : null;
- if (desc && (desc.get || desc.set)) {
- Object.defineProperty(newObj, key, desc);
- } else {
- newObj[key] = obj[key];
- }
- }
- }
- newObj.default = obj;
- if (cache) {
- cache.set(obj, newObj);
- }
- return newObj;
- }
- function _defineProperty(obj, key, value) {
- if (key in obj) {
- Object.defineProperty(obj, key, {
- value: value,
- enumerable: true,
- configurable: true,
- writable: true
- });
- } else {
- obj[key] = value;
- }
- return obj;
- }
- const isDarwin = process.platform === 'darwin';
- const icon = path().resolve(__dirname, '../assets/jest_logo.png');
- class NotifyReporter extends _BaseReporter.default {
- constructor(globalConfig, startRun, context) {
- super();
- _defineProperty(this, '_notifier', loadNotifier());
- _defineProperty(this, '_startRun', void 0);
- _defineProperty(this, '_globalConfig', void 0);
- _defineProperty(this, '_context', void 0);
- this._globalConfig = globalConfig;
- this._startRun = startRun;
- this._context = context;
- }
- onRunComplete(contexts, result) {
- const success =
- result.numFailedTests === 0 && result.numRuntimeErrorTestSuites === 0;
- const firstContext = contexts.values().next();
- const hasteFS =
- firstContext && firstContext.value && firstContext.value.hasteFS;
- let packageName;
- if (hasteFS != null) {
- // assuming root package.json is the first one
- const [filePath] = hasteFS.matchFiles('package.json');
- packageName =
- filePath != null
- ? hasteFS.getModuleName(filePath)
- : this._globalConfig.rootDir;
- } else {
- packageName = this._globalConfig.rootDir;
- }
- packageName = packageName != null ? `${packageName} - ` : '';
- const notifyMode = this._globalConfig.notifyMode;
- const statusChanged =
- this._context.previousSuccess !== success || this._context.firstRun;
- const testsHaveRun = result.numTotalTests !== 0;
- if (
- testsHaveRun &&
- success &&
- (notifyMode === 'always' ||
- notifyMode === 'success' ||
- notifyMode === 'success-change' ||
- (notifyMode === 'change' && statusChanged) ||
- (notifyMode === 'failure-change' && statusChanged))
- ) {
- const title = util().format('%s%d%% Passed', packageName, 100);
- const message = `${isDarwin ? '\u2705 ' : ''}${(0, _jestUtil().pluralize)(
- 'test',
- result.numPassedTests
- )} passed`;
- this._notifier.notify({
- icon,
- message,
- timeout: false,
- title
- });
- } else if (
- testsHaveRun &&
- !success &&
- (notifyMode === 'always' ||
- notifyMode === 'failure' ||
- notifyMode === 'failure-change' ||
- (notifyMode === 'change' && statusChanged) ||
- (notifyMode === 'success-change' && statusChanged))
- ) {
- const failed = result.numFailedTests / result.numTotalTests;
- const title = util().format(
- '%s%d%% Failed',
- packageName,
- Math.ceil(Number.isNaN(failed) ? 0 : failed * 100)
- );
- const message = util().format(
- (isDarwin ? '\u26D4\uFE0F ' : '') + '%d of %d tests failed',
- result.numFailedTests,
- result.numTotalTests
- );
- const watchMode = this._globalConfig.watch || this._globalConfig.watchAll;
- const restartAnswer = 'Run again';
- const quitAnswer = 'Exit tests';
- if (!watchMode) {
- this._notifier.notify({
- icon,
- message,
- timeout: false,
- title
- });
- } else {
- this._notifier.notify(
- {
- actions: [restartAnswer, quitAnswer],
- closeLabel: 'Close',
- icon,
- message,
- timeout: false,
- title
- },
- (err, _, metadata) => {
- if (err || !metadata) {
- return;
- }
- if (metadata.activationValue === quitAnswer) {
- (0, _exit().default)(0);
- return;
- }
- if (metadata.activationValue === restartAnswer) {
- this._startRun(this._globalConfig);
- }
- }
- );
- }
- }
- this._context.previousSuccess = success;
- this._context.firstRun = false;
- }
- }
- exports.default = NotifyReporter;
- _defineProperty(NotifyReporter, 'filename', __filename);
- function loadNotifier() {
- try {
- return require('node-notifier');
- } catch (err) {
- if (err.code !== 'MODULE_NOT_FOUND') {
- throw err;
- }
- throw Error(
- 'notify reporter requires optional peer dependency "node-notifier" but it was not found'
- );
- }
- }
|