tapjs/tap-parser

best way to get all output

Closed this issue · 4 comments

I am trying to find the best way to aggregate all the children and parent level plans and assertions and it seems to be proving not very fruitful when making sure that the tape and tap outputs are the same

const parser   = require('tap-parser');
const through  = require('through2');
const duplexer = require('duplexer');

module.exports = () => {
  var tap = parser();
  var out = through.obj();
  var dup = duplexer(tap, out);

  var currentPlan = -1;
  var currentAssertion = -1;
  var data = [];
  var name = null;
  var plan = null;

  var startTime = Date.now();

  tap.on('comment', (res) => {
    if(!plan) {
      data.push({ type: 'test', name: res, start: Date.now(), assertions: [] });

      // get the current index of the plan
      // so that we can use this to push the current assertions to it
      currentPlan += 1;
      currentAssertion = -1;
    }
  });

  tap.on('plan', (res) => {
    plan = res;
  });

  tap.on('extra', (res) => {
    data[currentPlan]['assertions'][currentAssertion]['console'] += `${res}\n`;
  });

  tap.on('assert', (res) => {
    console.log(res);
    data[currentPlan].assertions.push({
      type: 'assert',
      number: res.id,
      name: res.name,
      ok: res.ok,
      diag: res.diag,
      console: '',
      end: Date.now()
    });
    currentAssertion += 1;
  });

  tap.on('complete', (res) => {
    res['time'] = Date.now() - startTime;

    var plan = -1;

    // combine and clean up tests
    for(var i = 0; i < data.length; i++) {
      // This is a top level plan
      if(data[i].assertions.length == 0) {
        // move on with the tests
        plan = i;
        data[plan].tests = [];
        delete data[i].assertions;
      } else {
        // We know this is part of the currentPlan
        data[plan].tests.push(data[i]);
        delete data[i];
      }
    }

    data = data.filter((d) => d > '');

    function calculateTime(test) {
      test.end = test.assertions[test.assertions.length - 1].end;
      test.assertions.forEach((assertion) => {
        assertion.start = test.start;
      });
    }
    data.forEach((plan) => {
      for(var i = 0; i < plan.tests.length; i++) {
        calculateTime(plan.tests[i]);
      }
      plan.end = plan.tests[plan.tests.length - 1].end;
    });

    res['tests'] = data
    console.log(JSON.stringify(res, null, 4));
  });

  return dup;
};

which will output

tape

{
    "ok": false,
    "count": 8,
    "pass": 7,
    "fail": 1,
    "bailout": false,
    "todo": 0,
    "skip": 0,
    "plan": {
        "start": 1,
        "end": 8,
        "skipAll": false,
        "skipReason": "",
        "comment": ""
    },
    "failures": [
        {
            "ok": false,
            "id": 5,
            "name": "faiilllll",
            "diag": {
                "operator": "error",
                "expected": "undefined",
                "actual": "faiilllll",
                "at": "Test.<anonymous> (/Users/gacsapo/Documents/temp/tap-html/test/index.js:19:7)"
            }
        }
    ],
    "time": 3809,
    "tests": [
        {
            "type": "test",
            "name": "# look at me!\n",
            "start": 1496865751468,
            "tests": [
                {
                    "type": "test",
                    "name": "# hello world\n",
                    "start": 1496865751468,
                    "assertions": [
                        {
                            "type": "assert",
                            "number": 1,
                            "name": "should be equal",
                            "ok": true,
                            "console": "",
                            "end": 1496865751472,
                            "start": 1496865751468
                        }
                    ],
                    "end": 1496865751472
                },
                {
                    "type": "test",
                    "name": "# what what what\n",
                    "start": 1496865751472,
                    "assertions": [
                        {
                            "type": "assert",
                            "number": 2,
                            "name": "should be equal",
                            "ok": true,
                            "console": "",
                            "end": 1496865751472,
                            "start": 1496865751472
                        },
                        {
                            "type": "assert",
                            "number": 3,
                            "name": "should be equal",
                            "ok": true,
                            "console": "",
                            "end": 1496865751473,
                            "start": 1496865751472
                        },
                        {
                            "type": "assert",
                            "number": 4,
                            "name": "should be equal",
                            "ok": true,
                            "console": "",
                            "end": 1496865751473,
                            "start": 1496865751472
                        }
                    ],
                    "end": 1496865751473
                },
                {
                    "type": "test",
                    "name": "# should fail\n",
                    "start": 1496865751473,
                    "assertions": [
                        {
                            "type": "assert",
                            "number": 5,
                            "name": "faiilllll",
                            "ok": false,
                            "diag": {
                                "operator": "error",
                                "expected": "undefined",
                                "actual": "faiilllll",
                                "at": "Test.<anonymous> (/Users/gacsapo/Documents/temp/tap-html/test/index.js:19:7)"
                            },
                            "console": "",
                            "end": 1496865751476,
                            "start": 1496865751473
                        }
                    ],
                    "end": 1496865751476
                },
                {
                    "type": "test",
                    "name": "# what what what what!?\n",
                    "start": 1496865751476,
                    "assertions": [
                        {
                            "type": "assert",
                            "number": 6,
                            "name": "should be equal",
                            "ok": true,
                            "console": "",
                            "end": 1496865752263,
                            "start": 1496865751476
                        }
                    ],
                    "end": 1496865752263
                }
            ],
            "end": 1496865752263
        },
        {
            "type": "test",
            "name": "# should take a long time between tests\n",
            "start": 1496865752263,
            "tests": [
                {
                    "type": "test",
                    "name": "# long 1\n",
                    "start": 1496865752263,
                    "assertions": [
                        {
                            "type": "assert",
                            "number": 7,
                            "name": "should be equal",
                            "ok": true,
                            "console": "",
                            "end": 1496865755264,
                            "start": 1496865752263
                        }
                    ],
                    "end": 1496865755264
                },
                {
                    "type": "test",
                    "name": "# long 2\n",
                    "start": 1496865755264,
                    "assertions": [
                        {
                            "type": "assert",
                            "number": 8,
                            "name": "should be equal",
                            "ok": true,
                            "console": "1496865755264\n\n",
                            "end": 1496865755266,
                            "start": 1496865755264
                        }
                    ],
                    "end": 1496865755266
                }
            ],
            "end": 1496865755266
        }
    ]
}

and fails to output anything for tap....

@isaacs do you have any input on how to get overall data output for both tap and tape?

This isn't the complete program, so it's challenging for me to figure out what yo'ure trying to do, where the data is coming from, etc.

Maybe take a look at (or just use) the tap-parser bin script included with this package?

tap tests/*.js | tap-parser

Run tap-parser --help to output the options.

@isaacs the code is located https://github.com/gabrielcsapo/tap-html/blob/master/index.js. I didn't have it published when I opened the issue

@isaacs would you be able to look at this again?