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

Incorrect JSON coding format after mochawesome-merge #5111

Open
AliceShi789654 opened this issue Sep 10, 2019 · 18 comments
Open

Incorrect JSON coding format after mochawesome-merge #5111

AliceShi789654 opened this issue Sep 10, 2019 · 18 comments
Labels
E2E Issue related to end-to-end testing existing workaround topic: reporters 📄 Triaged Issue has been routed to backlog. This is not a commitment to have it prioritized by the team.

Comments

@AliceShi789654
Copy link

AliceShi789654 commented Sep 10, 2019

Current behavior:

about : #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.

@AliceShi789654
Copy link
Author

AliceShi789654 commented Sep 10, 2019

I have updated the sample code and now it runs cypress:run successfully: https://github.com/AliceShi789654/Atest. But mochawesome-report-generator was still failed. Thanks.

PS C:\1_Code\Atest2\Atest> npm install

> cypress@3.4.1 postinstall C:\1_Code\Atest2\Atest\node_modules\cypress
> node index.js --exec install


Cypress 3.4.1 is installed in C:\Users\hshi\AppData\Local\Cypress\Cache\3.4.1

npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN eslint-plugin-chai-friendly@0.4.1 requires a peer of eslint@>=3.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN eslint-plugin-cypress@2.6.1 requires a peer of eslint@>= 3.2.1 but none is installed. You must install peer dependencies yourself.
npm WARN Atest No description
npm WARN Atest No repository field.
npm WARN Atest No license field.

added 283 packages from 571 contributors and audited 619 packages in 26.22s
found 0 vulnerabilities

PS C:\1_Code\Atest2\Atest> npm run cypress:run

> @ cypress:run C:\1_Code\Atest2\Atest
> cypress run



====================================================================================================

  (Run Starting)

  ┌────────────────────────────────────────────────────────────────────────────────────────────────┐
  │ Cypress:    3.4.1                                                                              │
  │ Browser:    Electron 61 (headless)                                                             │
  │ Specs:      2 found (demo_baidu.ts, sample-spec.ts)                                            │
  └────────────────────────────────────────────────────────────────────────────────────────────────┘


────────────────────────────────────────────────────────────────────────────────────────────────────

  Running: demo_baidu.ts...                                                                (1 of 2)


  My first test case for cypress
    √ visit baidu home page and search for testerhome: (7311ms)


  1 passing (11s)

[mochawesome] Report JSON saved to C:\1_Code\Atest2\Atest\mochawesome-report\mochawesome.json


  (Results)

  ┌─────────────────────────────┐
  │ Tests:        1             │
  │ Passing:      1             │
  │ Failing:      0             │
  │ Pending:      0             │
  │ Skipped:      0             │
  │ Screenshots:  1             │
  │ Video:        true          │
  │ Duration:     10 seconds    │
  │ Spec Ran:     demo_baidu.ts │
  └─────────────────────────────┘


  (Screenshots)

  - C:\1_Code\Atest2\Atest\cypress\screenshots\demo_baidu.ts\My first test case for cypress -- visit baidu home page and search for testerhome.png (1000x1393)


  (Video)

  - Started processing:   Compressing to 32 CRF
  - Finished processing:  C:\1_Code\Atest2\Atest\cypress\videos\demo_baidu.ts.mp4 (2 seconds)


────────────────────────────────────────────────────────────────────────────────────────────────────
                                                                                                    
  Running: sample-spec.ts...                                                               (2 of 2) 


  My first test case for cypress
    √ Does not match! (44ms)


  1 passing (122ms)

[mochawesome] Report JSON saved to C:\1_Code\Atest2\Atest\mochawesome-report\mochawesome_001.json


  (Results)

  ┌──────────────────────────────┐
  │ Tests:        1              │
  │ Passing:      1              │
  │ Failing:      0              │
  │ Pending:      0              │
  │ Skipped:      0              │
  │ Screenshots:  0              │
  │ Video:        true           │
  │ Duration:     0 seconds      │
  │ Spec Ran:     sample-spec.ts │
  └──────────────────────────────┘


  (Video)

  - Started processing:   Compressing to 32 CRF
  - Finished processing:  C:\1_Code\Atest2\Atest\cypress\videos\sample-spec.ts.mp4 (0 seconds)


