Reputation: 8932
I m trying to build an application using Electron.
I need to make some unit test based on the electron env and using electron packages.
This way, I m using spectron to simulate my application.
On the documentation, it's written that I have to put in 'path' property the path where my executable file is. I don't have executable for now, I m in development mode.
Here's what I've tried based on another question :
beforeEach(() => {
app = new Application({
path: 'node_modules/.bin/electron'
});
app.start().then(res => console.log(res), err => console.log(err));
});
Nothing appears on the prompt and the following test is failing telling that I can't get getWindowCount on an undefined object (clearly, the app isn't instantiated) :
it('should call currentWindow', (done) => {
app.client.getWindowCount().then((count) => {
expect(count).to.equals(1);
done();
});
});
Does anybody knows what should I put in this path to make my test env work ?
PS : I m using mocha chai and sinon.
Thanks for your help
Upvotes: 2
Views: 2244
Reputation: 3285
You can also provides "electron" to the variables path if you use electron-prebuilt as mentioned in the doc :
path - Required. String path to the Electron application executable to launch. Note: If you want to invoke electron directly with your app's main script then you should specify path as electron via electron-prebuilt and specify your app's main script path as the first argument in the args array.
I think it looks like this :
import electron from 'electron'
import { Application } from 'spectron'
describe('application launch', function () {
this.timeout(10000)
beforeEach(function () {
this.app = new Application({
path: electron,
args: ['app']
})
return this.app.start()
})
...
}
Upvotes: 2
Reputation: 301
At first I was creating an executable for the purpose of testing, but that's actually not necessary.
You can see that Spectron has an example test and a global setup.
The example passes an option called args, and that’s exactly what you are missing. This is what I am doing:
var appPath = path.resolve(__dirname, '../'); //require the whole thing
var electronPath = path.resolve(__dirname, '../node_modules/.bin/electron');
beforeEach(function() {
myApp = new Application({
path: electronPath,
args: [appPath], // pass args along with path
});
return myApp.start().then(function() {
assert.equal(myApp.isRunning(), true);
chaiAsPromised.transferPromiseness = myApp.transferPromiseness;
return myApp;
});
});
My test sits in ./tests/app-test.js. The above works for me.
Upvotes: 11