Skip to content

Commit

Permalink
Add type weak-napi use in jest-leak-detector and import package a…
Browse files Browse the repository at this point in the history
…t top level
  • Loading branch information
lh0x00 committed Jul 17, 2019
1 parent 6e471f7 commit be7a7c0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 19 deletions.
5 changes: 3 additions & 2 deletions packages/jest-leak-detector/package.json
Expand Up @@ -11,10 +11,11 @@
"types": "build/index.d.ts",
"dependencies": {
"jest-get-type": "^24.8.0",
"pretty-format": "^24.8.0"
"pretty-format": "^24.8.0",
"weak-napi": "^1.0.3"
},
"devDependencies": {
"weak-napi": "^1.0.3"
"@types/weak-napi": "^1.0.0"
},
"engines": {
"node": ">= 6"
Expand Down
19 changes: 2 additions & 17 deletions packages/jest-leak-detector/src/index.ts
Expand Up @@ -7,6 +7,7 @@

import v8 from 'v8';
import vm from 'vm';
import weak from 'weak-napi';
import prettyFormat from 'pretty-format';
import {isPrimitive} from 'jest-get-type';

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

let weak;

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-api" package to be installed and work. ' +
'Please install it as a dependency on your main project',
);
}

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

// Ensure value is not leaked by the closure created by the "weak" callback.
Expand Down
7 changes: 7 additions & 0 deletions yarn.lock
Expand Up @@ -1961,6 +1961,13 @@
resolved "https://registry.yarnpkg.com/@types/tough-cookie/-/tough-cookie-2.3.5.tgz#9da44ed75571999b65c37b60c9b2b88db54c585d"
integrity sha512-SCcK7mvGi3+ZNz833RRjFIxrn4gI1PPR3NtuIS+6vMkvmsGjosqTJwRt5bAEFLRz+wtJMWv8+uOnZf2hi2QXTg==

"@types/weak-napi@^1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@types/weak-napi/-/weak-napi-1.0.0.tgz#b0977c0737cb62d028c4eda76f4e295bb3ae3c49"
integrity sha512-viW/kPA1gpeoNdUge025WqmThQ2lnnHzZWZJM5KlH8w9E5YehOh3GnDjW5w/sAEC91VOlePEiFSQmbnX7VVyLw==
dependencies:
"@types/node" "*"

"@types/write-file-atomic@^2.1.1":
version "2.1.1"
resolved "https://registry.yarnpkg.com/@types/write-file-atomic/-/write-file-atomic-2.1.1.tgz#7f9fcd6c5c8d194dba03472e3fa6cb29a839764c"
Expand Down

0 comments on commit be7a7c0

Please sign in to comment.