Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Endless recursion on circular dependencies #13

Closed
vectronic opened this issue Mar 4, 2020 · 5 comments · Fixed by #14
Closed

Endless recursion on circular dependencies #13

vectronic opened this issue Mar 4, 2020 · 5 comments · Fixed by #14

Comments

@vectronic
Copy link

Hello,

I have an issue when using clear-module within a Mocha unit test: I get an endless recursion. However if I run it outside of a test I don't.

I have successfully used clear-module in tests before. It looks to be related to:

  • upgrading to clear-module: "4.1.0" as the example below works with "3.1.0"
  • clearing sshpk as this has circular dependencies defined

This works fine:

// demo.js
require('sshpk');
const clearModule = require('clear-module');
console.log('before');
clearModule('sshpk');
console.log('after');

produces:

% node demo.js
before
after

However if I run using mocha, it hangs on the call to clearModule:

require('sshpk');
const clearModule = require('clear-module');
console.log('before');
clearModule('sshpk');
console.log('after');

describe('yyy', () => {

    it('xxx', function () {
    });
});

produces:

% ./node_modules/.bin/mocha test/services/demo.spec.js
before

When I debug this I can see an endless recursion of clearing the sshpk modules which have a circular reference.

I am using node 12.6.0 and the following lib versions:

  • "mocha": "7.1.0"
  • "clear-module": "4.1.0" (previously 3.1.0 which worked)
  • "sshpk": "1.16.1"
@vectronic
Copy link
Author

UPDATE: If I use clear-module v3.2.0 it works, but with v4.0.0 it hangs.

@sindresorhus
Copy link
Owner

Probably related to ccd516c. You might want the clearModule.single() method.

// @omrilotan

@omrilotan
Copy link
Contributor

I will issue a pull request this evening

@omrilotan
Copy link
Contributor

I have reproduced the issue with the following steps:

  • create a new npm project and install mocha, sshpk, clear-module

  • Create a test file

const sshpk = require('sshpk');
const clear = require('clear-module');

describe('reproduce this issue', () => {
	it('Should not get stuck between Before and After', () => {
		console.log('Before');
		clear('sshpk');
		console.log('After');
	})
});

#14 should definitely resolve this issue

@vectronic
Copy link
Author

Confirmed fixed for me - thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants