wallabyjs / public

Repository for Wallaby.js questions and issues
http://wallabyjs.com
760 stars 45 forks source link

How to use Jest and Electron? #1567

Open NodeGuy opened 6 years ago

NodeGuy commented 6 years ago

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

ArtemGovorov commented 6 years ago

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.

NodeGuy commented 6 years ago

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

I look forward to you adding support.

ArtemGovorov commented 6 years ago

@NodeGuy Thanks for the repo link!

ArtemGovorov commented 6 years ago

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'
},
NodeGuy commented 6 years ago

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

marcelh-gh commented 6 years ago

@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!

ArtemGovorov commented 6 years ago

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.

marcelh-gh commented 6 years ago

@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"

ArtemGovorov commented 6 years ago

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

TreTuna commented 6 years ago

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

ArtemGovorov commented 6 years ago

@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'
      }
    }
  };
};
TreTuna commented 6 years ago

@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

ArtemGovorov commented 6 years ago

@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.

NetizenAbel commented 4 years ago

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.

smcenlly commented 4 years ago

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

NetizenAbel commented 4 years ago

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.

samdesota commented 3 years ago

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.