Giter Site home page Giter Site logo

Jest test suite leaking memory about nestjs HOT 4 CLOSED

mikro-orm avatar mikro-orm commented on September 2, 2024
Jest test suite leaking memory

from nestjs.

Comments (4)

B4nan avatar B4nan commented on September 2, 2024

Yes, it is leaking, because for each test you are bootstrapping the application, but you never tear it down. Add this to the test file and the leak is gone:

  afterEach(async () => {
    await app.close();
  });

from nestjs.

Langstra avatar Langstra commented on September 2, 2024

Oh yea, forgot to add that in the reproduction repository. However, even after adding it the memory leak remains.

from nestjs.

Langstra avatar Langstra commented on September 2, 2024

I see that is responds to the onApplicationShutdown hook, but maybe it should be used in beforeApplicationShutdown? Since onApplicationShutdown will run after app.close resolves?

Also maybe enableShutdownHooks need to be enabled as they are by default disabled?

from nestjs.

B4nan avatar B4nan commented on September 2, 2024

Oh I see, it was more of a random "pass". Anyway, the "detect leaks" feature is experimental, for me it never worked correctly. See here for example:

jestjs/jest#5234

I see that is responds to the onApplicationShutdown hook, but maybe it should be used in beforeApplicationShutdown? Since onApplicationShutdown will run after app.close resolves?

Also maybe enableShutdownHooks need to be enabled as they are by default disabled?

This is based on the @nestjs/typeorm module, so I'd say that should be correct when it's done by the author of nest.

https://github.com/nestjs/typeorm/blob/master/lib/typeorm-core.module.ts#L102

Removing the onApplicationShutdown bit and explicitly calling orm.close() does not help. But I really believe this is more of a jest failure than actual leak. When I tried to duplicate the test 8 times and allowed garbage collection and heap logging, I can see the memory usage is consistent:

➜  nest-mikro-orm-jest-leak git:(master) ✗ node --expose-gc ./node_modules/.bin/jest --runInBand --logHeapUsage --config ./test/jest-e2e.json
 PASS  test/app1.e2e-spec.ts (62 MB heap size)
 PASS  test/app4.e2e-spec.ts (75 MB heap size)
 PASS  test/app5.e2e-spec.ts (76 MB heap size)
 PASS  test/app3.e2e-spec.ts (76 MB heap size)
 PASS  test/app7.e2e-spec.ts (76 MB heap size)
 PASS  test/app6.e2e-spec.ts (76 MB heap size)
 PASS  test/app2.e2e-spec.ts (76 MB heap size)
 PASS  test/app.e2e-spec.ts (76 MB heap size)

from nestjs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.