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

chore: make weak-napi optional #8869

Merged
merged 1 commit into from
Aug 22, 2019
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
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