JavaScript Remove logging the origin line in Jest

Jest has this feature to log the line that outputs to console methods.

In some cases, this can become annoying:

  console.log _modules/log.js:37
  ℹ login.0 screenshot start

  console.time _modules/init.js:409
  login.0.screenshot: 0.33ms

  console.time _modules/init.js:394
  0 | login.0: 0.524ms

  console.log _modules/log.js:37
  ℹ login.1 screenshot start

Any idea how I can turn it off?

Answer:1

Looking at the source code for Jest, it doesn't seem like there is a neat way to turn those messages off.

However, one possible solution could be to write your own Console. Here I have used Console.js from Jest as a starting ground, and then created SimpleConsole which does what you need (I have removed some terminal coloring features for simplicity, but you could just add them yourself).

Once added to your project, you can overwrite Jest's normal console with your own before running the tests:

const { SimpleConsole } = require('./SimpleConsole');
global.console = new SimpleConsole(process.stdout, process.stderr);

I have made a REPL that shows it in action.

The source code for SimpleConsole:

const path = require('path');
const assert = require('assert');
const {format} = require('util');
const {Console} = require('console');

function simpleFormatter() {
  const TITLE_INDENT = '    ';
  const CONSOLE_INDENT = TITLE_INDENT + '  ';

  return (type, message) => {
    message = message
      .split(/\n/)
      .map(line => CONSOLE_INDENT + line)
      .join('\n');

    return (
      message +
      '\n'
    );
  };
};

class SimpleConsole extends Console {
  constructor(stdout, stderr, formatBuffer) {
    super(stdout, stderr);
    this._formatBuffer = formatBuffer || simpleFormatter();
    this._counters = {};
    this._timers = {};
    this._groupDepth = 0;
  }

  _logToParentConsole(message) {
    super.log(message);
  }

  _log(type, message) {
    if (process.stdout.isTTY) {
      this._stdout.write('\x1b[999D\x1b[K');
    }
    this._logToParentConsole(
      this._formatBuffer(type, '  '.repeat(this._groupDepth) + message),
    );
  }

  assert(...args) {
    try {
      assert(...args);
    } catch (error) {
      this._log('assert', error.toString());
    }
  }

  count(label = 'default') {
    if (!this._counters[label]) {
      this._counters[label] = 0;
    }

    this._log('count', format(`${label}: ${++this._counters[label]}`));
  }

  countReset(label = 'default') {
    this._counters[label] = 0;
  }

  debug(...args) {
    this._log('debug', format(...args));
  }

  dir(...args) {
    this._log('dir', format(...args));
  }

  dirxml(...args) {
    this._log('dirxml', format(...args));
  }

  error(...args) {
    this._log('error', format(...args));
  }

  group(...args) {
    this._groupDepth++;

    if (args.length > 0) {
      this._log('group', chalk.bold(format(...args)));
    }
  }

  groupCollapsed(...args) {
    this._groupDepth++;

    if (args.length > 0) {
      this._log('groupCollapsed', chalk.bold(format(...args)));
    }
  }

  groupEnd() {
    if (this._groupDepth > 0) {
      this._groupDepth--;
    }
  }

  info(...args) {
    this._log('info', format(...args));
  }

  log(...args) {
    this._log('log', format(...args));
  }

  time(label = 'default') {
    if (this._timers[label]) {
      return;
    }

    this._timers[label] = new Date();
  }

  timeEnd(label = 'default') {
    const startTime = this._timers[label];

    if (startTime) {
      const endTime = new Date();
      const time = endTime - startTime;
      this._log('time', format(`${label}: ${time}ms`));
      delete this._timers[label];
    }
  }

  warn(...args) {
    this._log('warn', format(...args));
  }

  getBuffer() {
    return null;
  }
}

module.exports.SimpleConsole = SimpleConsole;
Answer:2

How could I change selected option for dropdown list using javascript or jQuery? <select id ="someId" name="someName"> <option>One</option> <option>Two</...

How could I change selected option for dropdown list using javascript or jQuery? <select id ="someId" name="someName"> <option>One</option> <option>Two</...

  1. selected option using jquery
  2. selected option using javascript
  3. selected option using angularjs
  4. set selected option using jquery
  5. get selected option using jquery
  6. change selected option using jquery
  7. get selected option using javascript
  8. set selected option using javascript
  9. change selected option using javascript
  10. show selected option using jquery
  11. get selected option using js
  12. remove selected option using jquery
  13. find selected option using jquery
  14. selected option value using php
  15. selected option value using jquery
  16. make option selected using jquery
  17. jquery set selected option using value
  18. jquery set selected option using index
  19. jquery get selected option using id
  20. get selected option value using jquery

I have these two strings: "1-2" and "1--2". I would like to have a regex that would match only the first occurrence of the hyphen in both strings, such that the split would then be: [1,2] and [1,-2]. ...

I have these two strings: "1-2" and "1--2". I would like to have a regex that would match only the first occurrence of the hyphen in both strings, such that the split would then be: [1,2] and [1,-2]. ...

  1. regex matching only numbers
  2. regex matching only letters
  3. regex matches only once
  4. regex matches only
  5. regex only matching first
  6. regex only matching last
  7. regex only matching first character
  8. regex unique matches only
  9. grep regex only matching
  10. regex return only matching string
  11. perl regex only matching

I'm using Node.js and I would like to check if a file exists and return the result to a property of a object. I'm trying like this: var file_exists = function(file) { return fs.stat(file, function(...

I'm using Node.js and I would like to check if a file exists and return the result to a property of a object. I'm trying like this: var file_exists = function(file) { return fs.stat(file, function(...

  1. check existence file python
  2. check existence file bash
  3. check existence file matlab
  4. check existence file powershell
  5. check existence file c++
  6. check existence file
  7. check existence file php
  8. check existence file linux
  9. check file existence java
  10. check file existence in shell script
  11. check file existence in unix
  12. check file existence ansible
  13. check file existence in perl
  14. check file existence shell
  15. check file existence vba
  16. check file existence r
  17. check file existence javascript
  18. check file existence nodejs
  19. check file existence in makefile
  20. check file existence batch

I am trying to generate an ordered list, but the number before each list item is always 1. Part of the html: <ol ng-repeat="student in student.gpas | filter: studentName | orderBy: 'gpa'"> ...

I am trying to generate an ordered list, but the number before each list item is always 1. Part of the html: <ol ng-repeat="student in student.gpas | filter: studentName | orderBy: 'gpa'"> ...

  1. isn't
  2. https://www.instagram.com/
  3. isnetworld
  4. instagram
  5. isn't she lovely
  6. isnt it romantic
  7. isna
  8. isn't it ironic
  9. isner
  10. isnull sql
  11. isn network
  12. isnac 2020
  13. isnt she lovely chords
  14. isns
  15. isnan
  16. isn't it
  17. isnull
  18. isn't it romantic cast
  19. isna excel
  20. disney plus