Dynamic loading of debug possibly incompatible with Jest
See original GitHub issueI 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:
- Created 3 years ago
- Reactions:3
- Comments:15 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 therequire
statement,debug
isundefined
(the core issue). However, doing this:Apparently (and I’m just learning this), naming your module
debug
and requiring a module nameddebug
is not supported. To test, I renamed the old filefollow-debug.js
and the problem is solved.I’m not sure this warrants changing the fix, but I’m posting here for posterity
It probably is. That’s the insight we needed! Jest does some magic hoisting.