How to Test Native Modules


#1

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?


#2

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:

Here are some test cases we used under this method:

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?
#3

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));

mocha.run(process.exit);

And then I load it with the electron executable

PATH=node_modules/.bin:$PATH electron run-tests.js