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.

Dynamic loading of debug possibly incompatible with Jest

See original GitHub issue

I am encountering this error in my test suite using nock 12.0.3 and axios 0.21.1. It seems to be intermittent as well.

TypeError: Cannot read property 'apply' of undefined
        at module.exports (/Users/brian.moran/repos/subscription-service/node_modules/follow-redirects/debug.js:15:9)
        at Object.request (/Users/brian.moran/repos/subscription-service/node_modules/follow-redirects/index.js:457:7)
        at dispatchHttpRequest (/Users/brian.moran/repos/subscription-service/node_modules/axios/lib/adapters/http.js:195:25)
        at new Promise (<anonymous>)
        at httpAdapter (/Users/brian.moran/repos/subscription-service/node_modules/axios/lib/adapters/http.js:46:10)
        at dispatchRequest (/Users/brian.moran/repos/subscription-service/node_modules/axios/lib/core/dispatchRequest.js:52:10)

I am able to “fix” the issue by adding if(debug === undefined) return; to debug.js

var debug;

module.exports = function () {
  if (!debug) {
    try {
      /* eslint global-require: off */
      debug = require("debug")("follow-redirects");
    }
    catch (error) {
      debug = function () { /* */ };
    }
  }
  
  // "fixes" TypeError: Cannot read property 'apply' of undefined 
  if(debug === undefined) return;
  debug.apply(null, arguments);
};

Is this a known issue or is perhaps my problem elsewhere?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:15 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
af-mikecrowecommented, Aug 18, 2021

For those curious, I may have solved this incorrectly. It seems the root cause is the fact the file is named debug.js and it is running in jest. If you set a breakpoint right after the require statement, debug is undefined (the core issue). However, doing this:

require.resolve('debug')
Uncaught RangeError: Maximum call stack size exceeded

Apparently (and I’m just learning this), naming your module debug and requiring a module named debug is not supported. To test, I renamed the old file follow-debug.js and the problem is solved.

I’m not sure this warrants changing the fix, but I’m posting here for posterity

1reaction
RubenVerborghcommented, Aug 9, 2021

it’s Jest related

It probably is. That’s the insight we needed! Jest does some magic hoisting.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is possible to debug dynamic loading JavaScript by some ...
Yes, It is (now) possible to debug dynamically loaded JavaScript using Google ... is replaced by #) to avoid errors on unsupported browsers...
Read more >
Troubleshooting - Jest
Try using the debugging support built into Node. Place a debugger; statement in any of your tests, and then, in your project's directory, ......
Read more >
Top 18 Most Common AngularJS Developer Mistakes - Toptal
Common Mistake #1: Accessing The Scope Through The DOM ... There are a few optimization tweaks recommended for production. One of them is...
Read more >
jest-environment-jsdom | Yarn - Package Manager
Jest will set process.env.NODE_ENV to 'test' if it's not set to something else. You can use that in your configuration to conditionally setup...
Read more >
Changelog - Cypress Documentation
Cypress component tests now correctly load assets with Angular. ... Type definitions will no longer conflict when running Cypress in a project with...
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