Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

console.Console global reference is undefined in the node environment #10282

Closed
twawszczak opened this issue Jul 17, 2020 · 4 comments 路 Fixed by #10502
Closed

console.Console global reference is undefined in the node environment #10282

twawszczak opened this issue Jul 17, 2020 · 4 comments 路 Fixed by #10502

Comments

@twawszczak
Copy link

馃悰 Bug Report

Regarding official documentation, in the nodeJS environment it is possible to create Console object with global console reference:

const { Console } = console;
const instance = new Console(..);

With jest, configured as a node instance, it doesn't work:

jest.config.js:

module.exports = {
  testEnvironment: 'node'
};

console.test.js:

test('console.Console should not be undefined in the node environment', () => {
    expect(console.Console).not.toBeUndefined();
});

output:

    expect(received).not.toBeUndefined()

    Received: undefined

      1 | test('console.Console should not be undefined in the node environment', () => {
    > 2 |     expect(console.Console).not.toBeUndefined();
        |                                 ^
      3 | });
      4 | 

      at Object.<anonymous> (console.test.js:2:33)

To Reproduce

Steps to reproduce the behavior: just check the simple demo repo linked below

Expected behavior

Global console.Console reference should be defined in the nodeJS environment.

Link to repl or repo (highly encouraged)

https://github.com/twawszczak/jest-console-issue

envinfo

  System:
    OS: macOS 10.15.4
    CPU: (8) x64 Intel(R) Core(TM) i5-8279U CPU @ 2.40GHz
  Binaries:
    Node: 12.16.3 - ~/.nvm/versions/node/v12.16.3/bin/node
    Yarn: 1.22.4 - ~/.nvm/versions/node/v12.16.3/bin/yarn
    npm: 6.14.4 - ~/.nvm/versions/node/v12.16.3/bin/npm
  npmPackages:
    jest: ^26.1.0 => 26.1.0 

@jeysal
Copy link
Contributor

jeysal commented Jul 17, 2020

Thanks for the issue, accepting PRs to add Console to our custom console object! One discussion point is probably whether we want to expose the actual Node.JS console class or the custom console class.

@Mark1626
Copy link
Contributor

Mark1626 commented Aug 17, 2020

@jeysal I'll pick it up

BufferedConsole:

Pretty straightforward, the following is valid

export default class BufferedConsole extends Console {

  constructor() { }

  static Console: NodeJS.ConsoleConstructor = BufferedConsole;

CustomConsole:

Not straight forward because of the base class. I thought of the following two ways

export default class CustomConsole extends Console {

  constructor(
    stdout: NodeJS.WriteStream,
    stderr: NodeJS.WriteStream,
    formatBuffer: Formatter) {
  }

  CustomConsole: CustomConsoleContructor = CustomConsole;
  // ...
}
export default class CustomConsole extends Console {

  constructor(
    stdout: NodeJS.WriteStream,
    stderr: NodeJS.WriteStream,
    formatBuffer: Formatter) {
  }

  Console: NodeJS.ConsoleContructor = Console;
  //...
}

Maybe this may be an approach, if the class does not extend Console.
I created an instance of console to use in _log and _warn.
Not sure how viable this is, exploring it as a possibility

export default class CustomConsole {
  private _console: Console;

  constructor(
    stdout: NodeJS.WriteStream,
    stderr: NodeJS.WriteStream,
    formatBuffer: Formatter = (_type: LogType, message: string): string =>
      message,
  ) {
    this._console = new Console(stdout, stderr);
  }

  Console: CustomConsoleContructor = CustomConsole;

private _log(type: LogType, message: string) {
    clearLine(this._stdout);
    this._console.log(
      this._formatBuffer(type, '  '.repeat(this._groupDepth) + message),
    );
  }

  private _logError(type: LogType, message: string) {
    clearLine(this._stderr);
    this._console.error(
      this._formatBuffer(type, '  '.repeat(this._groupDepth) + message),
    );
  }

Any thoughts? I'll get started on a PR when we finished discussion

@jeysal
Copy link
Contributor

jeysal commented Aug 21, 2020

Not sure I understand how it's more difficult for CustomConsole than BufferedConsole 'because of the base class', they both have Console as their base class right?
Either way, in case we decide to expose the 'standard' console, static Console = OriginalConsole; would suffice and not cause any further complications, right?
I think this decision would be a valid choice, since afaik creating Console instances is mostly done if you want to direct to a stream other than stdout/stderr, and in that case people might expect it to print 'normal' console messages rather than Jest-formatted ones. wdyt @SimenB?

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants