adamgruber / mochawesome

A Gorgeous HTML/CSS Reporter for Mocha.js
https://gitter.im/mochawesome/general
MIT License
1.06k stars 160 forks source link

Use `mocha --opts` command to run tests, it is fail to genarate the report when assert fail #207

Open iqianxing opened 6 years ago

iqianxing commented 6 years ago

1.The test suite :

var assert = require("chai").assert;

describe("# Hello, world!", function () {
    it("1+1=2",function(){
        assert.equal(1+1,3);
        assert.ok('Hello, world!');
    })
});

2.mocha.opts :

--reporter mochawesome
--reporter-options reportDir=mochawesome-report,reportFilename=mocha-helloworld,reportTitle=mocha-helloworld,reportPageTitle="Helloworld-Report",overwrite=true
--timeout 200

3.run mocha command

>mocha --opts mocha.opts
  1. it is fail to genarate the report

    ## Hello, world!
    1) 1+1=2
    
    ## mysql test
    undefined
    √ query (144ms)
    
    ## https://www.github.com
    2) github
    
    1 passing (385ms)
    2 failing

    image

adamgruber commented 6 years ago

Hi @iqianxing, are you saying it works ok if your tests pass? I was unable to reproduce this issue with the information given. If you can supply a link to a repo where you can reproduce the issue, I look into this further.

iqianxing commented 6 years ago

It works fine when my tests pass. My project: https://github.com/iqianxing/nstarter
I run mocha --opts mocha.opts in my subforder like fellow: mochawesome image

adamgruber commented 6 years ago

I'm still unable to reproduce your issue using the files you provided. What version of Node are you running?

iqianxing commented 6 years ago
D:\Projects\>node -v
v6.11.2

D:\Projects\>npm -v
3.10.10
iqianxing commented 6 years ago

I have meet this problem on my windows 7 、Windows 10

adamgruber commented 6 years ago

I'm really not sure about this one. I cloned your repo onto a Windows 10 machine, running node v6. Ran the tests inside example-mocha-helloworld and a report was generated.

JeffML commented 6 years ago

My tests have been failing to generate reports since Dec. 11. Not sure of the cause. Node 6.11.2. Using: SERVER_UNDER_TEST="$1" mocha ./tests --recursive --slow 1500 --reporter mochawesome

JeffML commented 6 years ago

I can verify that when an assert is fired, I get no report. If all tests pass, then a report is generated. More details in a moment.

JeffML commented 6 years ago

Here we go; the second test fails and no report is generated. This is on a Mac, Sierra OS

mocha ./tests/bugTest --slow 1500 --reporter mochawesome

bugTest.js

var chai = require('chai');
var should = chai.should();

describe("Bug test", () => {
    var request = require('superagent')
        .agent();

    it("request initialized", done => {
        request.should.be.ok;
        done();
    });

    it("can get /test page", done => {
        var req = request.get('https://www.google.com');

        req
            .end((err, res) => {
                if (err) {
                    done(err);
                    return;
                }

                res.ok.should.be.ok;
                res.type.should.match(/application\/json/);
                res.body.NODE_ENV.should.be.oneOf(['development', 'production'])
                should.exist(res.headers.date)
                should.not.exist(res.headers.cookies);
                done();
            })
    });
});

npm ls -depth 0 ├── chai@4.1.2 ├── chai-subset@1.6.0 ├── lodash@4.17.4 ├── mocha@4.0.1 ├── mochawesome@3.0.0 ├── nano@6.4.2 └── superagent@3.8.1

Hope that helps.

adamgruber commented 6 years ago

@JeffML Thanks for the extra info. I'm still not sure what's happening here. I've been able to run your test code and generate a report.

screen shot 2017-12-15 at 10 12 40 am

JeffML commented 6 years ago

Could be some bug triggered by one of the global module versions. Here's mine:

npm ls -g -depth=0 /usr/local/lib ├── bower@1.3.5 ├── grunt-cli@0.1.13 ├── n@1.1.0 ├── node-fetch@1.7.3 ├── node-inspector@0.2.0-beta3 ├── nodemon@0.6.23 ├── npm@5.6.0 └── zurb-foundation@4.2.3

