Skip to content

Instantly share code, notes, and snippets.

@FrankHassanabad
Last active November 9, 2021 00:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FrankHassanabad/68cef0d806f8cd9b2af50fa815af92c1 to your computer and use it in GitHub Desktop.
Save FrankHassanabad/68cef0d806f8cd9b2af50fa815af92c1 to your computer and use it in GitHub Desktop.
actionsClientMock leaking memory
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
// Importing like this won't leak memory
// import { actionsClientMock } from './actions_client.mock';
// Importing like this will leak memory as it's a more broad import/export with an issue somewhere
import { actionsClientMock } from './mocks';
describe('test', () => {
afterEach(() => {
jest.clearAllMocks();
jest.resetAllMocks();
jest.restoreAllMocks();
jest.resetModules();
});
test('test', () => {
// If you expose the garbage collector, run it before all the tests
if (global.gc) {
global.gc();
}
// We need to just do _something_ with the object in order to leak memory. We don't
// need to call actionsClientMock.create(); as that's not where the memory leak is. It's within the
// import/export somewhere.
Object.keys(actionsClientMock);
// You actually don't have to call into this below. You just have to touch the object or use the object for the memory leak to happen
// const actionsClient = actionsClientMock.create();
expect(true).toEqual(true);
});
});
@FrankHassanabad
Copy link
Author

FrankHassanabad commented Nov 9, 2021

Run the copy_tests.sh from: https://gist.github.com/FrankHassanabad/ce8353ae0519cbb2cdf38a9e279da1c9 to see it leaking

E.x.:

./copy_tests.sh ~/projects/kibana/x-pack/plugins/actions/server/sample.test.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment