Hardocs / desktop-app

Hardocs desktop app presenting current docs editing functionalities.
GNU General Public License v2.0
3 stars 2 forks source link

How to test the vuex store with electron and jest #68

Closed DNature closed 3 years ago

DNature commented 3 years ago

I was in the process of writing the first test for hardocs-desktop-app and this must include Vuex store; However, I'm getting this error and it's pointing to habitat-client:

Test suite failed to run

    TypeError: Cannot read property 'dialog' of undefined

      at Object.<anonymous> (node_modules/@hardocs-project/habitat-client/lib/modules/habitat-localservices.js:31:31)
      at Object.<anonymous> (node_modules/@hardocs-project/habitat-client/lib/modules/habitat-database.js:10:29)

Steps to reproduce the issue

  1. Clone feature/testing branch
  2. Install dependencies with yarn
  3. run tests: yarn test

Screenshot

image

DNature commented 3 years ago

@narration-sd Please take a look at this

jurra commented 3 years ago

@DNature It seems to me that dialog is an electron related module or function. Therefore I suspect you have to import electron in your test script in order for it to work. Did you try this? Check this also it can be relevant: https://www.electronjs.org/docs/api/dialog

narration-sd commented 3 years ago

On the surface, looks @jurra is likely on the right traiil with this. As far as I know dialog works fine - it's in use, and the Electron side is solid.

Whether you can fix your case by simply importing it is a good question -- if the tests may not be running on an active Electron interface. You'll have to learn the details and so forth to understand.

If you can't solve this readily, I may have an idea coming from the outside, but let's see what you discover.

DNature commented 3 years ago

I imported the dialog module from electron but it still did not work, I also tried the remote module but it didn't work.

Code example

import { createLocalVue } from '@vue/test-utils';
const { dialog } = require('electron').remote;

import Vuex from 'vuex';
import * as docs from '../docs';

const localVue = createLocalVue();
localVue.use(Vuex);

describe('Test for docs operations', () => {
  const store = new Vuex.Store({
    modules: {
      docs
    }
  });

  console.log(dialog);

  console.log(store.state.actions.cwd);
});
DNature commented 3 years ago

I also noticed that habitat client does not include electron as a dependency, therefore, destructuring dialog will not work.

image

Dependencies

image

DNature commented 3 years ago

I found this solution https://github.com/electron/electron/issues/3909#issuecomment-568048005

narration-sd commented 3 years ago

Divine, I think it's more a nature of what is actually running, operating, than of importing libraries.

In fact, you should not, and should not have to be, importing anything but the habitat-client -- it would be at least redundant, and at worst interfere.

Electron is a quite complex multi-thread-process machine, with much internal interaction expected, and it's not going to work for you unless it's all properly initiated and operating.

In this test you are trying to do, is there a fully running Electron user interface showing on a terminal? Probably not. Then you need to do as we do in the software profession, look for what other persons make available for the problem, have themselves done.

Especially, put the calm time in, to well understand, assimilate what we find.

I made a simple Google query ('unit testing Electron applications') and immediately found these two links. The first is a useful overview, minus the later part which is selling. The second is the free testing framework that he is talking about, which is built into Electron, Spectron.

https://smartbear.com/blog/how-to-test-electron-apps/
https://www.electronjs.org/spectron

At first glance, anyway, Spectron appears to be fully aware and ready to help you for tests that involve with the multi-threads and the ui -- as they should understand the challenge

DNature commented 3 years ago

Thank you for the spectron package you suggested @narration-sd . I never knew that electron has a different test system.

I will definitely look into it ASAP

jurra commented 3 years ago

We made a first progress and learned how to test vuex with jest using mocks without having to depend on electron to do the tests. But we might need to test also electron features like ipc messaging, and for this particular case we need to use spectron.