Is it possible to conceal the specifics of failure in the output of mocha?

Occasionally, when executing a series of mocha tests, I am not interested in the specifics of failures; I simply require a list of tests indicating pass or fail. Despite trying various reporters, they all tend to provide detailed information on failures. I appreciate the structure of the default spec reporter, but I have not been able to find a way to hide the details.

For instance, consider the following tests:

const assert = require('assert')
describe('test test', function() {
  it('should pass', function() {

  })
  it('should fail', function() {
    assert(false)
  })
})

This code snippet generates output like this:

  test test
    ✓ should pass
    1) should fail


  1 passing (9ms)
  1 failing

  1) test test
       should fail:
      AssertionError [ERR_ASSERTION]: false == true
      + expected - actual

      -false
      +true

      at Context.<anonymous> (test-solution.js:69:5)

What I actually desire is simpler:

  test test
    ✓ should pass
    1) should fail

  1 passing (9ms)
  1 failing

Is there something obvious that I am overlooking, or are these details something that cannot be suppressed?

Answer №1

It's frustrating when default reporters don't meet our needs, causing unnecessary time wasted on irrelevant information. Customizing the output should be a simple task.

The solution to this issue lies in creating a custom reporter tailored to your requirements. If that seems daunting, here's a quick and easy alternative: Disable the reporter and log events instead.

const Mocha = require('mocha');
let file = './devtest.js';
let passCount = 0;
let errors = [];

// Initiate Mocha with disabled reporter
const mocha = new Mocha({ reporter: function () {} });
mocha.addFile(file);
console.log('\n===== start mocha file ' + file);

mocha.run()
   .on('pass', function (test) {
      passCount++;
      logSuccess(test.title);
   })
   .on('fail', function (test, err) {
      errors.push({test, err});
      logError(test.title);
   })
   .on('end', function () {
      console.log();
      console.log('   -------------------------');
      logSuccess(passCount + ' tests passed');
      logError(errors.length + ' tests failed');
      // Possible actions here - e.g., callback(errors)
   });

function logSuccess (str) {
   console.log('\u001b[32m  ✓ \u001b[0m\u001b[90m' + str + '\u001b[0m');
}
function logError (str) {
   console.log('\u001b[31m  ✖ ' + str + '\u001b[0m');
}

https://i.sstatic.net/byw5k.png

While this method may lack some features present in standard reporters, it offers simplicity and speed. Extending functionality is straightforward since you have access to all errors and data.

If anyone can share a basic example of a custom reporter that works well, feel free to do so. Personally, I encountered issues with a custom reporter affecting my console output, leading me to prefer a simpler approach.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ways to change the default color selected by the user in an HTML color input

I'm currently working on a web application to manage a group of LEDs, but I'm encountering some issues when trying to update the color displayed in the HTML color input. It's crucial for me to provide users with real-time feedback on the cur ...

Styling the background position in CSS with the :target pseudo-class

After conducting some research, I was unable to find a satisfactory answer to my question. Essentially, I am attempting to shrink my website's header when a button is clicked. Here is the CSS code: I have been experimenting with making the backgrou ...

Implementing Knockout.js with JqueryUI Autocomplete: Access the complete object instead of just the value

I have implemented a custom binding for a JQueryUI auto complete feature that works well. However, I am looking to modify it so that it returns the Item object, which can then be pushed to another array. Can someone provide guidance on how to achieve this ...

How can a chat application create a unique identification for a specific chat room or conversation?

Imagine I have two users, "Sophie" & "Emma". Sophie decides to initiate a chat with Emma by clicking the chat button. A chat box pops up where Sophie can type her message and send it directly to Emma. To create a unique chat ID, I combine the names of ...

Storing user and message data with LocalStorage technology

Seeking advice on a straightforward approach to storing user data and messages. My idea is to use unique key values, such as random tokens (Ynjk_nkjSNKJN) for users, and real ids (1,2,3) for messages. Has anyone encountered this issue before? The goal is ...

