Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing main context for unit tests on actual functions #114

Open
psvensson opened this issue Sep 7, 2016 · 3 comments
Open

Accessing main context for unit tests on actual functions #114

psvensson opened this issue Sep 7, 2016 · 3 comments

Comments

@psvensson
Copy link

Hi, I've tried to find a way by reading the current documentation to get a handle to the context of my main.js module, somehow.

It's is very elegant to test UI features, but it would be great to be able to use Spectron for all unit tests, but I can't find a way at the moment to do this.

For example, say that I have I have a function foo in my main.js (or in anything it has a reference to),I'd like to write a unit test that would look a bit like this;

it 'should just work ffs', (done)->
app.getMagicReferenceToMainJs().then (main)->
bar = main.foo()
expect(bar).to.equal(4711)
done()

In such a case, what would the magic function be named (or what should I use to the same effect)?
Thanks for any help!

@teameh
Copy link
Contributor

teameh commented Dec 23, 2016

It's not ideal, but what you could do is create regular unit tests files apart from your Spectron tests and mock the electron api you depend on, for example, If your code relies on `app.getPath('userData') you could create a mock like:

const mock = require('mock-require');
mock('electron', {
    app: {
        getPath: () => '/tmp'
    },
});

It's not ideal, but it works for me.

@iffy
Copy link

iffy commented Jan 25, 2017

I need the same thing but for a (very) slightly different reason. My app opens files, and I want to cause files to be opened from my tests. I would like to be able to do:

it('should open a file', () => {
  app.mainScopeOrWhatever.openFile('/path/to/file')
  app.browserWindow.isVisible().then(function (visible) {
    console.log('window is visible? ' + visible)
  })  
})

where my main.js has:

function openFile(path) {
  // ...
}

I wouldn't even mind having to explicitly deliver openFile to the tests inside main.js with something like:

if (process.env.RUNNING_IN_SPECTRON) {
    electron.for_test.openFile = openFile;
}

@iffy
Copy link

iffy commented Jan 25, 2017

For now, my workaround is to pollute app like this:

// main.js
if (process.env.RUNNING_IN_SPECTRON) {
  app.foobar = (something) => {
    return something + '!';
  }
}
// test.js
// ...
it('should run?', () => {
  return app.electron.remote.app.foobar('hey')
    .then((response) => {
      console.log('foobar response', response);
    })
})

but I'm not sure how well that will work going forward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants