karma-runner / karma-mocha

A Karma plugin. Adapter for Mocha testing framework.
MIT License
379 stars 95 forks source link

window.mocha not defined when running in nw.js #184

Open kierans opened 7 years ago

kierans commented 7 years ago

Just like this question I'm trying to run Mocha tests via Karma in NW.js.

However my tests aren't running as window.mocha isn't defined.

Uncaught TypeError: Cannot read property 'setup' of undefined
at node_modules/karma-mocha/lib/adapter.js:250

Is it possible to do something like window.mocha = window.mocha || require("mocha") in the Adapter? Any other tips/suggestions.

insightfuls commented 7 years ago

It turns out this is an issue with Browserify, which is used to package Mocha for the browser. Mocha simply assigns global.mocha = ... and Browserify injects the first defined of global, self and window as global. In most browsers, global isn't defined, so window.mocha ends up being defined, which is what you want. But in NW.js both global and window are defined (because NW.js puts global on window.global) so Mocha ends up defined at global.mocha which is the wrong context. It also defines global.describe, global.it, etc. which in the browser should all be on window to work.

The issue is being tracked here: https://github.com/substack/insert-module-globals/pull/48

kierans commented 7 years ago

I've created https://github.com/kierans/karma-nodewebkit-mocha as a wrapper for karma-mocha until the underlying bug is fixed.