mochawesome icon indicating copy to clipboard operation
mochawesome copied to clipboard

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

Open iqianxing opened this issue 7 years ago • 29 comments

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

iqianxing avatar Nov 12 '17 16:11 iqianxing

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.

adamgruber avatar Nov 13 '17 21:11 adamgruber

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

iqianxing avatar Nov 22 '17 17:11 iqianxing

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

adamgruber avatar Nov 28 '17 21:11 adamgruber

D:\Projects\>node -v
v6.11.2

D:\Projects\>npm -v
3.10.10

iqianxing avatar Nov 30 '17 08:11 iqianxing

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

iqianxing avatar Nov 30 '17 09:11 iqianxing

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.

adamgruber avatar Nov 30 '17 16:11 adamgruber

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 avatar Dec 14 '17 17:12 JeffML

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 avatar Dec 14 '17 17:12 JeffML

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 ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] └── [email protected]

Hope that helps.

JeffML avatar Dec 14 '17 17:12 JeffML

@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

adamgruber avatar Dec 15 '17 15:12 adamgruber

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

npm ls -g -depth=0 /usr/local/lib ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] ├── [email protected] └── [email protected]

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.

JeffML avatar Dec 15 '17 18:12 JeffML

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

adamgruber avatar Dec 15 '17 20:12 adamgruber

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?

JeffML avatar Dec 16 '17 01:12 JeffML

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.

adamgruber avatar Dec 16 '17 03:12 adamgruber

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

> [email protected] 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 avatar Dec 16 '17 18:12 JeffML

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

> [email protected] 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! [email protected] test: `mocha test.js --reporter mochawesome`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] 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 [email protected]
3 info using [email protected]
4 verbose run-script [ 'pretest', 'test', 'posttest' ]
5 info lifecycle [email protected]~pretest: [email protected]
6 info lifecycle [email protected]~test: [email protected]
7 verbose lifecycle [email protected]~test: unsafe-perm in lifecycle true

8 verbose lifecycle [email protected]~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 [email protected]~test: CWD: C:\Users\jlowe\OneDrive\Documents\workspace\mochawesome-test
10 silly lifecycle [email protected]~test: Args: [ '/d /s /c', 'mocha test.js --reporter mochawesome' ]
11 silly lifecycle [email protected]~test: Returned: code: 1  signal: null
12 info lifecycle [email protected]~test: Failed to exec test script
13 verbose stack Error: [email protected] 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 [email protected]
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 [email protected] test: `mocha test.js --reporter mochawesome`
22 error Exit status 1
23 error Failed at the [email protected] 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 avatar Dec 16 '17 19:12 JeffML

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 avatar Dec 16 '17 19:12 JeffML

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 avatar Dec 16 '17 23:12 JeffML

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.

JeffML avatar Dec 18 '17 19:12 JeffML

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.

adamgruber avatar Dec 18 '17 19:12 adamgruber

Hi all, I'm experiencing the same issue.

  • I've installed mocha as de depenency (npm install mocha --save) in my nightwatchjs test solution. (nightwatchjs 0.9.19) : "dependencies": { "mocha": "^4.0.1", "mochawesome": "^3.0.0", "nightwatch": "^0.9.19", },
  • I added the mocha runner in the nightwatch.json : "test_runner" : { "type" : "mocha", "options" : { "ui" : "bdd", "reporter" : "mochawesome" } },

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)`

RaspFR avatar Dec 28 '17 09:12 RaspFR

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

adamgruber avatar Dec 28 '17 14:12 adamgruber

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

RaspFR avatar Dec 29 '17 10:12 RaspFR

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;
    });

RaspFR avatar Dec 29 '17 10:12 RaspFR

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...

JeffML avatar Dec 29 '17 21:12 JeffML

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"
      }
    }
  }, 

RaspFR avatar Jan 03 '18 14:01 RaspFR

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

philaw avatar May 14 '18 07:05 philaw

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

philaw avatar May 14 '18 07:05 philaw

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.

stale[bot] avatar May 12 '19 13:05 stale[bot]