Skip to content

Commit

Permalink
chore: make weak-napi optional (#8869)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored Aug 22, 2019
1 parent 93555aa commit 16f7385
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
6 changes: 3 additions & 3 deletions packages/jest-leak-detector/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
"types": "build/index.d.ts",
"dependencies": {
"jest-get-type": "^24.9.0",
"pretty-format": "^24.9.0",
"weak-napi": "^1.0.3"
"pretty-format": "^24.9.0"
},
"devDependencies": {
"@types/weak-napi": "^1.0.0"
"@types/weak-napi": "^1.0.0",
"weak-napi": "^1.0.3"
},
"engines": {
"node": ">= 8"
Expand Down
17 changes: 16 additions & 1 deletion packages/jest-leak-detector/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import {setFlagsFromString} from 'v8';
import {runInNewContext} from 'vm';
import weak = require('weak-napi');
import prettyFormat = require('pretty-format');
import {isPrimitive} from 'jest-get-type';

Expand All @@ -24,6 +23,22 @@ export default class {
);
}

let weak: typeof import('weak-napi');

try {
// eslint-disable-next-line import/no-extraneous-dependencies
weak = require('weak-napi');
} catch (err) {
if (!err || err.code !== 'MODULE_NOT_FOUND') {
throw err;
}

throw new Error(
'The leaking detection mechanism requires the "weak-napi" package to be installed and work. ' +
'Please install it as a dependency on your main project',
);
}

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

Expand Down

0 comments on commit 16f7385

Please sign in to comment.