diff --git a/packages/jest-leak-detector/package.json b/packages/jest-leak-detector/package.json index 4cd1e9bf41e5..79be99fda730 100644 --- a/packages/jest-leak-detector/package.json +++ b/packages/jest-leak-detector/package.json @@ -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" } } diff --git a/packages/jest-leak-detector/src/index.js b/packages/jest-leak-detector/src/index.js index 7649aa26b770..33e6f5787350 100644 --- a/packages/jest-leak-detector/src/index.js +++ b/packages/jest-leak-detector/src/index.js @@ -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', @@ -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;