From 17d7bb862c55f98a03f81c995ab578ae1ab79cd5 Mon Sep 17 00:00:00 2001 From: Jonatan Nilsson Date: Fri, 20 Sep 2024 19:09:20 +0000 Subject: [PATCH] expose stub calls and remove logging on before/after code --- lib/eltro.mjs | 43 +++++++-------- lib/sinon.mjs | 1 + package.json | 2 +- test/eltro.flow.test.mjs | 111 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 135 insertions(+), 22 deletions(-) diff --git a/lib/eltro.mjs b/lib/eltro.mjs index 76f7b9d..f7c2281 100644 --- a/lib/eltro.mjs +++ b/lib/eltro.mjs @@ -117,6 +117,7 @@ process.on('uncaughtException', function(err) { }) function Eltro() { + this.process = process this.__timeout = 2000 this.hasExclusive = false this.reporter = 'list' @@ -159,8 +160,8 @@ Eltro.prototype.begin = function() { } Eltro.prototype.__runTest = async function(stats, test, prefix = 'Test', child = null) { - if (this.reporter === 'list') { - process.stdout.write(' \x1b[90m? ' + test.name + '\x1b[0m') + if (this.reporter === 'list' && prefix === 'Test') { + this.process.stdout.write(' \x1b[90m? ' + test.name + '\x1b[0m') } let markRealTest = child || test @@ -279,24 +280,24 @@ Eltro.prototype.__runTest = async function(stats, test, prefix = 'Test', child = this.captureOutsideExceptions = null if (this.reporter === 'list') { - readline.clearLine(process.stdout, 0) - readline.cursorTo(process.stdout, 0, null) + readline.clearLine(this.process.stdout, 0) + readline.cursorTo(this.process.stdout, 0, null) if (markRealTest.skipTest) { - process.stdout.write(' \x1b[94m- ' + markRealTest.name + '\x1b[0m\n') + this.process.stdout.write(' \x1b[94m- ' + markRealTest.name + '\x1b[0m\n') } else if (!markRealTest.error) { if (!test.name.startsWith('~')) { - process.stdout.write(' \x1b[32m√\x1b[90m ' + markRealTest.name + ' (' + markRealTest.totalTime + 'ms)\x1b[0m\n') + this.process.stdout.write(' \x1b[32m√\x1b[90m ' + markRealTest.name + ' (' + markRealTest.totalTime + 'ms)\x1b[0m\n') } } else if (prefix === 'Test') { - process.stdout.write(' \x1b[31m' + this.failedTests.length + ') ' + markRealTest.name + ' (' + markRealTest.totalTime + 'ms)\x1b[0m\n') + this.process.stdout.write(' \x1b[31m' + this.failedTests.length + ') ' + markRealTest.name + ' (' + markRealTest.totalTime + 'ms)\x1b[0m\n') } } else if (this.reporter === 'dot') { if (markRealTest.skipTest) { - process.stdout.write('\x1b[94m.\x1b[0m') - } else if (!markRealTest.error) { - process.stdout.write('\x1b[32m.\x1b[0m') + this.process.stdout.write('\x1b[94m.\x1b[0m') + } else if (markRealTest.error) { + this.process.stdout.write('\x1b[31m.\x1b[0m') } else if (prefix === 'Test') { - process.stdout.write('\x1b[31m.\x1b[0m') + this.process.stdout.write('\x1b[32m.\x1b[0m') } } } @@ -304,7 +305,7 @@ Eltro.prototype.__runTest = async function(stats, test, prefix = 'Test', child = Eltro.prototype.__runGroup = async function(g, stats) { if (g.tests.length) { if (this.reporter === 'list') { - console.log(' ' + g.name) + this.process.stdout.write(' ' + g.name + '\n') } } if (g.before) { @@ -351,8 +352,8 @@ Eltro.prototype.__runGroup = async function(g, stats) { Eltro.prototype.run = async function() { if (this.reporter && this.reporter !== 'test') { - console.log('') - console.log('') + this.process.stdout.write('' + '\n') + this.process.stdout.write('' + '\n') } captureUnknownErrors(this) @@ -381,23 +382,23 @@ Eltro.prototype.run = async function() { } } } else if (this.reporter) { - console.log('') - console.log('') + this.process.stdout.write('' + '\n') + this.process.stdout.write('' + '\n') if (stats.passed) { - console.log(' \x1b[32m' + stats.passed + ' passing \x1b[90m(' + (end[0] * 1000 + Math.round(end[1] / 1000000)) + 'ms)\x1b[0m') + this.process.stdout.write(' \x1b[32m' + stats.passed + ' passing \x1b[90m(' + (end[0] * 1000 + Math.round(end[1] / 1000000)) + 'ms)\x1b[0m' + '\n') } if (stats.failed) { - console.log(' \x1b[31m' + stats.failed + ' failing\x1b[0m') + this.process.stdout.write(' \x1b[31m' + stats.failed + ' failing\x1b[0m' + '\n') } if (stats.skipped) { - console.log(' \x1b[94m' + stats.skipped + ' pending\x1b[0m') + this.process.stdout.write(' \x1b[94m' + stats.skipped + ' pending\x1b[0m' + '\n') } - console.log('') + this.process.stdout.write('' + '\n') if (this.failedTests.length) { for (let x = 0; x < this.failedTests.length; x++) { let test = this.failedTests[x]; - console.log(' ' + (x + 1) + ') ' + test.name + ':') + this.process.stdout.write(' ' + (x + 1) + ') ' + test.name + ':' + '\n') printError(test.error) } } diff --git a/lib/sinon.mjs b/lib/sinon.mjs index 0623163..1593e84 100644 --- a/lib/sinon.mjs +++ b/lib/sinon.mjs @@ -34,6 +34,7 @@ export function stub(returnFunc = null) { func.lastCall = null func.called = false func.callCount = 0 + func.calls = calls func.findCall = function(fn) { for (let call of calls) { diff --git a/package.json b/package.json index ae679ff..662bcae 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "eltro", - "version": "1.4.5", + "version": "1.4.6", "description": "Eltro is a tiny no-dependancy test framework for node", "main": "index.mjs", "scripts": { diff --git a/test/eltro.flow.test.mjs b/test/eltro.flow.test.mjs index 85fc960..40c450d 100644 --- a/test/eltro.flow.test.mjs +++ b/test/eltro.flow.test.mjs @@ -8,6 +8,9 @@ function CreateT() { t.logger = { log: stub() } + t.process = { + stdout: { write: stub() } + } return t } @@ -284,6 +287,60 @@ e.describe('#beforeEach()', function() { assert.match(t.logger.log.getCallN(4)[0], /DDDD/) assert.match(t.logger.log.getCallN(4)[0], /BBBB/) }) + + e.describe('reporter', function() { + e.test('should not log before each with reporter list', async function() { + const t = CreateT() + t.reporter = 'list' + t.begin() + t.describe('BBBB', function() { + t.beforeEach(function() {}) + + t.describe('CCCC', function() { + t.test('c1', function() { }) + t.test('c2', function() { }) + }) + + t.describe('DDDD', function() { + t.test('d1', function() { }) + }) + + t.test('AAAA', function() { }) + }) + await t.run() + for (let row of t.process.stdout.write.calls) { + assert.notMatch(row.filter(x => x).join(' '), /before each/i) + } + }) + + e.test('should not log success before each with reporter dot', async function() { + const t = CreateT() + t.reporter = 'dot' + t.begin() + t.describe('BBBB', function() { + t.beforeEach(function() {}) + + t.describe('CCCC', function() { + t.test('c1', function() { }) + t.test('c2', function() { }) + }) + + t.describe('DDDD', function() { + t.test('d1', function() { }) + }) + + t.test('AAAA', function() { }) + }) + await t.run() + let total = 0 + for (let row of t.process.stdout.write.calls) { + if (row.filter(x => x).join(' ').match(/\[32m\./)) { + total++ + } + } + assert.strictEqual(total, 4) + }) + }) }) e.describe('#after()', function() { @@ -679,6 +736,60 @@ e.describe('#afterEach()', function() { assert.match(t.logger.log.getCallN(5)[0], /JJJJ/) assert.match(t.logger.log.getCallN(5)[0], /YYYY/) }) + + e.describe('reporter', function() { + e.test('should not log before each with reporter list', async function() { + const t = CreateT() + t.reporter = 'list' + t.begin() + t.describe('BBBB', function() { + t.afterEach(function() {}) + + t.describe('CCCC', function() { + t.test('c1', function() { }) + t.test('c2', function() { }) + }) + + t.describe('DDDD', function() { + t.test('d1', function() { }) + }) + + t.test('AAAA', function() { }) + }) + await t.run() + for (let row of t.process.stdout.write.calls) { + assert.notMatch(row.filter(x => x).join(' '), /after each/i) + } + }) + + e.test('should not log success before each with reporter dot', async function() { + const t = CreateT() + t.reporter = 'dot' + t.begin() + t.describe('BBBB', function() { + t.afterEach(function() {}) + + t.describe('CCCC', function() { + t.test('c1', function() { }) + t.test('c2', function() { }) + }) + + t.describe('DDDD', function() { + t.test('d1', function() { }) + }) + + t.test('AAAA', function() { }) + }) + await t.run() + let total = 0 + for (let row of t.process.stdout.write.calls) { + if (row.filter(x => x).join(' ').match(/\[32m\./)) { + total++ + } + } + assert.strictEqual(total, 4) + }) + }) }) let commonBeforeTests = ['before', 'beforeEach']