Working with global window variable in mocha js from node

19,971

Solution 1

You are ignoring the difference between running JavaScript code in the browser and running JavaScript code in Node.

In the browser, the window name is a reference to the object which holds all your global variables. So when you do foo = 1 in the outermost scope, you declare a global foo, which is also accessible as window.foo. Conversely, if you assign a new field like this: window.bar = 1, then you have a new global called bar.

In Node, your global object is accessed as global. So if you do foo = 1 in the outermost scope, foo is also accessible as global.foo. And if you do global.bar = 1, you have a new global named bar.

Your code shows that you modify a window object, which does not appear to be a reference to the global object. Options:

  1. Run Mocha in the browser instead of in Node. See Mocha's documentation.

  2. Set your Node environment so that it mimics enough of a browser environment to satisfy node. Setting a global window variable to be a equal to global might be enough but I don't know Backbone enough to know whether Backbone will be happy with this.

  3. Run your Backbone-based code in jsdom. Jsdom provides realistic window and document, as if your code was running in a browser, but it has its limits. I don't know whether Backbone would be happy with those limits.

Solution 2

Another solution would be to use https://www.npmjs.com/package/window-or-global

import React, { Component } from 'react'
// in node, you'll get the global object instead of crashing by an error 
import root from 'window-or-global'

class MyComponent extends Component {

  // this method is only invoked in the browser environment 
  componentDidMount() {
    root.addEventListener(/*...*/)
  }

  componentWillUnmount() {
    root.addEventListener(/*...*/)
  }

  render() {}

}

// Voilà. Enjoy your universal react component! ;) 
// No more 'window is not defined' errors when you render your component 
// on server side. 

To install, run npm install --save window-or-global.

Running tests on server (for example with mocha-webpack) is way more faster than in a browser.

Share:
19,971
ivan
Author by

ivan

Updated on June 03, 2022

Comments

  • ivan
    ivan almost 2 years

    I am new to js unit testing and I am trying to use mocha for my backbone contact manager tutorial that i found at this github repo. However, i have a global window.ContactManager variable that I firsted wanted to test whether it exists and then test the router.on functionality inside the start function later. The variable looks like so:

      window.ContactManager = {
      Models: {},
      Collections: {},
      Views: {},
    
      start: function(data) {
        var contacts = new ContactManager.Collections.Contacts(data.contacts),
            router = new ContactManager.Router();
    
        router.on('route:home', function() {
          router.navigate('contacts', {
            trigger: true,
            replace: true
          });
        });
    
        router.on('route:showContacts', function() {
          var contactsView = new ContactManager.Views.Contacts({
            collection: contacts
          });
    .....
    

    My test that does not work: var expect = require ('chai').expect;

    describe("Application", function() {
        it('creates a global variable for the name space ContactManager' , function () {
            expect(ContactManager).to.exist;
        })
    });
    

    How do I test and access a global window variable existence in mocha from running the tests in the console?