What could be causing this addEventListener to fail when I am assigning elements to a class?

I've encountered an issue with my code where I have two text inputs and two date inputs. I tried to select all of them using QuerySelectorAll with a class, added a click listener that should change the textContent of a div element to "", but it's ...

Troubleshooting issue with Typescript compilation to UMD module

I am facing an issue with my files a.ts and b.ts. Here is the code for a.ts: function abc(){ alert("abc()") } export {abc} And here is the code for b.ts: import * as a from "./a" a.abc(); After compiling it using the following t ...

Placing a div over a JavaScript element

Is it feasible to overlay a div(1) with a background image on top of another div(2) that contains JavaScript (like Google maps API v3)? I have experimented with z-index without success, and I am unable to utilize absolute positioning because I depend on t ...

Avoiding the default action to submit AJAX form data won't result in any changes to the front end?

Currently, I am working with Flask and have utilized JavaScript to prevent default behavior in order to send all the necessary data through an AJAX request. However, I am facing an issue where although my view contains all the data (verified by console out ...

Comparing the Length of JavaScript Arrays

I have code that checks for similar values in two arrays, and if there are any, they are not displayed in the result. However, when I switch the lengths of the arrays so that Array2 is longer than Array1, I end up with an empty result array. How can I achi ...

css rules are not applied if the element is dynamically added using javascript

I'm encountering an issue with my code that inserts icons into a div with the ID of "editor". When I try to add a <select> element to the div with the ID of "drug_tool", the CSS styling rules for it are being ignored. How can I resolve this prob ...

Having difficulty getting my create-react-app to display on Heroku

I successfully managed to get my react-app running smoothly on my localhost server. However, when I attempted to deploy it on Heroku, I encountered a problem. Upon visiting the app address provided by Heroku, all I see is a blank page with none of the comp ...

Strategies for breaking apart a large, monolithic Node.js JavaScript application

My node.js server application is expanding, and I am looking to split it into multiple files. Below is a snippet of the current monolithic server.js file: var express = require('express'); var app = express(); // other initialization code etc / ...

Having issues with $emitting not working for parent-child components in Vue. Any ideas on what I might be doing incorrectly?

I have a login component that I need to call in the main vue component of App.vue. Within the login vue, when I click on any button, it should activate another vue component using Vue.js router to replace the login page. I have searched for solutions but h ...

Setting up the global configuration for Parsley.js Would you like to learn

I have been struggling to find helpful documentation, as it either seems outdated or I just can't seem to get it right. $.fn.parsley.defaults = {} // not working window.parsley.defaults = {} // not working window.ParsleyConfig = {} // not working My ...

Retrieving the HTML ID attribute of an anchor link within a form and sending it to the backend server

I am currently working on a project that involves a form containing an anchor link tag with a dynamic ID. I am utilizing this anchor link tag to submit the form via Javascript. However, I am facing difficulty in fetching the ID of the same anchor link tag, ...

What is the best way to remove empty elements from an Array?

Having an issue with my API post request. If no values are entered in the pricing form fields, I want to send an empty array. I attempted to use the filter method to achieve this but it still sends an array with an empty object (i.e. [{}]) when making the ...

Uploading multiple files simultaneously in React

I am facing an issue with my React app where I am trying to upload multiple images using the provided code. The problem arises when console.log(e) displays a Progress Event object with all its values, but my state remains at default values of null, 0, and ...

Creating a new dynamic page can be achieved by clicking on a dynamically generated link. Would you like to learn how to do that?

Recently, I developed a custom API using Node.js to retrieve information about blogs from Medium.com. The API currently provides: The author/main picture of the article Title A link to the article on medium.com (redundant) The entire article text in the ...

Output the keycode to the console repeatedly

I'm currently facing a minor mental obstacle: I have a javascript function embedded in html that displays the keycode when a key is pressed. It's connected to a function that provides detailed information about the character and keycode being pre ...