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

Test runs twice #1327

Closed
domq opened this issue Aug 29, 2014 · 23 comments
Closed

Test runs twice #1327

domq opened this issue Aug 29, 2014 · 23 comments

Comments

@domq
Copy link
Contributor

domq commented Aug 29, 2014

This gist produces the following output, where test 3 runs twice. (It actually does run twice, as can be observed with the process.stdout.writes; it's not just a reporting artifact)

child_process: customFds option is deprecated, use stdio instead.

✓ test 1 
1) test 1
This actually runs twice.
This actually runs twice.
2) test 3
3) test 3

1 passing (8ms)
3 failing

1)  test 1:
   Uncaught Error: Too bad
    at /Users/dom/Dev/JSRad/dupe-test.js:3:15
    at process._tickCallback (node.js:343:11)

2)  test 3:
   Error: done() invoked with non-Error: OUCH
    at Object.<anonymous> (/Users/dom/Dev/JSRad/node_modules/mocha.git/lib/runnable.js:223:25)
    at process._tickCallback (node.js:343:11)

3)  test 3:
   Error: done() invoked with non-Error: OUCH
    at Object.<anonymous> (/Users/dom/Dev/JSRad/node_modules/mocha.git/lib/runnable.js:223:25)
    at process._tickCallback (node.js:343:11)

This looks like it could be related with #660

@travisjeffery
Copy link
Contributor

your first test needs to use done:

it("test 1", function(done) {
    process.nextTick(function() {
        throw new Error("Too bad");
        done();
    });
});
it("test 2", function() {});
it("test 3", function(done) {
    process.stdout.write("This actually runs twice.\n");
    process.nextTick(done.bind({},"OUCH"));
});

  1) test 1
  ✓ test 2
This actually runs twice.
  2) test 3

  1 passing (5ms)
  2 failing

  1)  test 1:
     Uncaught Error: Too bad
      at /Users/tj/dev/mocha-test/test.js:5:15
      at process._tickCallback (node.js:419:13)

  2)  test 3:
     Error: done() invoked with non-Error: OUCH
      at Object.<anonymous> (/Users/tj/dev/mocha-test/node_modules/mocha/lib/runnable.js:222:25)
      at process._tickCallback (node.js:419:13)

@domq
Copy link
Contributor Author

domq commented Aug 29, 2014

2014-08-29 23:52 GMT+02:00 Travis Jeffery notifications@github.com:

your first test needs to use done:

Hi Travis, thanks for the quick reply.

I arrived to this gist by minimizing a much larger problematic case, and
indeed at this point pretty much any change will make the reported symptom
disappear. Still, it seems to me that it() test bodies should never run
twice (and whatever happens in the tests that run before them shouldn't
make a difference whether they do).

I'll work on the issue next week, but I'll need some time to get up to
speed with the mocha code base.

It's bedtime now in Europe, have a nice evening,

Dominique Quatravaux
dominique@quatravaux.org

@domq
Copy link
Contributor Author

domq commented Sep 1, 2014

Please pull from #1335 for a regression test and a fix.

jbnicolai pushed a commit that referenced this issue Sep 9, 2014
…ve a state.

Fixes #1327
Closes #1335

Signed-off-by: Joshua Appelman <jappelman@xebia.com>
@jbnicolai
Copy link

Thanks for reporting and fixing this!

tandrewnichols pushed a commit to tandrewnichols/mocha that referenced this issue Dec 15, 2014
…ve a state.

Fixes mochajs#1327
Closes mochajs#1335

Signed-off-by: Joshua Appelman <jappelman@xebia.com>
@ghost
Copy link

ghost commented May 6, 2015

This seems to be happening again.

@danielstjules
Copy link
Contributor

@DiegoAlberto do you have an example? The regression test for this issue is showing things are fine: #1686

@ghost
Copy link

ghost commented May 7, 2015

Hello, not really sure if this is the same error as the one I'm referring to:

mad-eye/meteor-mocha-web#166

Test being run twice on velocity mocha.

