jemishgopani/wdio-json-reporter

wdioV5: wdio-json-reporter-1.4.0: JSON report merger fails with the error when the file is empty.

NaveenThiyagarajan opened this issue · 2 comments

Issue: When there are feature files which are not used as part of execution because of no matching tagExpression, there is still a empty JSON report generated for that feature file. The file is completely empty.

And in the end when we call mergeResults(process.cwd() + '/reports/json/', 'results-*'), it fails with the error:

2020-06-23T12:20:46.251Z ERROR @wdio/cli:utils: Error in onCompleteHook: SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at fileNames.forEach.fileName (/Users/user/Documents/io_workspace/e_automation/node_modules/wdio-json-reporter/src/mergeResults.js:19:24)
    at Array.forEach (<anonymous>)
    at getDataFromFiles (/Users/user/Documents/io_workspace/e_automation/node_modules/wdio-json-reporter/src/mergeResults.js:18:15)
    at mergeResults (/Users/user/Documents/io_workspace/e_automation/node_modules/wdio-json-reporter/src/mergeResults.js:9:21)
    at Object.mergeJSONReporterResults (/Users/user/Documents/io_workspace/e_automation/utils/email/mergeJSON.js:5:5)
    at Object.onComplete (/Users/user/Documents/io_workspace/e_automation/wdio.conf.js:458:25)
    at Promise.all.onCompleteHook.map (/Users/user/Documents/io_workspace/e_automation/node_modules/@wdio/cli/build/utils.js:82:13)
    at Array.map (<anonymous>)
    at runOnCompleteHook (/Users/user/Documents/io_workspace/e_automation/node_modules/@wdio/cli/build/utils.js:80:37)

Here are the version details:

├── @babel/cli@7.10.1
├── @babel/core@7.10.2
├── @babel/preset-env@7.10.2
├── @babel/register@7.10.1
├── @wdio/allure-reporter@5.22.4
├── @wdio/cli@5.23.0
├── @wdio/cucumber-framework@5.23.0
├── @wdio/local-runner@5.23.0
├── @wdio/selenium-standalone-service@5.16.10
├── @wdio/spec-reporter@5.23.0
├── @wdio/sync@5.23.0
├── allure-commandline@2.7.0
├── aws-sdk@2.656.0
├── chai@4.1.2
├── clean-dir@1.0.3
├── config@2.0.1
├── date-fns@1.29.0
├── generate-password@1.5.1
├── jimp@0.5.4
├── jsonpath@1.0.0
├── jsqr@1.2.0
├── log4js@3.0.5
├── log4js-extend@0.2.1
├── mysql@2.16.0
├── nodemailer@4.6.8
├── proxy-agent@3.1.1
├── qrcode-reader@1.0.4
├── request@2.88.0
├── string-similarity@2.0.0
├── wdio-cucumber-reporter@0.0.2
├── wdio-cucumber-snippet-reporter@0.0.5
├── wdio-json-reporter@1.4.0
└── webdriverio@5.23.0

Please let me know if you need any more info or if i am missing anything.

Ok, went through the code: In src/mergeResults.js

function getDataFromFiles (dir, filePattern) {
    const fileNames = fs.readdirSync(dir).filter(file => file.match(filePattern))
    const data = []

    fileNames.forEach(fileName => {
        data.push(JSON.parse(fs.readFileSync(`${dir}/${fileName}`)))
    })

    return data
}

I feel there should be a check before allowing JSON.parse to parse an empty file.

Stale issue message