Mocha: Still complains about "window is not defined" but have it defined in globals

Created on 14 Aug 2017  路  2Comments  路  Source: mochajs/mocha

Should not complain about undefined window when I have it defined like that:

NODE_PATH=./src/modules:./lib \
  ./node_modules/.bin/mocha \
  --globals window,navigator \
  --colors \
  --compilers coffee:coffee-script/register \
  ./**/__mocha__/*

Happens on latest version.

question

Most helpful comment

From Mocha's --help documentation:

--check-leaks check for global variable leaks
--globals allow the given comma-delimited global [names]

And from the elaborations available at https://mochajs.org:

--globals
Accepts a comma-delimited list of accepted global variable names. For example, suppose your app deliberately exposes a global named app and YUI, you may want to add --globals app,YUI. It also accepts wildcards. You could do --globals '*bar' and it would match foobar, barbar, etc. You can also simply pass in '*' to ignore all globals.

--check-leaks
By default, Mocha will not check for global variables leaked while running tests, to enable this pass --check-leaks, to specify globals that are acceptable use --globals, for example --globals jQuery,MyLib.

TL;DR: --globals does not define anything and is niether documented as doing so nor intended to. Rather, it's used to tell Mocha to make a specific exception to the optional behavior of complaining if your code adds global variables.


Given that your code is trying to use window, which is typically the name of the global context in a browser environment, I'm going to go out on a limb and guess that you're trying to do one of the following (so as to offer some pointers for each):

  • Testing a website? Run your site and use Selenium to open a browser and test it. Selenium controls a browser using interactions that simulate a user, allowing a very realistic functional/integration test.
  • Testing JS code that is supposed to run in a browser? Use Karma. Karma takes test runners such as Mocha and runs them in a browser -- from the commandline so you can get the results programmatically instead of having to manually open a browser page and see the test results.
  • Testing JS code that is specifically meant to be used in Node with JSDOM or a similar library? Make sure you set up JSDOM (or whichever library is meant to be used) in your tests as you would when actually using the code being tested.

Mocha doesn't do any of these things by itself*; it does, however, work great with each of these three tools for these three different use cases.

*(except testing JS code in a browser manually where you can't get the results programmatically, which Mocha supports directly but which really is only useful if you want to be able to distribute a package and run its tests without having to have Node or something like that)

All 2 comments

From Mocha's --help documentation:

--check-leaks check for global variable leaks
--globals allow the given comma-delimited global [names]

And from the elaborations available at https://mochajs.org:

--globals
Accepts a comma-delimited list of accepted global variable names. For example, suppose your app deliberately exposes a global named app and YUI, you may want to add --globals app,YUI. It also accepts wildcards. You could do --globals '*bar' and it would match foobar, barbar, etc. You can also simply pass in '*' to ignore all globals.

--check-leaks
By default, Mocha will not check for global variables leaked while running tests, to enable this pass --check-leaks, to specify globals that are acceptable use --globals, for example --globals jQuery,MyLib.

TL;DR: --globals does not define anything and is niether documented as doing so nor intended to. Rather, it's used to tell Mocha to make a specific exception to the optional behavior of complaining if your code adds global variables.


Given that your code is trying to use window, which is typically the name of the global context in a browser environment, I'm going to go out on a limb and guess that you're trying to do one of the following (so as to offer some pointers for each):

  • Testing a website? Run your site and use Selenium to open a browser and test it. Selenium controls a browser using interactions that simulate a user, allowing a very realistic functional/integration test.
  • Testing JS code that is supposed to run in a browser? Use Karma. Karma takes test runners such as Mocha and runs them in a browser -- from the commandline so you can get the results programmatically instead of having to manually open a browser page and see the test results.
  • Testing JS code that is specifically meant to be used in Node with JSDOM or a similar library? Make sure you set up JSDOM (or whichever library is meant to be used) in your tests as you would when actually using the code being tested.

Mocha doesn't do any of these things by itself*; it does, however, work great with each of these three tools for these three different use cases.

*(except testing JS code in a browser manually where you can't get the results programmatically, which Mocha supports directly but which really is only useful if you want to be able to distribute a package and run its tests without having to have Node or something like that)

very good points - give me time to figure out which of those tests should be moved to selenium, karma or jsdom. thanks heaps for your honest feedback and links. again, very good comments. thanks

Was this page helpful?
0 / 5 - 0 ratings