@rjcorwin
Copy link

I fixed this in my case by encapsulating my Mocha.run() in the following condition. I'm not sure why it does but...

    if (navigator.userAgent.indexOf('PhantomJS') === -1) {
      mocha.run();
    }

@rjcorwin
Copy link

Oooh I get it. mocha.run() in the yeoman generator-backbone project I'm using, mocha.run() is inserted dynamically while that mocha.run() there in my DOM is for when a browser that isn't PhantomJS hits the page. Without that if statement, mocha.run() would definitely be run twice which would cause all the it calls to run twice.

@yairEO
Copy link

yairEO commented Mar 14, 2016

@rjsteinert - didn't solve the problem for me when running tests though the HTML file in the browser. failed tests still appear twice :(

Mocha HTML:

<html>
    <head>
        <meta charset="utf-8">
        <link href="mocha.css" rel="stylesheet" />
        <link href="tests.css" rel="stylesheet" />
    </head>
    <body>
        <div id="mocha"></div>
        <script src="../node_modules/mocha/mocha.js"></script>
        <script src="../node_modules/chai/chai.js"></script>
        <script>
            mocha.setup('bdd');
            mocha.ui('bdd');
            var expect = chai.expect;
            var should = chai.should();
            var assert = chai.assert;
        </script>
        <script src="bin/test.js"></script>
        <script>
            if( navigator.userAgent.indexOf('PhantomJS') === -1 ){
                mocha.run();
            }
        </script>
    </body>
</html>

A failed test:

describe('Test', ()=> {
    it('Failed test', ()=>{
        '1'.should.equal(2);
    });
});

Failed tests shown twice... mocha@2.4.5

@sgnl
Copy link

sgnl commented Mar 25, 2016

I am experiencing the issue above.

Here is a js-fiddle, I am forcing a failing test to showcase that failing tests display twice:

https://jsfiddle.net/sgnl/wfyavarf/2/

^ above example is fixed with mocha 5: https://jsfiddle.net/wfyavarf/9/

@danielstjules
Copy link
Contributor

Duplicate error reports in the browser will be fixed via #2112

@brettz9
Copy link

brettz9 commented Oct 23, 2016

I'm seeing the test run twice in the browser despite being on Mocha 3.1.2.

@brettz9
Copy link

brettz9 commented Oct 23, 2016

Sorry, my mistake...

@BebeSparkelSparkel
Copy link

Any resolution for this?

@spolat
Copy link

spolat commented Dec 22, 2017

+1 getting same error.

@johnslay
Copy link

Came across this issue while trying to debug an issue I had with an amqplib. Turns out that I had an async action that would emit an error using Node's EventEmitter API. So I'm assuming the test had received the promise rejection plus the error that was never caught from the event emitter. I managed to get it to stop reproducing the test by implementing on('error') and handling the error on the event emitter. Hope this helps.

@BebeSparkelSparkel
Copy link

@johnslay Thanks for the tip! I'll try it

@meetDeveloper
Copy link

It was happening to me due to not passing done and calling it, but could not understand why was it run twice because of this.

@johnpc
Copy link

johnpc commented Feb 18, 2020

@meetDeveloper this just happened to me as well

@PranavBhatia
Copy link

Same failed test ran twice despite having done handled properly:
3) should update the attribute: awayMode of a device with: {"type":"action"}
4) should update the attribute: awayMode of a device with: {"type":"action"}

@kevinx701
Copy link

I got same issue, I only have one case.
#indexOf()
√ new test (1979ms)
#indexOf()
√ new test (4547ms)
2 passing (7s)

@resistdesign
Copy link

resistdesign commented Apr 18, 2020

For me the Glob path for spec was running/FINDING a test twice.
AND using QUOTING mattered.
I landed on using single quotes around my Glob path in my npm script: https://github.com/resistdesign/rdx/blob/refactor/2020/package.json#L30

"test:int": "mocha --inline-diffs --require resistdesign-babel-register --ui exports './src/**/*.integration.jsx'"

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

No branches or pull requests