0

I have an object like this:

runs = 
    {
        "suites": [
            {
                "status": "fail",
                "testcases": [
                    {
                        "status": "pass"
                    },
                    {
                        "status": "fail"
                    }
                ]
            },
            {
                "status": "pass",
                "testcases": [
                    {
                        "status": "pass"
                    }
                ]
            }
        ]
    }

I would like to retrieve the count of test case pass and fail (In the above case pass: 2, fail: 1). I tried the following to get the pass count:

runs.suites.filter(suite => {
    suite.testcases.filter(testcase => {
        return testcase.status === 'pass';
    })
}).length

But it is giving me an incorrect result.

4 Answers 4

1

Try this

// Code goes here

    runs = 
        {
            "suites": [
                {
                    "status": "fail",
                    "testcases": [
                        {
                            "status": "pass"
                        },
                        {
                            "status": "fail"
                        }
                    ]
                },
                {
                    "status": "pass",
                    "testcases": [
                        {
                            "status": "pass"
                        }
                    ]
                }
            ]
        };
        var count = 0;
        var test = runs.suites.filter(suite => {
            suite.testcases.filter(testcase => {
                (testcase.status=='pass')?count++:count;
            })
            return count;
        });

    console.log(test.length);
Sign up to request clarification or add additional context in comments.

Comments

1

You could use an object for counting.

var runs = { "suites": [{ "status": "fail", "testcases": [{ "status": "pass" }, { "status": "fail" }] }, { "status": "pass", "testcases": [{ "status": "pass" }] }] },
count = {}

runs.suites.forEach(a => 
    a.testcases.forEach(b =>
        count[b.status] = (count[b.status] || 0) + 1));

console.log(count);

Comments

1

Try this:

var runs = {"suites": [{"status": "fail","testcases": [{"status": "pass"},{"status": "fail"}]},{"status": "pass","testcases": [{"status": "pass"}]}]};

const statuses = runs.suites.reduce((a, b)=> a.testcases.concat(b.testcases)).map(x=> x.status)
console.log(statuses.filter(x=> x === "pass").length) // 2
console.log(statuses.filter(x=> x === "fail").length) // 1

1 Comment

I'm getting Uncaught (in promise) TypeError: run.suites.reduce(...).map is not a function(…)
0

You can use Array#reduce and take the count of pass/fail in an object {pass : 2, fail : 1}

var runs = {"suites": [{"status": "fail","testcases": [{"status": "pass"},{"status": "fail"}]},{"status": "pass","testcases": [{"status": "pass"}]}]};

var obj = runs.suites.reduce( (tot, curr)=> {
  curr.testcases.forEach(tc=>{
    tc.status === 'pass' ? tot.pass++ : tot.fail++;
  });
  return tot;
}, {pass : 0, fail : 0});

console.log(obj);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.