Skip to content

Commit e00529d

Browse files
mjesuncpojer
authored andcommitted
Make "weak" optional dependency and check it at runtime (#4984)
* Make "weak" optional dependency and check it at runtime * Update index.js
1 parent ef55e89 commit e00529d

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

packages/jest-leak-detector/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
"license": "MIT",
99
"main": "build/index.js",
1010
"dependencies": {
11-
"pretty-format": "^21.2.1",
11+
"pretty-format": "^21.2.1"
12+
},
13+
"optionalDependencies": {
1214
"weak": "^1.0.1"
1315
}
1416
}

packages/jest-leak-detector/src/index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import prettyFormat from 'pretty-format';
1313
import v8 from 'v8';
1414
import vm from 'vm';
15-
import weak from 'weak';
1615

1716
const PRIMITIVE_TYPES = new Set([
1817
'undefined',
@@ -35,6 +34,17 @@ export default class {
3534
);
3635
}
3736

37+
let weak;
38+
39+
try {
40+
weak = require('weak');
41+
} catch (err) {
42+
throw new Error(
43+
'The leaking detection mechanism requires the "weak" package to work. ' +
44+
'Please make sure that you can install the native dependency on your platform.',
45+
);
46+
}
47+
3848
weak(value, () => (this._isReferenceBeingHeld = false));
3949
this._isReferenceBeingHeld = true;
4050

0 commit comments

Comments
 (0)