I am currently facing a memory leak issue in my code and I am attempting to troubleshoot it by generating a heap snapshot using the Nodes v8 library. I am developing an endpoint in typescript with express that can be invoked to retrieve a JSON object, which can then be saved to a file and imported into chrome dev tools.
However, when I execute my jest test to check the functionality of the endpoint, it gets stuck indefinitely. I am running
jest --runInBand InfoController.test.ts
InfoController.ts
import { Router } from 'express';
import { getHeapSnapshot } from 'v8';
import { constants } from 'http2';
export const InfoController = (): Router => {
const router = Router();
router.get('/heapdump', (_, res) => {
console.log('requesting heap snapshot')
const heapShot = getHeapSnapshot();
console.log('heap');
let data = '';
heapShot.on('readable', () => {
const chunk = heapShot.read();
while (chunk !== null) {
data += chunk;
}
});
heapShot.on('end', () => res.status(constants.HTTP_STATUS_OK).json(JSON.parse(data)));
});
return router;
};
InfoController.test.ts
import express from 'express';
import { constants as httpConstants } from 'http2';
import Request from 'supertest';
import { InfoController } from './InfoController';
describe('🧪 InfoController', () => {
describe('GET /heapdump', () => {
test('should be able to retrieve a v8 heapdump of the service', async () => {
const controller = InfoController();
const app = express();
app.use(express.json());
app.use(controller);
const result = await Request(app).get('/heapdump').expect(httpConstants.HTTP_STATUS_OK);
console.log(result.body);
});
});
});
jest.config.js
module.exports = {
preset: 'ts-jest',
bail: true,
verbose: true,
testEnvironment: 'node',
collectCoverage: false,
testMatch: ['**/**/*.test.ts'],
testPathIgnorePatterns: ['<rootDir>/node_modules/', '<rootDir>/build/'],
collectCoverageFrom: ['<rootDir>/src/**', '!<rootDir>/src/index.ts'],
coveragePathIgnorePatterns: ['<rootDir>/node_modules', '<rootDir>/__tests__'],
globalSetup: '<rootDir>/__tests__/global/setup.ts',
};
This is the output I am seeing
$ jest --runInBand src/http/controllers/InfoController.test.ts
console.log
requesting heap snapshot
at src/http/controllers/InfoController.ts:8:13
RUNS src/http/controllers/InfoController.test.ts
Following this, it just hangs indefinitely without completion ????