cypress: Incorrect JSON coding format after mochawesome-merge

Current behavior:

about : https://github.com/cypress-io/cypress/issues/5103

Steps to reproduce: (app code and test code)

Although there were errors in the log , but the result is generated. You can get two .json files in 'mochawesome-report '.

Then merge two .json into one to generate in the root directory.

PS C:\1_Code\Atest> npx mochawesome-merge  > mochawesome.json

Finally, run the command to generate report failed :

PS C:\1_Code\Atest2>  npx mochawesome-report-generator mochawesome.json

npx: installed 50 in 5.759s
✘ Some files could not be processed:
mochawesome.json
  Unexpected token � in JSON at position 0

I found that the JSON encoding format after merge was incorrect, and I didn’t want to change it manually. I needed to integrate it into automated deployment and test scripts.

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Comments: 18 (1 by maintainers)

Most upvoted comments

If anyone is still struggling with this issue, the solution can be found here: https://www.npmjs.com/package/mochawesome-merge

you need to use the following syntax now to avoid the JSON coding format issue: npx mochawesome-merge mochawesome-report/.*json -o mochawesome.json

the below syntax is technically legacy, but I still seem to run into the issue mentioned here when i use it, so do not use this: npx mochawesome-merge mochawesome-report/*.json > mochawesome.json

In case this is helpful for anyone, I ran into the same issue as the original summary and looks like the issue was related to how Powershell encodes the output to file:

mochawesome-merge .\report\*.json > .\merged.json # doesn't work

For me the fix was to instead use something as follows to explicitly use ASCII:

mochawesome-merge .\report\*.json | out-file -encoding ascii .\merged.json

Guys, I found the best solution of this problem. With this solution, we don’t have to do much with the configurations or setup.

Solution is the “cypress-mochawesome-reporter”.

It will handle all the corner cases which we were facing with generating mochawesome report, then merge and then marge to consolidate all the jsons. Steps to do:

  1. install cypress-mochawesome-reporter
  2. add below line in your configuration file: “reporter”: “cypress-mochawesome-reporter”
  3. Add import 'cypress-mochawesome-reporter/register' to index.js inside support folder
  4. run your all the test files(.js files)
  5. at last, npm run generate-mochawesome-report (You can add above line to scripts of your package.json file)

It will create a folder results/html where you can find your consolidated report named “mochasome.html”

Just try it, you will have a smile while looking into the consolidate report with all ease. Here is the bonus, you will also be able to see screenshots.

Hope, it will help you.

@mustaf19 did the solution @mflynn001 shared work for you?

I had same issue with Unexpected token while trying to create the report on Windows 10. Here is my solution on another issue: https://github.com/Antontelesh/mochawesome-merge/issues/6#issuecomment-627496076

@maximumsnow Add following lines of code to your package.json file under scripts tag.

"scripts": {
  "delete-mochawesome-folder":"rm -f mochawesome-report/*.json",
  "e2e_mochawesome": "yarn cypress run --spec cypress/integration/Tests/*.js",
  "merge-json":"npx mochawesome-merge --reportDir mochawesome-report > mochawesome1.json",
  "html-generator": " npx mochawesome-report-generator mochawesome1.json"
}
  1. Make sure you have installed
npm install --save-dev mocha mochawesome mochawesome-merge mochawesome-report-generator
  1. Make sure all the previous .json files are removed from the mochawesome-report folder as well as from cypress main folder.

Now execute above commands in sequence and here you go 😉

image

I believe I’m seeing the same issue. I added comments to issue #5103

I’m not going to duplicate them here.