Parsing Jest response and fail Bitbucket Pipeline

This is an extension to Lean and reusable DX Bitbucket Pipeline for PR checks.

Similar to Apex tests I also want to run all JEST tests when a scratch org is created using my shell scripts AND from the Bitbucket pipeline.

Making this work for both shell script and pipeline is hard because I cannot really work with the JSON output of the SFDX Jest tool. The pipeline does not fail even if the script fails so as a workaround I’m using jq to parse the returned response and return exit code 1 to fail the build in case of error.

Here is my script file

#!/bin/bash
source `dirname $0`/config.sh

holdScreen=true
while getopts 'p' flag; do
  case "${flag}" in
    p) holdScreen=false ;;
  esac
done

execute() {
    $@ > temp.json
    cat temp.json
    $(jq '.status == 0' temp.json) || $holdScreen || exit 1
}

executenpm() {
    $@ > temp.json
    # note: a hack to remove the first 4 lines and pass result in a valid format to jq
    sed '1,4 {d}' temp.json > temp2.json
    cat temp.json
    $(jq '.success' temp.json) || $holdScreen || exit 1
}


echo "deleting old scratch org"
sfdx force:org:delete -p -u $SCRATCH_ORG_ALIAS

echo "Creating scratch ORG"
sfdx force:org:create -a $SCRATCH_ORG_ALIAS -s -f ./config/project-scratch-def.json -d 1

echo "Pushing changes to scratch org"
execute "sfdx force:source:push --json"

echo "Assigning permission"
execute "sfdx force:user:permset:assign --json -n Admin"

echo "Running apex tests"
execute "sfdx force:apex:test:run -l RunLocalTests -w 30 --json"


echo "Running jest tests"
npm install
executenpm "npm run test:unit"

rm temp.json

if $holdScreen 
then
    echo "Org is set up"
    read
fi

In the last step npm run test:unit returns a JSON with 4 additional lines

> sfprojecttemplate@1.0.0 test:unit E:\projects\logicline\sfProjectTemplate
> sfdx-lwc-jest -- --json

{
    "numFailedTestSuites": 1,
    "numFailedTests": 1,
    "numPassedTestSuites": 0,
    "numPassedTests": 0,
    "numPendingTestSuites": 0,
    "numPendingTests": 0,
    "numRuntimeErrorTestSuites": 0,
    "numTodoTests": 0,
    "numTotalTestSuites": 1,
    "numTotalTests": 1,
    "openHandles": [],
    "snapshot": {
        "added": 0,
        "didUpdate": false,
        "failure": false,
        "filesAdded": 0,
        "filesRemoved": 0,
        "filesRemovedList": [],
        "filesUnmatched": 0,
        "filesUpdated": 0,
        "matched": 0,
        "total": 0,
        "unchecked": 0,
        "uncheckedKeysByFile": [],
        "unmatched": 0,
        "updated": 0
    },
    "startTime": 1583238616121,
    "success": false,
    "testResults": [
        {
            "assertionResults": [
                {
                    "ancestorTitles": [
                        "c-test-jest initialize"
                    ],
                    "failureMessages": [
                        "Error: expect(received).toBeNull()\n\nReceived: <div class=\"message\">Hekl</div>\n    at E:\\projects\\logicline\\sfProjectTemplate\\force-app\\main\\default\\lwc\\modal\\__tests__\\modal.test.js:17:13"
                    ],
                    "fullName": "c-test-jest initialize message should be initialization",
                    "location": null,
                    "status": "failed",
                    "title": "message should be initialization"
                }
            ],
            "endTime": 1583238619468,
            "message": "  ● c-test-jest initialize › message should be initialization\n\n    expect(received).toBeNull()\n\n    Received: <div class=\"message\">Hekl</div>\n\n      15 | \n      16 |             // Verify\n    > 17 |             expect(modal.shadowRoot.querySelector(\".message\")).toBeNull();\n         |             ^\n      18 |         });\n      19 |     });\n      20 | \n\n      at force-app/main/default/lwc/modal/__tests__/modal.test.js:17:13\n",
            "name": "E:\\projects\\logicline\\sfProjectTemplate\\force-app\\main\\default\\lwc\\modal\\__tests__\\modal.test.js",
            "startTime": 1583238616942,
            "status": "failed",
            "summary": ""
        }
    ],
    "wasInterrupted": false
}

I had to create a separate function executenpm which deletes the first 4 lines which is a hack.

Is there a better way to parse the jest response?

Answer

enter image description here
Can you change your package json npm script to:
lwc-jest -- --json > json-out.json?
The output should be only the JSON and not the additional information from NPM.

The problem is you are saving the npm output, not only the JSON content.

Then to read the json file, just run:
cat json-out.json | jq ".success"

Attribution
Source : Link , Question Author : Anmol Kumar , Answer Author : Diéffrei Quadros

Leave a Comment