Node Test Runner & the TAP protocol. How to log/print the vals, desc & results of successful tests

673 Views Asked by At

Node.js Test-runner / TAP Protocol

So I really like the idea of Node.js using its own test-runner, and I like the idea of it using the Tap protocol. The only issue I have, is sometimes while developing my tests (using Mocha &/or Jest) I like to view the actual and expected, even when all test have passed, IDK about other people, but there has been more than one occassion where I was getting false positives, which ends up causing me a headache, as I continue with my work, just to figure out that I am working on a system that is flawed (at this point I get angry delete everything to a practical point in my tests) and have to re write a bunch of code. To avoid this pit-fall, I have learned to read test results

"Actual" & "Expected" values as a way of verifying that the tests are in fact passing for all the right reasons (constrained by the arguments that I intended them to be at any given moment in the test).

Let me say this while I am thinking it: "Jest & Mocha are overly verbose" On the other hand, the tap protocol seems to be, "not verbose enough"

I would like to have control over what tests print when they are sucessfull. To be more specific, I would like to be able to print the ACTUAL/EXPECTED values of a test even when its successful (not all, just the ones I specify)...

Does the tap protocol define (or allow) the printing of Actual/expected values for successful tests? &/or is it possible to specify certain tests to print their actual/expected results, even when the test is successful, using the "Node test-runner" API?

1

There are 1 best solutions below

0
Michał Grzegorzewski On

I had similar question, so TAP specification generally shows how subtests should be printed

and after a little research I found this article

now I checked the latest lts version (v18.14.1) of node at 2023-02-21 and I wrote a dummy test.

import { describe, it } from 'node:test';
import * as assert from 'assert';

describe(`As a User`, () => {
  it(`should send a message to receiver`, () => {
    assert.deepEqual({ a: { b: { c: 'abc' } } }, { a: { b: { c: 'abc' } } });
  });
});

I used ts-node to run it

> node -v && node --require ts-node/register --test communication.test.ts

v18.14.1
TAP version 13
# Subtest: /Users/user/projects/communication.test.ts
    # Subtest: As a User
        # Subtest: should send a message to receiver
        ok 1 - should send a message to receiver
          ---
          duration_ms: 0.465333
          ...
        1..1
    ok 1 - As a User
      ---
      duration_ms: 1.365375
      ...
    1..1
ok 1 - /Users/user/projects/communication.test.ts
  ---
  duration_ms: 404.991583
  ...
1..1
# tests 1
# pass 1
# fail 0
# cancelled 0
# skipped 0
# todo 0
# duration_ms 405.751958

So try the latest lts node or if you don't care about the future try a custom test reporter which may give you a better control on the output which is available since v19.6.0