jprichardson / electron-mocha

Run Mocha tests in Electron
MIT License
346 stars 64 forks source link

app.getAppPath() returns node_modules/electon-mocha/lib instead of my app path #158

Closed davfive closed 4 years ago

davfive commented 4 years ago

Hi, Version Info: electron-mocha: 8.2.2, mocha: 7.1.1, electron: 7.2.3

I added app.getAppPath() lookup to a helper file that now gets as part of my tests. Previously I was directly calling mocha, however with this I needed to start using electron-mocha (I think). When I run this from within my built electron app, it properly gives me the top-level of my src directory (package.json:

Essentially what happens is that if I run

npx electron-mocha 'src/**/*.spec.js'

Then when I have a standard js file (not part of a spec), I get this

const app = require('electron').app || require('electron').remote.app
console.error(app.getAppPath())
=> (actual) /Users/me/path-to-repo/node_modules/electron-mocha/lib
=> (expected) /Users/me/path-to-repo/src/
# Expected because in my path-to-repo/package.json, I specify 'src/main.js' as the entry point

I'm not sure if this is expected (albeit unfortunate) behavior or if I'm just missing something after reading the docs and scouring the source. I can't find out how to resolve this.

I've included a minimal setup that illustrates this. Just unzip the file and run

yarn
npx electron-mocha 'src/index.spec.js'

electron-mocha-wrong-getAppPath.zip

inukshuk commented 4 years ago

Yes, this is expected behavior. app.getAppPath will return the path to the 'entry point' script called by Electron. When you run electron-mocha, you're not executing your app, but electron-mocha so this is totally fine. To work around this I would suggest mocking out getAppPath as this would probably be the right approach anyway, since this is a value that will vary greatly, especially when you package your app.

davfive commented 4 years ago

Thanks. That's what I ended up doing.