====================================================================================================

  (Run Finished)


      Spec                                                Tests  Passing  Failing  Pending  Skipped
  ┌────────────────────────────────────────────────────────────────────────────────────────────────┐
  │ √ demo_baidu.ts                             00:10        1        1        -        -        - │
  ├────────────────────────────────────────────────────────────────────────────────────────────────┤
  │ √ sample-spec.ts                             98ms        1        1        -        -        - │
  └────────────────────────────────────────────────────────────────────────────────────────────────┘
    All specs passed!                           00:10        2        2        -        -        -

PS C:\1_Code\Atest2\Atest> npx mochawesome-merge > mochawesome.json
PS C:\1_Code\Atest2\Atest> npx mochawesome-report-generator mochawesome.json
npx: installed 50 in 6.986s

✘ Some files could not be processed:
mochawesome.json
  Unexpected token � in JSON at position 0
PS C:\1_Code\Atest2\Atest>

@maximumsnow
Copy link

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

I'm not going to duplicate them here.

@HareemManzoor
Copy link

Getting same issue.
image
@jennifer-shehane

@HareemManzoor
Copy link

HareemManzoor commented Oct 1, 2019

@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

@prudhvidandamudi
Copy link

facing same issue, anyone has any solution??

image

@HareemManzoor
Copy link

@prudhvidandamudi please make sure that you are removing all the previous json reports. Also can you please the folder structure? Just want to make sure that you have placed your json report in correct folder. Thanks in advance :)

@mylearning1005
Copy link

mylearning1005 commented Apr 16, 2020

I am getting the following error : Can some one please help

PS C:\Cypress_Practice_tests\UITest> npx mochawesome-report-generator mochawesome.json
npx: installed 48 in 6.446s

✘ Some files could not be processed:
mochawesome.json
  Unexpected token � in JSON at position 0
PS C:\Cypress_Practice_tests\CalculateFunding-UITest> 

package.json

  "devDependencies": {
    "cypress": "^4.3.0",
    "mocha": "5.2.0",
    "mochawesome": "4.1.0",
    "mochawesome-merge": "4.0.3",
    "mochawesome-report-generator": "4.1.0"
  }

cypress.json

{
    "reporter": "mochawesome",
    "reporterOptions": {
      "reportDir": "cypress/results",
      "overwrite": false,
      "html": false,
      "json": true
    }
  }

@mstephano
Copy link

I had same issue with Unexpected token while trying to create the report on Windows 10. Here is my solution on another issue: Antontelesh/mochawesome-merge#6 (comment)

@automationJatinder
Copy link

I am still facing this problem (Unexpected token � in JSON at position 0) when using marge command to create HTML file. Does anyone has a solution for this?

@gaurav05153
Copy link

@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

@gaurav05153
Copy link

I tried your approach but "html-generator" step didn't work for me. Still I am on same issue where different report jsons are created but generation of a combined HTML report didn't work for me.
marge command (npx marge mochawesome.json) is also throwing below error:
mochawesomeconsolidated.json
Unexpected token � in JSON at position 0

@gaurav05153
Copy link

gaurav05153 commented Jul 23, 2020

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.

@nids2307
Copy link

nids2307 commented Sep 9, 2020

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.

When I try to install cypress-mochawesome-reporter, I see the following error:
yarn add -D cypress-mochaewsome-reporter
yarn add v1.22.4
[1/4] Resolving packages...
error An unexpected error occurred: "https://repo.website.com/artifactory/api/npm/npmjs/cypress-mochaewsome-reporter: not_found".

@hnnesv
Copy link

hnnesv commented Aug 16, 2021

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

@mflynn001
Copy link

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

@mustaf19
Copy link

Its an issue since ancient times.. I still have this issue..

@nagash77
Copy link
Contributor

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

@nagash77 nagash77 self-assigned this May 12, 2023
@nagash77 nagash77 added E2E Issue related to end-to-end testing Triaged Issue has been routed to backlog. This is not a commitment to have it prioritized by the team. labels May 17, 2023
@nagash77 nagash77 removed their assignment May 17, 2023
@isacraraujo
Copy link

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

Amazing! I'm using Mochawesome in Cypress, and this simple detail solved my problem!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E2E Issue related to end-to-end testing existing workaround topic: reporters 📄 Triaged Issue has been routed to backlog. This is not a commitment to have it prioritized by the team.
Projects
None yet
Development

No branches or pull requests