public icon indicating copy to clipboard operation
public copied to clipboard

How to use Jest and Electron?

Open NodeGuy opened this issue 6 years ago • 17 comments

Issue description or question

When I try to run my Jest tests in Electron I see the following error message:

​​[Error] Failed to load configuration file:  Test framework [jest] is not supported​​
​​​​​[Info]​​​​​ Started Wallaby.js Core v1.0.568

Wallaby.js configuration file

module.exports = wallaby => {
  return {
    files: ["build/lib/**/*.js"],
    tests: ["build/test/**/*.js"],
    env: {
      kind: 'electron'
    },
    testFramework: "jest",
  };
};

Code editor or IDE name and version

Visual Studio Code v1.21.1

OS name and version

macOS 10.13.3

NodeGuy avatar Mar 19 '18 20:03 NodeGuy

Jest runs in node, not sure if it can run in Electron - I haven't seen anyone using Jest with Electron (even without wallaby). Even if plain Jest works with Electron, we will need to add the support separately.

ArtemGovorov avatar Mar 19 '18 23:03 ArtemGovorov

We're using Jest with Electron: https://github.com/cosmos/voyager

I look forward to you adding support.

NodeGuy avatar Mar 19 '18 23:03 NodeGuy

@NodeGuy Thanks for the repo link!

ArtemGovorov avatar Mar 20 '18 00:03 ArtemGovorov

Correct me me if I'm wrong, but I can't see where you are running Jest with Electron, from what I can see, your package.json is running Jest tests in node:

"test:unit": "cross-env LOGGING=false MOCK=false ANALYTICS=false NODE_ENV=testing jest --maxWorkers=2",

To do the same in wallaby you may use

env: {
  type: 'node'
}

instead of

env: {
  kind: 'electron'
},

ArtemGovorov avatar Mar 20 '18 00:03 ArtemGovorov

Oh dear, how embarrassing. I'm new to the project and to Jest and made an incorrect assumption; thanks for pointing it out.

NodeGuy avatar Mar 20 '18 21:03 NodeGuy

@ArtemGovorov according to the Wallaby Jest is not supported to run in Node. Here it sounds, it is supported. What is the current status? Thanks!

marcelh-gh avatar Jul 03 '18 21:07 marcelh-gh

Jest is not supported to run in Node

Jest is supported to run in Node in Wallaby. You may find the sample config in our docs.

ArtemGovorov avatar Jul 03 '18 23:07 ArtemGovorov

@ArtemGovorov Thanks. You might want to update the info in https://wallabyjs.com/docs/integration/overview.html Under Supported testing frameworks Jest is only listed under "for browser" and not under "for Node"

marcelh-gh avatar Jul 04 '18 07:07 marcelh-gh

@marcelh-gh Thanks for the note. The docs had been updated.

ArtemGovorov avatar Jul 04 '18 09:07 ArtemGovorov

There is an option in Electron to run as a normal Node process by setting the ENV variable ELECTRON_RUN_AS_NODE=true. This is how we're using Jest with Electron. The command that we use to run our tests is ELECTRON_RUN_AS_NODE=true ./node_modules/.bin/electron ./node_modules/.bin/jest Perhaps this could be leveraged to run Wallaby on Electron apps with Jest? https://electronjs.org/docs/api/environment-variables#electron_run_as_node

TreTuna avatar Oct 17 '18 04:10 TreTuna

@TreTuna I haven't tried, but if Electron starts as node with the flag, then maybe this could work:

module.exports = function () {
  return {
    ...

    env: {
      type: 'node',
      runner: require('electron'), // this should return electron binary path
      params: {
        env: 'ELECTRON_RUN_AS_NODE=true'
      }
    }
  };
};

ArtemGovorov avatar Oct 17 '18 04:10 ArtemGovorov

@ArtemGovorov Well, that seems to work! I don't have any tests written in this repo yet that involve Electron processes, but it's running the very basic ones I have going on this. However, it does load up 6 Electron processes in the process. Which seems a bit excessive.

image

TreTuna avatar Oct 17 '18 05:10 TreTuna

@TreTuna Awesome, thanks for the update.

However, it does load up 6 Electron processes in the process. Which seems a bit excessive.

Wallaby runs tests in parallel using multiple worker processes. You may control the number of workers with the workers setting.

ArtemGovorov avatar Oct 17 '18 07:10 ArtemGovorov

Jest runs in node, not sure if it can run in Electron - I haven't seen anyone using Jest with Electron (even without wallaby). Even if plain Jest works with Electron, we will need to add the support separately.

I'm using jest with electron.

NetizenAbel avatar Feb 28 '20 21:02 NetizenAbel

@kashaiahyah85 - were you able to get this working with @ArtemGovorov's previous suggested fix mentioned in this issue?

smcenlly avatar Mar 01 '20 22:03 smcenlly

Actually, @smcenlly , I'm just using facebook-atom/jest-electron-runner more or less according to its documentation, and selectively mocking things when necessary. You'll need two different jest configurations (projects), one for main and one for the renderer. I go the route of trying the tests all in main, then separating what I need to, because the main process is a little faster on average for testing.

This is how I've configured that.

NetizenAbel avatar Mar 03 '20 22:03 NetizenAbel

Hey, I also use jest with electron, facebook-atom/jest-electron-runner works quite well, but I haven't been able to get wallaby to work with this, it would be very useful if this was supported.

samdesota avatar Dec 10 '20 11:12 samdesota