Things it's not: 1) not a file permissions error 2) not OS dependent

Other things it could be: 1) timing issue 2) program terminating before output is completed 3) undeclared variable (global assignment) 4) uncaught exception

I'm not having any problem reproducing this so far.

adamgruber commented 6 years ago

I created a repo to try to isolate this. I can clone this and run npm test and a report will be generated.

https://github.com/adamgruber/mochawesome-test

JeffML commented 6 years ago

I cloned your repo and the good news is: it doesn't work. I think that's good news: it eliminates it being a project issue.

  Bug test
    ✓ request initialized
    1) can get /test page

  1 passing (346ms)
  1 failing

This fails to generate a mochawesome-report folder

If I remove the second test by changing it to xit:

xit("can get /test page", done => {...

and run as before, the folder is generated and contains html and json files, along with an assets folder.

The only thing I can recall is that I recently upgraded my npm package. I might have upgraded mochawesome about that time, too. Could it be there's a bug in npm install?

adamgruber commented 6 years ago

What if the failing test is a more simple example and not one that requires the done callback? Does that generate a report? I'm wondering if there's an issue with making requests though it feels like a stab in the dark.

JeffML commented 6 years ago

Switched over to my Windows 10 box and running into different issues. I don't think I've tried running mocha on a PC before. If I just do an npm install and try running mocha, mocha is not recognized. Same if I do an npm run test. If I install mocha globally, then I get this:

PS C:\Users\jlowe\OneDrive\Documents\workspace\mochawesome-test> npm test

> mochawesome-test@1.0.0 test C:\Users\jlowe\OneDrive\Documents\workspace\mochawesome-test
> mocha test.js --reporter mochawesome

"mochawesome" reporter not found
C:\Users\jlowe\AppData\Roaming\npm\node_modules\mocha\lib\mocha.js:174
      throw new Error('invalid reporter "' + reporter + '"');
      ^

Error: invalid reporter "mochawesome"
    at Mocha.reporter (C:\Users\jlowe\AppData\Roaming\npm\node_modules\mocha\lib\mocha.js:174:13)
    at Object.<anonymous> (C:\Users\jlowe\AppData\Roaming\npm\node_modules\mocha\bin\_mocha:220:7)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Function.Module.runMain (module.js:676:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3
npm ERR! Test failed.  See above for more details.

Anyway, I'll go back to my Mac and see if I can simplify the test further.

JeffML commented 6 years ago

Okay, I played around with this a bit more on my PC. --save-dev mocha doesn't work (mocha not recognized), but --save mocha does. Now the test runs, mochawesome works, but there is ELIFECYCLE error:

npm run test

> mochawesome-test@1.0.0 test C:\Users\jlowe\OneDrive\Documents\workspace\mochawesome-test
> mocha test.js --reporter mochawesome

  Bug test
    √ request initialized
    1) can get /test page

  1 passing (190ms)
  1 failing

  1) Bug test
       can get /test page:
     Uncaught AssertionError: expected 'text/html' to match /application\/json/
      at req.end (test.js:24:33)
      at Request.callback (node_modules\superagent\lib\node\index.js:706:12)
      at Stream.parser (node_modules\superagent\lib\node\index.js:906:18)
      at Unzip.unzip.on (node_modules\superagent\lib\node\unzip.js:55:12)
      at endReadableNT (_stream_readable.js:1056:12)
      at _combinedTickCallback (internal/process/next_tick.js:138:11)
      at process._tickCallback (internal/process/next_tick.js:180:9)

[mochawesome] Report JSON saved to C:\Users\jlowe\OneDrive\Documents\workspace\mochawesome-test\mochawesome-report\mochawesome.json

[mochawesome] Report HTML saved to C:\Users\jlowe\OneDrive\Documents\workspace\mochawesome-test\mochawesome-report\mochawesome.html

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! mochawesome-test@1.0.0 test: `mocha test.js --reporter mochawesome`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the mochawesome-test@1.0.0 test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\jlowe\AppData\Roaming\npm-cache\_logs\2017-12-16T19_05_18_720Z-debug.log

the log:

cat C:\Users\jlowe\AppData\Roaming\npm-cache\_logs\2017-12-16T19_05_18_720Z-debug.log
0 info it worked if it ends with ok
1 verbose cli [ 'C:\\Program Files\\nodejs\\node.exe',
1 verbose cli   'C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli   'run',
1 verbose cli   'test' ]
2 info using npm@5.5.1
3 info using node@v8.9.3
4 verbose run-script [ 'pretest', 'test', 'posttest' ]
5 info lifecycle mochawesome-test@1.0.0~pretest: mochawesome-test@1.0.0
6 info lifecycle mochawesome-test@1.0.0~test: mochawesome-test@1.0.0
7 verbose lifecycle mochawesome-test@1.0.0~test: unsafe-perm in lifecycle true

8 verbose lifecycle mochawesome-test@1.0.0~test: PATH: C:\Program Files\nodejs\node_modules\npm\bin\node-gyp-bin;C:\Users\jlowe\OneDrive\Documents\workspace\
mochawesome-test\node_modules\.bin;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\Sy
stem32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R
) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine C
omponents\IPT;C:\Program Files\Git\cmd;C:\Program Files\nodejs\;C:\Users\jlowe\AppData\Local\Microsoft\WindowsApps;C:\Users\jlowe\AppData\Roaming\npm
9 verbose lifecycle mochawesome-test@1.0.0~test: CWD: C:\Users\jlowe\OneDrive\Documents\workspace\mochawesome-test
10 silly lifecycle mochawesome-test@1.0.0~test: Args: [ '/d /s /c', 'mocha test.js --reporter mochawesome' ]
11 silly lifecycle mochawesome-test@1.0.0~test: Returned: code: 1  signal: null
12 info lifecycle mochawesome-test@1.0.0~test: Failed to exec test script
13 verbose stack Error: mochawesome-test@1.0.0 test: `mocha test.js --reporter mochawesome`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\index.js:280:16)
13 verbose stack     at emitTwo (events.js:126:13)
13 verbose stack     at EventEmitter.emit (events.js:214:7)
13 verbose stack     at ChildProcess.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\lib\spawn.js:55:14)
13 verbose stack     at emitTwo (events.js:126:13)
13 verbose stack     at ChildProcess.emit (events.js:214:7)
3 verbose stack     at maybeClose (internal/child_process.js:925:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:209:5)
14 verbose pkgid mochawesome-test@1.0.0
15 verbose cwd C:\Users\jlowe\OneDrive\Documents\workspace\mochawesome-test
16 verbose Windows_NT 10.0.15063
17 verbose argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "test"
18 verbose node v8.9.3
19 verbose npm  v5.5.1
20 error code ELIFECYCLE
21 error errno 1
22 error mochawesome-test@1.0.0 test: `mocha test.js --reporter mochawesome`
22 error Exit status 1
23 error Failed at the mochawesome-test@1.0.0 test script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]

