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

Migrate jest-leak-detector to TypeScript #7825

Merged
merged 9 commits into from
Feb 7, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- `[diff-sequences]`: Migrate to Typescript ([#7820](https://github.com/facebook/jest/pull/7820))
- `[jest-get-type]`: Migrate to TypeScript ([#7818](https://github.com/facebook/jest/pull/7818))
- `[jest-regex-util]`: Migrate to TypeScript ([#7822](https://github.com/facebook/jest/pull/7822))
- `[jest-leak-detector]`: Migrate to TypeScript ([#7825](https://github.com/facebook/jest/pull/7825))

### Performance

Expand Down
14 changes: 14 additions & 0 deletions packages/jest-leak-detector/build/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
export default class {
_isReferenceBeingHeld: boolean;
constructor(value: any);
isLeaking(): boolean;
_runGarbageCollector(): void;
_isPrimitive(value: any): boolean;
}
//# sourceMappingURL=index.d.ts.map
1 change: 1 addition & 0 deletions packages/jest-leak-detector/build/index.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

108 changes: 108 additions & 0 deletions packages/jest-leak-detector/build/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';

Object.defineProperty(exports, '__esModule', {
value: true
});
exports.default = void 0;

function _prettyFormat() {
const data = _interopRequireDefault(require('pretty-format'));

_prettyFormat = function _prettyFormat() {
return data;
};

return data;
}

function _v() {
const data = _interopRequireDefault(require('v8'));

_v = function _v() {
return data;
};

return data;
}

function _vm() {
const data = _interopRequireDefault(require('vm'));

_vm = function _vm() {
return data;
};

return data;
}

function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {default: obj};
}

class _default {
constructor(value) {
if (this._isPrimitive(value)) {
throw new TypeError(
[
'Primitives cannot leak memory.',
'You passed a ' +
typeof value +
': <' +
(0, _prettyFormat().default)(value) +
'>'
].join(' ')
);
}

let weak;

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

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

weak(value, () => (this._isReferenceBeingHeld = false));
this._isReferenceBeingHeld = true; // Ensure value is not leaked by the closure created by the "weak" callback.

value = null;
}

isLeaking() {
this._runGarbageCollector();

return this._isReferenceBeingHeld;
}

_runGarbageCollector() {
const isGarbageCollectorHidden = !global.gc; // GC is usually hidden, so we have to expose it before running.

_v().default.setFlagsFromString('--expose-gc');

_vm().default.runInNewContext('gc')(); // The GC was not initially exposed, so let's hide it again.

if (isGarbageCollectorHidden) {
_v().default.setFlagsFromString('--no-expose-gc');
}
}

_isPrimitive(value) {
return value !== Object(value);
}
}

exports.default = _default;
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
r3nya marked this conversation as resolved.
Show resolved Hide resolved

'use strict';
Expand Down
1 change: 1 addition & 0 deletions packages/jest-leak-detector/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
},
"license": "MIT",
"main": "build/index.js",
"types": "build/index.d.ts",
"dependencies": {
"pretty-format": "^24.0.0"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ it('tests different objects', () => {
});

it('correctly checks more complex leaks', () => {
let ref1 = {};
let ref2 = {};
let ref1: any = {};
let ref2: any = {};

// Create a circular dependency between ref1 and ref2.
ref1.ref2 = ref2;
Expand Down
72 changes: 72 additions & 0 deletions packages/jest-leak-detector/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';
r3nya marked this conversation as resolved.
Show resolved Hide resolved

import v8 from 'v8';
import vm from 'vm';
import prettyFormat from 'pretty-format';

export default class {
_isReferenceBeingHeld: boolean;

constructor(value: any) {
r3nya marked this conversation as resolved.
Show resolved Hide resolved
if (this._isPrimitive(value)) {
throw new TypeError(
[
'Primitives cannot leak memory.',
'You passed a ' + typeof value + ': <' + prettyFormat(value) + '>',
].join(' '),
);
}

let weak;

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

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

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

// Ensure value is not leaked by the closure created by the "weak" callback.
value = null;
}

isLeaking(): boolean {
this._runGarbageCollector();

return this._isReferenceBeingHeld;
}

_runGarbageCollector() {
r3nya marked this conversation as resolved.
Show resolved Hide resolved
const isGarbageCollectorHidden = !global.gc;

// GC is usually hidden, so we have to expose it before running.
v8.setFlagsFromString('--expose-gc');
vm.runInNewContext('gc')();

// The GC was not initially exposed, so let's hide it again.
if (isGarbageCollectorHidden) {
v8.setFlagsFromString('--no-expose-gc');
}
}

_isPrimitive(value: any): boolean {
return value !== Object(value);
}
}
7 changes: 7 additions & 0 deletions packages/jest-leak-detector/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "build"
}
r3nya marked this conversation as resolved.
Show resolved Hide resolved
}