question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

It seems no work when use mocha-jsdom when depend lib have 'window' variable.

See original GitHub issue

HI, I have a JS file import other libs which use the window variable and I want to test it by mocha.
I use ES6 and I must compile the test JS file by babel, I use the command mocha --compilers js:babel-core/register, but when I compile the JS there are some error as follow.
I use mocha-jsdom but it seems no work.
How I use mocha-jsdom to mock the ‘window’ variable? Thanks.

JS file something like this:

import xxx from 'xxx'; // use the 'window' variable

export default function foo() {
  xxx.bar(); 
}

test file something like this:

import jsdom from 'mocha-jsdom';
import foo from './foo';

describe('test' () => {
  jsdom();

  it(' test test', () => {
    foo();     
  });
});

error:

> mocha --compilers js:babel-core/register --recursive

.../node_modules/velocity-animate/velocity.js:403
})(window);
   ^

ReferenceError: window is not defined
    at Object.<anonymous> (...node_modules/velocity-animate/velocity.js:403:4)
    at Module._compile (module.js:425:26)
    at Module._extensions..js (module.js:432:10)
    at Object.require.extensions.(anonymous function) [as .js] (.../node_modules/babel-register/lib/node.js:138:7)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:313:12)
    at Module.require (module.js:366:17)
    at require (module.js:385:17)
    at Object.<anonymous> (.../node_modules/rc-collapse/lib/openAnimation.js:9:24)
    at Module._compile (module.js:425:26)
    at Module._extensions..js (module.js:432:10)
    at Object.require.extensions.(anonymous function) [as .js] (.../node_modules/babel-register/lib/node.js:138:7)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:313:12)
    at Module.require (module.js:366:17)
    at require (module.js:385:17)
    at Object.<anonymous> (.../node_modules/rc-collapse/lib/Collapse.js:17:22)
......

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

22reactions
EvHauscommented, Jul 31, 2019

For those running into the same problem, I found an alternate solution which seems to work better.

  1. npm uninstall mocha-jsdom
  2. npm install jsdom
  3. Create a new setup.js file somewhere and put this code in it:
import jsdom from 'jsdom';

global.document = jsdom.jsdom('<!doctype html><html><body></body></html>');
global.window = document.defaultView;
global.navigator = global.window.navigator;
  1. Load this file before all your mocha tests via mocha -r test/setup.js

Then you can import everything without issue and jsdom will be automatically loaded for all your tests.

1reaction
rstacruzcommented, Dec 18, 2015

You’ll need to import it inside a before() block.

var foo

describe('test', function () {
  jsdom()

  before(function () {
    foo = require('foo')
  })
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

Mocha: Can't seem to call module's methods that use jQuery ...
So, I am using Mocha/Chai for some testing and the item I am testing depends on jQuery. I am using jsdom to help...
Read more >
How to mock global window variables with Jest
Mocking non-existent globals. In case of variables which are globally available but are not provided by jsdom by default e.g. various API  ......
Read more >
Get Better at Unit Testing with Mocha, Chai, and JSDOM
Unit testing isn't a chore: it actually has great value! Learn its benefits and how you can improve unit testing through outlining, mocking, ......
Read more >
Configuration - Quokka.js
The settings in the global config file are applied to all Quokka files, no matter if you are running Quokka in an opened...
Read more >
An Overview of JavaScript Testing in 2022 - Medium
Jsdom simulates whatever you get when you run your JS inside the browser like window, document, body, location, cookies, selectors but it doesn' ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found