Using ESNext in test cases

Mocha does not support ESNext features. You may have to use babel to use the latest ECMAScript features in your test cases. However, there is another testing framework, called AVA, which gives you the ability to use the latest JavaScript language features out of the box in your test cases. Why do we need to use this? It's because most of the Spectron APIs return the JavaScript promise. If you look at the preceding code, you may note the long list of callbacks and promise chains just to get a simple output, especially when we need to access the Electron and client API. With ES2017, this can be avoided using async functions. Also, there are a number of useful features that reduce our effort and code available in the new ECMAScript. You can even use Mocha with Babel transpiler to get the same features in your current test cases. As Spectron supports any testing frameworks, let's check how we can use AVA framework to test our Electron application. 

First, import the dependencies in your test case code:

import test from 'ava';
import { Application } from 'spectron';

You can use the ES6 import statement with this framework. Add the initialization code using the beforeEach life cycle callback:

test.beforeEach(async t => {
t.context.app = new Application({
path: '/Applications/MyApp.app/Contents/MacOS/MyApp'
});

await t.context.app.start();
});

The code is similar to Mocha, but it uses an async function instead of promises. Next, add the afterEach callback for cleaning up the resources:

test.afterEach.always(async t => {
await t.context.app.stop();
});

Add the actual test to the file by adding the following code:

test(async t => {
const app = t.context.app;
await app.client.waitUntilWindowLoaded();

const win = app.browserWindow;
t.is(await app.client.getWindowCount(), 1);
t.false(await win.isMinimized());
t.false(await win.isDevToolsOpened());
t.true(await win.isVisible());
t.true(await win.isFocused());

const {width, height} = await win.getBounds();
t.true(width > 0);
t.true(height > 0);
});

The code itself briefs us about the type of checking that it's executing in each line. The test method is the key point to enter into a test case. That's what accepts an async function as the parameter. The app.client.waitUntiWindowLoad asks the test case to wait until the Electron loads its window before executing the next line. Once it is loaded, then the browserWindow object can be accessed and used to test the features and functionalities. The t.is method is similar to  assert.equal that we used with Mocha. The currentCount should be equal to one in our test case. You should be able to understand the rest of the line as the code itself describes what functionality is being tested.

To run the test case, install the framework using npm into your global npm cache:

npm install -g ava

The ava command can be used to run the tests. The command is the same as Mocha that we used in the preceding section. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.72.165