facebook-atom / jest-electron-runner

custom test runner for Jest that allows tests to be run in Electron environment
MIT License
189 stars 33 forks source link

Make it possible to show: true electron #16

Closed neojski closed 5 years ago

neojski commented 5 years ago

I have tests that use advanced browser API (svg) so I find jest-electron-runner essential to run my tests. However, I would like to be able to occasionally inspect how the results look like.

I actually have two problems/questions:

  1. I would like to add a configuration option to show: true the electron window on demand -- would you guys be fine if I added a cosmiconfig that would make it possible?
  2. I can't make https://github.com/americanexpress/jest-image-snapshot work -- in particular, I can't figure out how to create screenshots. I suppose that electron's capturePage doesn't work when the window is show: false but I haven't verified this theory yet. Did you guys have any experience with it?

Thanks!

marotili commented 5 years ago

Hi @neojski I managed to do what you want to do, so maybe I can help:

  1. You can execute remote.getCurrentWindow().show() in your test to open the electron window. I use that for debugging. I think it is possible to take screenshot even if the window is not shown.

  2. I render a react component using enzyme and then to html using enzymes render function. I attach the rendered html to the document body. After that I wait a little while for the rendering to complete, since I don't know yet how to check if it finished rendering.

The capturePage call looks like this:

remote.getCurrentWebContents().capturePage(image => {
      expect(image.toPNG()).toMatchImageSnapshot(})
    })

Maybe you can show us what how your test setup looks like?

Best regards

neojski commented 5 years ago

Hi @mfpi, thanks for your reply. So, remote.getCurrentWindow().show()is great for debugging. I've also learned that the getCurrentWebContests().capturePage only works under two conditions:

  1. I run show() first
  2. I wait some time after running show() (like a second).

Do you experience similar issues?

neojski commented 5 years ago

Oh, I was wrong. It's only the number 2. that's important and not the show() itself. And, I think, it's not even electon's fault but it's my rendering using react that's not synchronous. I think this solves all of my problems.

neojski commented 5 years ago

I don't know yet how to check if it finished rendering

I've been fighting with this recently and the best bet I have so far is to insert some magical pixel and poll to see if it's rendered. If you figure out how to do it better please let me know :-)