Hi. I have an application built on top of Electron, and I also have one native module as a dependency. I recompiled it to use the Electron’s V8 headers, but now I can’t run any specs, because Mocha expects node’s headers. I can switch to a different test framework if required, I know the Atom team uses Jasmine, but I haven’t found in their source code how to compile the modules for tests. Maybe there are no native modules in Atom. Do you guys know how to deal with that?
In https://github.com/fireball-x/fireball we have the same problem and I solve this by using Mocha in headless Electron. To do this, you need to spawn an Electron app (without calling BrowserWindow.show), then require the native module and run mocha with it. Every time we spawn a headless Electron, we will pass
--test <test-file> as argument to it, when Electron start up, it will parse the arguments and trigger the runTest function.
Here is the detail of the code we did it:
- https://github.com/fireball-x/editor-framework/blob/master/utils/run-tests.js (where you start your tests by normal node environment, the code will spawn the Electron afterward)
- https://github.com/fireball-x/editor-framework/blob/master/core/test-runner.js#L54 (the mocha you run in your Spawned Electron)
Here are some test cases we used under this method:
- https://github.com/fireball-x/fireball/blob/master/test/native-modules.js ( a very simple test to prove the native module can be required in the latest Electron )
I also create a test-framework to make it easy to test code in Electron’s environment and here is a documentation to explain this: https://github.com/fireball-x/editor-framework/blob/master/docs/editor/unit-test.md, the whole implementation of it can be found in:
Hope these things can help you figure out your own solution.
Running tests using mocha and requiring atom?
I ended up having the following init script for tests:
var Mocha = require(‘mocha’);
var testDir = ‘compiled/test/’;
var mocha = new Mocha();
Mocha.utils.lookupFiles(testDir, [‘js’], true).forEach(mocha.addFile.bind(mocha));
And then I load it with the electron executable
PATH=node_modules/.bin:$PATH electron run-tests.js