Skip to content

Commit

Permalink
Make "weak" optional dependency and check it at runtime (#4984)
Browse files Browse the repository at this point in the history
* Make "weak" optional dependency and check it at runtime

* Update index.js
  • Loading branch information
mjesun authored and cpojer committed Nov 29, 2017
1 parent ef55e89 commit e00529d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
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(
'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

0 comments on commit e00529d

Please sign in to comment.