mochajs / mocha

☕️ simple, flexible, fun javascript test framework for node.js & the browser
https://mochajs.org
MIT License
22.55k stars 3.01k forks source link

🐛 Bug: --check-leaks only detects leak the first time (e.g. when running twice with `--watch`) #1948

Open fatso83 opened 8 years ago

fatso83 commented 8 years ago

I have the following bit of code, for which mocha correctly detects a leak:

describe('leak detection', function() {

    function testMe(){
        x = 123; // here I add 'var ' to make the test pass
    }

    it('should not leak', function() {
        testMe();
    });
});

By substituting 'x' for 'var x' the test passes. Seemingly, the same applies when running mocha with the --watch task, but when I remove the var statement the test still passes without any error about leaking. This is a bug.

$ mocha --check-leaks --watch

  leak detection
    ✓ should not leak
    1) should not leak

  1 passing (13ms)
  1 failing

  1) leak detection should not leak:
     Error: global leak detected: x

  leak detection
    ✓ should not leak

  1 passing (1ms)

  leak detection
    ✓ should not leak

  1 passing (2ms)

Using mocha 2.3.3 on Node 4.2.1

stale[bot] commented 6 years ago

I am a bot that watches issues for inactivity. This issue hasn't had any recent activity, and I'm labeling it stale. In 14 days, if there are no further comments or activity, I will close this issue. Thanks for contributing to Mocha!

ScottFreeCode commented 6 years ago

This behavior is still present; doesn't have anything to do with adding or removing var, it's just that after the assignment of the global in the first run, subsequent runs don't detect the assignment because Mocha's global leak detection specifically ignores globals that were already there before the test started.

I don't know if there's a reasonable fix for that, however.

anpaul0615 commented 4 years ago

I think adding reset logic (always set to first runtime global variables) to the lib\cli\watch-run.js will be able to detect it properly every run on watch mode.