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

Make "weak" optional dependency and check it at runtime #4984

Merged
merged 2 commits into from
Nov 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/jest-leak-detector/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
"license": "MIT",
"main": "build/index.js",
"dependencies": {
"pretty-format": "^21.2.1",
"pretty-format": "^21.2.1"
},
"optionalDependencies": {
"weak": "^1.0.1"
}
}
12 changes: 11 additions & 1 deletion packages/jest-leak-detector/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import prettyFormat from 'pretty-format';
import v8 from 'v8';
import vm from 'vm';
import weak from 'weak';

const PRIMITIVE_TYPES = new Set([
'undefined',
Expand All @@ -35,6 +34,17 @@ export default class {
);
}

let weak;

try {
weak = require('weak');
} catch (err) {
throw new Error(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This hides all sorts of errors. can we do an explicit check for MODULE_NOT_FOUND, and if not, rethrow original error?

(can use https://www.npmjs.com/package/optional and rethrow, but rolling our own should not be an issue)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for convenience, optional would be something like

const weak = optional('weak', { rethrow: true });

if (!weak) {
  throw new Error(
    'The leaking detection mechanism requires the "weak" package to work. ' +
      'Please make sure that you can install the native dependency on your platform.',
  );
}

current usage is just an if away, so might not be worth the extra dep 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the stricter check, let me add that into a new PR but as our own custom check; checking error.code should not require a new dependency 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in #5010.

'The leaking detection mechanism requires the "weak" package to work. ' +
'Please make sure that you can install the native dependency on your platform.',
);
}

weak(value, () => (this._isReferenceBeingHeld = false));
this._isReferenceBeingHeld = true;

Expand Down