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

Testing code with promises #30

Closed
karpikpl opened this issue Sep 26, 2018 · 3 comments
Closed

Testing code with promises #30

karpikpl opened this issue Sep 26, 2018 · 3 comments
Labels

Comments

@karpikpl
Copy link

Hey,
I'm trying to test code that uses promises and always getting the MemoryLeakError.
Is there anything I'm doing wrong, or is that just how promises work?

here's my code:

const Leakage =  require('leakage');

it('leak test', async() => {
    let i = 0;
    jasmine.DEFAULT_TIMEOUT_INTERVAL = 100000;
    expect.hasAssertions();

     await Leakage.iterate.async(async () => {
        let dummyPromise = new Promise((resolve) => {
           setTimeout(() => resolve('ok ' + ++i), 1);
        });
        
        try {
            const result = await dummyPromise;
            expect(result).toBeTruthy();
        } catch (e) {
            throw 'this should not happen ' + e;
        }
    });
})

and very minimalistic package.json

{
  "name": "promise-leak",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
      "test": "jest --runInBand"
  },
  "license": "MIT",
  "dependencies": {
    "jest": "^23.6.0",
    "leakage": "^0.4.0"
  }
}
@andywer
Copy link
Owner

andywer commented Sep 27, 2018

Hey @karpikpl!

Your code looks fine so far. Haven't ever tested the lib with Jasmine, though.

Can you please post the exact error output and ideally a heap dump (maybe in a GitHub gist or so)?

@karpikpl
Copy link
Author

So I have good news and bad news.
Good news is that the test is passing with mocha but failing with jest.

Same test, just different testing framework:

const Leakage =  require('leakage');
var assert = require('assert');

describe('leaks', function() {

    let expected, actual;

    beforeEach(function() {
        expected = 0;
        actual = 0;
    });

    afterEach(function() {
        if (!expected || expected == actual) return;
          var err = new Error('expected ' + expected + ' assertions, got ' + actual);
          this.currentTest.emit('error', err);
    });

    it('leak test', function() {
        this.timeout(100000);
        expected = 181;

         return Leakage.iterate.async(async () => {
            let dummyPromise = new Promise((resolve) => {
               setTimeout(() => resolve('ok'), 1);
            });

            try {
                const result = await dummyPromise;
                assert.equal(result, 'ok');
                actual++;
            } catch (e) {
                throw 'this should not happen ' + e;
            }
        });
    })
});

With mocha it passes and it's also much faster - 5s.

With Jest it fails but it's also much slower

$ jest --runInBand
 FAIL  tests/test.js (26.959s)
  ● leak test

    MemoryLeakError: Heap grew on 6 subsequent garbage collections (180 of 180 iterations) by 362 kB.

      Iterations between GCs: 30

      Final GC details:
      [   30.3 kB] [+ 378x] [-   2x] system / Context
      [   23.4 kB] [+ 378x] [-   1x] Closure
      [   21.9 kB] [+ 103x] [-   4x] Array
      [    3.6 kB] [+  31x] [-   1x] Timeout
      ... (9 more)

      at testConstantHeapSize (node_modules/leakage/lib/testConstantHeapSize.js:24:12)
      at Object.onAllDone (node_modules/leakage/lib/index.js:130:27)
      at Immediate.onHeapDiff (node_modules/leakage/lib/index.js:114:16)

I was unable to pass the --heap-file heap-diff.json param to jest. It worked with mocha, but mocha doesn't have the issue

@andywer
Copy link
Owner

andywer commented Sep 29, 2018

Thanks for the comprehensive update! 👍🙂

I suppose we can turn that into one actionable only: Document that you should use tape or mocha; test runners that come with a minimal overhead and don‘t do much magic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants