Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8380419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:27:14+00:00 2026-06-09T16:27:14+00:00

My question is about unit testing javascript code using Mocha. Ultimately this may be

  • 0

My question is about unit testing javascript code using Mocha. Ultimately this may be a question about dependency-injection or Inversion of Control, I have a poor grasp on these concepts.

I’m trying to write a test which simply creates an instance of header.js and tests that it is an object. However anytime I do so my test blows up when it loads Modernizr, with the following error message. It seems to occur because Modernizr is dependent on running inside the browser, but my unit tests need to run on the command line.

/lib/modernizr.js:8

“MozAppearance”in l.style,s=r&&!!b.createRange().compareNode,t=s?l:n.parentNod
^
TypeError: Cannot read property ‘parentNode’ of undefined
at /Users/devadmin/devenv/djscript/djscript-vendor/lib/modernizr.js:8:6291

I have some requirejs modules which are in the following hierarchy:

lib
└── pj
    ├── module
    │   └── header
    │       └── header.js
    ├── util
    │   └── header
    │       └── Util.js
    └── widget
        └── header
            ├── Header.js
            └── view
                ├── HeaderView.js
                └── NavView.js

header.js instantiates Header.js, which in turn creates a HeaderView and a NavView. Only HeaderView requires Modernizr.

Any hints as to how I can get this code into a testable state? Would it help to see how I packaged Modernizr for use with requirejs?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T16:27:16+00:00Added an answer on June 9, 2026 at 4:27 pm

    well, I haven’t used Mocha specifically but you should be able to create a fake Modernizr instance in your tests / harness by doing something like…

    define(<module path/name - e.g. 'libs/modernizr'>, function(){
        return {
            history: false,
            foo: true,
            bar: false
        };
    });
    

    …before you try to load up your module under test. With that fake instance defined, your module under test should use the fake instead of the actual modernizr lib.

    You are basically hijacking AMD’s “Named Module” (link: http://requirejs.org/docs/api.html#modulename) functionality to substitute the real modernizr with your fake. In this way you can explicitly test any states you need.

    Hope it helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is about the extent to which it makes sense unit testing. I’ve
This question isn't about unit-testing. And it is for a desktop product. This is
Most of the existing questions about unit testing JavaScript code and integrating it into
Unit testing Abstract classes in Groovy I asked a question previous about unit testing
I have a question about unit testing. Say I have a controller with one
Most of the discussion on this site is very positive about unit testing. I'm
My company is fairly new to unit testing our code. I've been reading about
Quick question about unit testing. My application has a few embeded Xml files, they
I am learning about unit-testing best-pratices (especially thanks to this post : What makes
Yet another simple question about unit testing model objects that uses data access layer.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.