Skip to content

Commit 21b552b

Browse files
committed
Add type weak-napi use in jest-leak-detector and import package at top level
1 parent 7521ffd commit 21b552b

File tree

3 files changed

+12
-19
lines changed

3 files changed

+12
-19
lines changed

packages/jest-leak-detector/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
"types": "build/index.d.ts",
1212
"dependencies": {
1313
"jest-get-type": "^24.8.0",
14-
"pretty-format": "^24.8.0"
14+
"pretty-format": "^24.8.0",
15+
"weak-napi": "^1.0.3"
1516
},
1617
"devDependencies": {
17-
"weak-napi": "^1.0.3"
18+
"@types/weak-napi": "^1.0.0"
1819
},
1920
"engines": {
2021
"node": ">= 6"

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

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import v8 from 'v8';
99
import vm from 'vm';
10+
import weak from 'weak-napi';
1011
import prettyFormat from 'pretty-format';
1112
import {isPrimitive} from 'jest-get-type';
1213

@@ -23,23 +24,7 @@ export default class {
2324
);
2425
}
2526

26-
let weak;
27-
28-
try {
29-
// eslint-disable-next-line import/no-extraneous-dependencies
30-
weak = require('weak-napi');
31-
} catch (err) {
32-
if (!err || err.code !== 'MODULE_NOT_FOUND') {
33-
throw err;
34-
}
35-
36-
throw new Error(
37-
'The leaking detection mechanism requires the "weak-api" package to be installed and work. ' +
38-
'Please install it as a dependency on your main project',
39-
);
40-
}
41-
42-
weak(value, () => (this._isReferenceBeingHeld = false));
27+
weak(value as object, () => (this._isReferenceBeingHeld = false));
4328
this._isReferenceBeingHeld = true;
4429

4530
// Ensure value is not leaked by the closure created by the "weak" callback.

yarn.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2103,6 +2103,13 @@
21032103
resolved "https://registry.yarnpkg.com/@types/tough-cookie/-/tough-cookie-2.3.5.tgz#9da44ed75571999b65c37b60c9b2b88db54c585d"
21042104
integrity sha512-SCcK7mvGi3+ZNz833RRjFIxrn4gI1PPR3NtuIS+6vMkvmsGjosqTJwRt5bAEFLRz+wtJMWv8+uOnZf2hi2QXTg==
21052105

2106+
"@types/weak-napi@^1.0.0":
2107+
version "1.0.0"
2108+
resolved "https://registry.yarnpkg.com/@types/weak-napi/-/weak-napi-1.0.0.tgz#b0977c0737cb62d028c4eda76f4e295bb3ae3c49"
2109+
integrity sha512-viW/kPA1gpeoNdUge025WqmThQ2lnnHzZWZJM5KlH8w9E5YehOh3GnDjW5w/sAEC91VOlePEiFSQmbnX7VVyLw==
2110+
dependencies:
2111+
"@types/node" "*"
2112+
21062113
"@types/write-file-atomic@^2.1.1":
21072114
version "2.1.2"
21082115
resolved "https://registry.yarnpkg.com/@types/write-file-atomic/-/write-file-atomic-2.1.2.tgz#1657761692b70cf9ad2593e4eb4ac498adb9463f"

0 commit comments

Comments
 (0)