I am able to write test cases for electron.js POC code but their results are not reflecting on nyc code coverage results. As per my understanding, code coverage HTML results files should reflect the covered test cases, but it’s not happening. I have tried to find a solution on google, but no luck so far. I didn’t get much about the code coverage for electron code on google, Badly stuck with this task. I am desperately looking out for the solution. Hence, I thought to raise my doubt in this forum.
Any suggestion or input would be highly appreciated & great help to me! Thanks for reading .
**Github code link:**
https://github.com/sajid983356/pocElectronCodeCoverage
**main.js**
const { app, BrowserWindow, ipcMain } = require('electron');
let mainWindow;
function createWindow() {
mainWindow = new BrowserWindow({ width: 800, height: 600 });
mainWindow.loadFile('index.html');
mainWindow.on('closed', function () {
mainWindow = null;
});
}
app.on('ready', createWindow);
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', function () {
if (mainWindow === null) {
createWindow();
}
});
ipcMain.on('greet-me', (event, args) => {
console.log("step 1")
event.sender.send('greeting', args + ' Jane');
});
**renderer.js**
const { ipcRenderer } = require('electron');
const greetButton = document.getElementById('greetButton');
const greetElement = document.getElementById('greet');
greetButton.addEventListener('click', () => {
ipcRenderer.send('greet-me', 'Hello');
});
ipcRenderer.on('greeting', (event, args) => {
greetElement.innerText = args;
});
**test/spec.js**
const electron = require('electron');
const Application = require('spectron').Application;
const expect = require('chai').expect;
describe('Spectorn example', function () {
this.timeout(20000); //10 seconds
global.app = null;
//starts the application before all the test in this block
before(() => {
//create the electron app
app = new Application({
path: electron,
args: ['.']
});
//start the electron app
return app.start().then(() => {
app.client.waitUntilWindowLoaded();
app.browserWindow.show();
return app;
});
});
//stop th electron application after all the test
after(() => {
if (app && app.isRunning()) {
return app.stop();
}
});
it('should open the browserwindow', () => {
return app.client
.waitUntilWindowLoaded()
.browserWindow.isVisible()
.then(res => {
console.log('visible: ', res);
expect(res).to.be.equal(true);
})
.browserWindow.isFocused()
.then(res => {
console.log('isFocused: ', res);
expect(res).to.be.equal(true);
})
.browserWindow.isMinimized()
.then(res => {
console.log('isMinimized: ', res);
expect(res).to.be.equal(false);
})
.browserWindow.isDevToolsOpened()
.then(res => {
console.log('isDevToolsOpened: ', res);
expect(res).to.be.equal(false);
});
});
it('should open the browserwindow with correct size', () => {
return app.client
.waitUntilWindowLoaded()
.browserWindow.getBounds()
.then(res => {
expect(res.width).to.be.equal(800);
expect(res.height).to.be.equal(600);
});
});
it('should communicate with main process and renderer process', async () => {
const results = await app.client.waitUntilWindowLoaded();
const text = await app.client.getText('#greet');
expect(text).to.equal('');
await app.client.click('#greetButton');
const greetText = await app.client.getText('#greet');
expect(greetText).to.equal('Hello Jane');
return results;
});
});
**package.json**
{
"name": "electron-quick-start",
"version": "1.0.0",
"description": "A minimal Electron application",
"main": "main.js",
"scripts": {
"start": "electron .",
"test": "mocha",
"coverage": "nyc npm run test"
},
"repository": "https://github.com/electron/electron-quick-start",
"keywords": [
"Electron",
"quick",
"start",
"tutorial",
"demo"
],
"author": "GitHub",
"license": "CC0-1.0",
"devDependencies": {
"chai": "^4.2.0",
"electron": "^3.0.4",
"mocha": "^5.2.0",
"spectron": "^5.0.0"
},
"dependencies": {
"nyc": "^15.1.0"
}
}
**index.html**
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Testing</title>
</head>
<body>
<button id="greetButton">Greet Me</button>
<p id="greet"></p>
<script>
// You can also require other files to run in this process
require('./renderer.js')
</script>
</body>
</html>
**.nycrc.json**
{
"all": true,
"check-coverage": false,
"branches": 100,
"lines": 100,
"functions": 85,
"statements": 10,
"report-dir": "./coverage",
"reporter": [
"cobertura",
"html",
"lcov",
"text"
],
"include": [
"*.js"
]
}
**PFA for the references:**