Skip to content
This repository has been archived by the owner on Sep 1, 2021. It is now read-only.

renderer console not work #54

Open
superwf opened this issue Dec 9, 2019 · 4 comments
Open

renderer console not work #54

superwf opened this issue Dec 9, 2019 · 4 comments

Comments

@superwf
Copy link

superwf commented Dec 9, 2019

Thanks for this great runner first, it make to test web component with jest possible.
I am new to electron, may be it is something basic I don't know.
How to make the renderer process to output to terminal?
I tried process.stdout.write and remote.getGlobal().console, but all not work.

@aaronabramov
Copy link
Contributor

i'm pretty sure that was supposed to do it https://github.com/facebook-atom/jest-electron-runner/blob/master/packages/electron/src/browser_window_injected_code.js#L58

but seems like it doesn't. can you put a console.log around that code and see if it works? :)

@superwf
Copy link
Author

superwf commented Dec 11, 2019

const mainConsole = new Console(process.stdout, process.stderr);

I change this line to
const mainConsole = require('electron').remote.getGlobal('console')
mainConsole.log(111111)
It works, but the output just flash away and overwriten by the jest output.
mainConsole.log('\n\n', 111111, '\n\n')
Works better, the output will keep on screen.
But in test case, console.log still not work

@hustcc
Copy link

hustcc commented Dec 11, 2019

have a try with jest-electron.

@ethangclark
Copy link

@superwf @aaronabramov something is overwriting global.console

this addition to browser_window_injected_code.js worked for me (using electron 11.1.1, jest 26.6.3)

const patchConsole = () => {
  const mainConsole = new Console(process.stdout, process.stderr);
  const rendererConsole = global.console;
  const mergedConsole = {};
  Object.getOwnPropertyNames(rendererConsole)
    .filter(prop => typeof rendererConsole[prop] === 'function')
    .forEach(prop => {
      mergedConsole[prop] =
        typeof mainConsole[prop] === 'function'
          ? (...args) => {
              mainConsole[prop](...args);
              return rendererConsole[prop](...args);
            }
          : (...args) => rendererConsole[prop](...args);
    });
  delete global.console;
  global.console = mergedConsole;

  let _console = global.console
  Object.defineProperty(global, 'console', {
    get() {
      return _console
    },
    set(newConsole) {
      Object.entries(_console).forEach(([k, method]) => {
        const newMethod = newConsole[k]
        if (typeof method !== 'function' || typeof newMethod !== 'function') return
        newConsole[k] = (...args) => {
          method(...args)
          return newMethod(...args)
        }
      })
      _console = newConsole
    },
  })
};
patchConsole();

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants