Spectron with Jest to test basic window open not working
Asked Answered
I

1

8

I am developing an Electron desktop app with React. Trying to setup testing for the electron side of code. I have a Launch.test.js file, where I am trying to test spectron with Jest. When I run the test file its throwing errors. Having trouble fixing the errors.

My file structure is like this

node_modules
public
src
 --components
 --tests
   --Launch.test.js
main.js
package.json
README.md

And the code for Launch.test.js goes like follow -

const Application = require('spectron').Application;
const path = require('path');

let electronPath = path.join(__dirname, '..', '..', 'node_modules', '.bin', 'electron');

if (process.platform === 'win32') {
     electronPath += '.cmd';
}

const appPath = path.join(__dirname, '..', '..');

const app = new Application({
  path: electronPath,
  args: [appPath],
});

describe('Test Example', () => {
  beforeEach(() => {
  return app.start();
});

afterEach(() => {
   return app.stop();
});

it('opens a window', () => {
  expect.assertions(1);
  return app.client.element('input').getValue().then((j) => {
    console.log('xxxx', j);
    expect(1).toEqual(1);
  });
});

});

When i run it I get the following errors.

src/tests/Launch.test.js (12.74s)
Test Example
× opens a window (5253ms)

● Test Example › opens a window

Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.

  at mapper (node_modules/jest-jasmine2/build/queue_runner.js:41:52)

● Test Example › opens a window

An element could not be located on the page using the given search parameters ("input").

  at execute(<Function>, "require") - C:/Users/CKE1LUD/Desktop/repos/leela/node_modules/spectron/lib/api.js:63:26

● Test Example › opens a window

Application not running

  21 |
  22 |   afterEach(() => {
> 23 |     return app.stop();
     |                ^
  24 |   });
  25 |
  26 |   it('opens a window', () => {

  at Application.Object.<anonymous>.Application.stop (node_modules/spectron/lib/application.js:59:48)
  at Object.stop (src/tests/Launch.test.js:23:16)

● Test Example › opens a window

expect.assertions(1)

Expected one assertion to be called but received zero assertion calls.

  25 |
  26 |   it('opens a window', () => {
> 27 |     expect.assertions(1);
     |            ^
  28 |     return app.client.element('input').getValue().then((j) => {
  29 |       console.log('xxxx', j);
  30 |       expect(1).toEqual(1);

  at Object.assertions (src/tests/Launch.test.js:27:12)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        15.316s
Immoralist answered 11/1, 2019 at 12:55 Comment(2)
Where you able to solve this?Micaelamicah
I haven't been working on that project for years. If someone finds a suitable solution they are welcome to mark it as the correct answerExpressive
E
1

Use jest.setTimeout() to increase the waiting timeout.

Sometimes you have a lot of initialization code before the Electron is really up, so you want to wait longer than the default (5000).

Extenuate answered 28/1, 2020 at 19:23 Comment(2)
I still get the error ``` { Error: unknown error: Chrome failed to start: exited abnormally. (unknown error: DevToolsActivePort file doesn't exist) (The process started from chrome location path-to-my-project/node_modules/spectron/lib/launcher.js is no longer running, so ChromeDriver is assuming that Chrome has crashed.)... ```Expressive
@FedericoBellucci have you managed this problem since? if not, what workaround was used?Flaggy

© 2022 - 2024 — McMap. All rights reserved.