I'm really confused now, though. I just now reinstalled mocha globally and mocha test --reporter mochawesome works now. No errors. Report files are generated. Again, this is on my PC. If I delete the report folder and rerun, it works also.

(sorry for the running commentary, but trying things) If I leave mocha installed globally, but remove it locally, then the Error: invalid reporter "mochawesome" error comes back. Maybe you understand that but I don't. If I reinstall mocha using --save-dev, mocha installs but the test fails again with the same error. If I remove mocha, and reinstall using --save, everything works.

On my PC, I am running Node 8.9.3 and npm 5.5.1.

JeffML commented 6 years ago

And after playing around with NODE_ENV in powershell, I can't get mochawesome to fail, no matter if I install mocha with --save or --save-dev, with NODE_ENV=production or =development.

JeffML commented 6 years ago

Now I am unable to reproduce this (using the test project) on my Mac. I am hoping the debug log provided some useful info.

JeffML commented 6 years ago

Had another test box (Mac OS) where this was failing to generate reports. Go it working, but still not quite sure how. Things I did:

1) upgraded my npm from 2.3.x to 2.15.x 2) removed and reinstalled mocha and mochawesome, --save-dev 3) removed and reinstall mocha globally (--save)

Before this, when I ran mocha from the command line, it would not find the mochawesome reporter; now it does (mocha tests/bugTest.js --reporter mochawesome). If I run 'npm test', it works. If I try 'npm run test', I get the ELIFECYCLE error mentioned in previous comment.

