Skip to content

Commit

Permalink
tap printers modularized
Browse files Browse the repository at this point in the history
  • Loading branch information
izelnakri committed Mar 13, 2021
1 parent 622f042 commit 6b9c494
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/setup/node-js-environment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import chalk from 'chalk';
import QUnit from '../../index.js';
import yaml from 'js-yaml'
import indentString from 'indent-string';
import TAPDisplayFinalResult from '../tap/display-final-result.js';
import TAPDisplayTestResult from '../tap/display-test-result.js';

let COUNTER = { testCount: 0, failCount: 0, skipCount: 0, passCount: 0 };

global.window = global;
window.QUnit = QUnit;
window.QUNIX_TEST_TIME_COUNTER = (function() { // NOTE: might be needed for failFast option timeTaken calculation
const startTime = new Date();

return {
start: startTime,
stop: () => +(new Date()) - (+startTime)
};
})();


window.QUnit.on('testEnd', (details) => TAPDisplayTestResult(COUNTER, details));
window.QUnit.done((details) => {
window.QUNIT_RESULT = Object.assign(details, {
timeTaken: window.QUNIX_TEST_TIME_COUNTER.stop()
});
TAPDisplayFinalResult(COUNTER, details.timeTaken);
});

export default window.QUnit;
15 changes: 15 additions & 0 deletions lib/tap/display-final-result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export default function({ testCount, passCount, skipCount, failCount }, timeTaken) {
console.log('');
console.log(`1..${testCount}`);
console.log(`# tests ${testCount}`);
console.log(`# pass ${passCount}`);
console.log(`# skip ${skipCount}`);
console.log(`# fail ${failCount}`);

// let seconds = timeTaken > 1000 ? Math.floor(timeTaken / 1000) : 0;
// let milliseconds = timeTaken % 100;

console.log(`# duration ${timeTaken}`);
console.log('');
}
// console.log(details.timeTaken); // runtime
57 changes: 57 additions & 0 deletions lib/tap/display-test-result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import yaml from 'js-yaml'
import indentString from 'indent-string';

// tape TAP output: ['operator', 'stack', 'at', 'expected', 'actual']
// ava TAP output: ['message', 'name', 'at', 'assertion', 'values'] // Assertion #5, message
export default function(COUNTER, details) { // NOTE: https://github.com/qunitjs/qunit/blob/master/src/html-reporter/diff.js
COUNTER.testCount++;

if (details.status === 'skipped') {
COUNTER.skipCount++;
console.log(`ok ${COUNTER.testCount}`, details.fullName.join(' | '), '# skip');
} else if (details.status === 'todo') {
console.log(`not ok ${COUNTER.testCount}`, details.fullName.join(' | '), '# skip');
} else if (details.status === 'failed') {
COUNTER.failCount++;
console.log(`not ok ${COUNTER.testCount}`, details.fullName.join(' | '), `# (${details.runtime} ms)`);
details.assertions.reduce((errorCount, assertion, index) => {
if (!assertion.passed && assertion.todo === false) {
COUNTER.errorCount++;
let stack = assertion.stack?.match(/\(.+\)/g);

console.log(' ---');
console.log(indentString(yaml.safeDump({
name: `Assertion #${index + 1}`, // TODO: check what happens on runtime errors
actual: assertion.actual || null,
expected: assertion.expected || null,
message: assertion.message || null,
stack: assertion.stack || null,
at: stack ? stack[0].replace('(file://', '').replace(')', '') : null
}).trim(), 4));
console.log(' ...');
}

return errorCount;
}, 0);
} else if (details.status === 'passed') {
COUNTER.passCount++;
console.log(`ok ${COUNTER.testCount}`, details.fullName.join(' | '), `# (${details.runtime} ms)`);
}
}

// not ok 10 test exited without ending: deepEqual true works
// ---
// operator: fail
// at: process.<anonymous> (/home/izelnakri/ava-test/node_modules/tape/index.js:85:19)
// stack: |-
// Error: test exited without ending: deepEqual true works
// at Test.assert [as _assert] (/home/izelnakri/ava-test/node_modules/tape/lib/test.js:269:54)
// at Test.bound [as _assert] (/home/izelnakri/ava-test/node_modules/tape/lib/test.js:90:32)
// at Test.fail (/home/izelnakri/ava-test/node_modules/tape/lib/test.js:363:10)
// at Test.bound [as fail] (/home/izelnakri/ava-test/node_modules/tape/lib/test.js:90:32)
// at Test._exit (/home/izelnakri/ava-test/node_modules/tape/lib/test.js:226:14)
// at Test.bound [as _exit] (/home/izelnakri/ava-test/node_modules/tape/lib/test.js:90:32)
// at process.<anonymous> (/home/izelnakri/ava-test/node_modules/tape/index.js:85:19)
// at process.emit (node:events:376:20)
// ...

0 comments on commit 6b9c494

Please sign in to comment.