That's kind of a mess of clues, but if you revert to an earlier instance of npm, then remove and reinstall mocha, I bet you can reproduce this.

adamgruber commented 6 years ago

I'm not too concerned about the ELIFECYCLE error. I believe that's coming from the failed test and not indicative of any issue with the reporter.

RaspFR commented 6 years ago

Hi all, I'm experiencing the same issue.

and if all the tests pass, the report is generated. If one or more tests fail, the test report fail ! with the following error :

`There was an error while starting the test runner:

TypeError: test.titlePath is not a function at C:\saagie\ta\satui\node_modules\mocha\lib\reporters\base.js:227:10 at Array.forEach (native) at Function.exports.list (C:\saagie\ta\satui\node_modules\mocha\lib\reporters\base.js:167:12) at Spec.Base.epilogue (C:\saagie\ta\satui\node_modules\mocha\lib\reporters\base.js:344:10) at emitNone (events.js:91:20) at Runner.emit (events.js:185:7) at C:\Users\Julien\AppData\Roaming\npm\node_modules\nightwatch\node_modules\mocha-nightwatch\lib\overrides\runner.js:40:12 at C:\Users\Julien\AppData\Roaming\npm\node_modules\nightwatch\node_modules\mocha-nightwatch\lib\runner.js:661:9 at next (C:\Users\Julien\AppData\Roaming\npm\node_modules\nightwatch\node_modules\mocha-nightwatch\lib\runner.js:295:14) at Immediate. (C:\Users\Julien\AppData\Roaming\npm\node_modules\nightwatch\node_modules\mocha-nightwatch\lib\runner.js:339:5)`

adamgruber commented 6 years ago

@RaspFR That error is coming from the base mocha reporter. What happens if you switch reporters?

RaspFR commented 6 years ago

If I'm using the "list" or "spec" reporter for instance, everything works fine.

RaspFR commented 6 years ago

so when there is test fails with mochawesome, it seems to fail here in the base.js file. the issue is related to the test.titlePath().forEach() :


    //indented test title
    var testTitle = '';
    test.titlePath().forEach(function (str, index) {
      if (index !== 0) {
        testTitle += '\n     ';
      }
      for (var i = 0; i < index; i++) {
        testTitle += '  ';
      }
      testTitle += str;
    });
JeffML commented 6 years ago

Would it have something to do with custom title? Here's how I'm launching my tests in bash:

echo "MOCHA!"

if [[ $# -ne 2 ]]; then
  echo "Usage: $0 URL NICKNAME" >&2
  exit 1
fi

echo URL of server is $1

NOW=$(date +"%m-%d-%Y %r")
NOW_STR="Mocha tests (${NOW})"

export MOCHAWESOME_REPORTTITLE="${NOW_STR}"

SERVER_UNDER_TEST="$1" mocha ./tests --recursive --slow 1500 --reporter mochawesome

However, the problem has now gone away...

RaspFR commented 6 years ago

I've added some test title options in my json file but I'm still facing the issue :'(. @adamgruber Any hint ?


"test_runner" : {
    "type" : "mocha",
    "options" : {
      "ui" : "bdd",
      "reporter" : "mochawesome",
      "reporterOptions": {
        "reportTitle": "My UI Tests",
        "reportPageTitle": "MY REPORT"
      }
    }
  }, 
philaw commented 6 years ago

I had problem maybe related, the report generated, but in console, it says: [mochawesome] No files were generated

philaw commented 6 years ago

I am not sure this is the solution, but what I did to make it work was downgrade node from 10 to 8.

stale[bot] commented 5 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.