From a6facf0f66a23eda0b3e5ef74547c2dac50ab575 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Weslley=20Ara=C3=BAjo?= <46850407+wellwelwel@users.noreply.github.com> Date: Sun, 9 Jun 2024 23:40:07 -0300 Subject: [PATCH] fix(logs): improve output for `test`, `describe` and `it` (#357) --- src/configs/indentation.ts | 5 +++-- src/helpers/parse-assertion.ts | 7 ++++++- src/modules/describe.ts | 14 +++++++++++--- src/modules/it.ts | 23 +++++++++++++++++++++-- src/modules/test.ts | 20 ++++++++++++++++++-- 5 files changed, 59 insertions(+), 10 deletions(-) diff --git a/src/configs/indentation.ts b/src/configs/indentation.ts index c809b5d1..e51591a7 100644 --- a/src/configs/indentation.ts +++ b/src/configs/indentation.ts @@ -3,8 +3,9 @@ export const indentation = { test: ' ', stdio: ' ', - describeCounter: 0, - testCounter: 0, + hasDescribe: false, + hasTest: false, + hasIt: false, }; /* c8 ignore stop */ diff --git a/src/helpers/parse-assertion.ts b/src/helpers/parse-assertion.ts index 5fbe42c7..a03b7445 100644 --- a/src/helpers/parse-assertion.ts +++ b/src/helpers/parse-assertion.ts @@ -5,7 +5,9 @@ import { EOL } from 'node:os'; import { format } from './format.js'; import { hr } from './hr.js'; import { findFile } from './find-file.js'; +/* c8 ignore next */ import { each } from '../configs/each.js'; +/* c8 ignore next */ import { indentation } from '../configs/indentation.js'; import { fromEntries, entries } from '../polyfills/object.js'; import { nodeVersion } from './get-runtime.js'; @@ -56,7 +58,10 @@ export const parseAssertion = async ( const isPoku = typeof process.env?.FILE === 'string' && process.env?.FILE.length > 0; const FILE = process.env.FILE; - const preIdentation = indentation.describeCounter > 0 ? ' ' : ''; + let preIdentation = ''; + + if (indentation.hasDescribe || indentation.hasTest) preIdentation += ' '; + if (indentation.hasIt) preIdentation += ' '; try { if (typeof each.before.cb === 'function' && each.before.assert) { diff --git a/src/modules/describe.ts b/src/modules/describe.ts index b7dca8c8..9a04ca69 100644 --- a/src/modules/describe.ts +++ b/src/modules/describe.ts @@ -1,5 +1,6 @@ import { format, backgroundColor } from '../helpers/format.js'; import { write } from '../helpers/logs.js'; +/* c8 ignore next */ import { indentation } from '../configs/indentation.js'; /* c8 ignore next */ import type { DescribeOptions } from '../@types/describe.js'; @@ -35,12 +36,12 @@ export async function describe( /* c8 ignore start */ if (title) { + indentation.hasDescribe = true; + const { background, icon } = options || {}; - const message = `${cb ? '›' : icon || '☰'} ${title || ''}`; + const message = `${cb ? format.dim('◌') : icon || '☰'} ${format.bold(title) || ''}`; const noBackground = !background; - indentation.describeCounter++; - if (noBackground) write(`${format.bold(message)}`); else { write( @@ -56,4 +57,11 @@ export async function describe( /* c8 ignore next */ if (resultCb instanceof Promise) await resultCb; + + /* c8 ignore start */ + if (title) { + indentation.hasDescribe = false; + write(`${format.bold(format.success('●'))} ${format.bold(title)}`); + } + /* c8 ignore stop */ } diff --git a/src/modules/it.ts b/src/modules/it.ts index 00327735..99eab301 100644 --- a/src/modules/it.ts +++ b/src/modules/it.ts @@ -1,6 +1,9 @@ /* c8 ignore next */ import { each } from '../configs/each.js'; -import { describe } from './describe.js'; +/* c8 ignore next */ +import { indentation } from '../configs/indentation.js'; +import { format } from '../helpers/format.js'; +import { write } from '../helpers/logs.js'; export async function it( message: string, @@ -30,7 +33,14 @@ export async function it( cb = args[1] as () => unknown | Promise; } else cb = args[0] as () => unknown | Promise; - if (message) describe(message, { icon: '›' }); + /* c8 ignore start */ + if (message) { + indentation.hasIt = true; + write( + `${indentation.hasDescribe ? ' ' : ''}${format.dim('◌')} ${format.bold(format.italic(format.dim(message)))}` + ); + } + /* c8 ignore end */ const resultCb = cb(); @@ -42,4 +52,13 @@ export async function it( /* c8 ignore next */ if (afterResult instanceof Promise) await afterResult; } + + /* c8 ignore start */ + if (message) { + indentation.hasIt = false; + write( + `${indentation.hasDescribe ? ' ' : ''}${format.bold(format.success('●'))} ${format.bold(format.italic(message))}` + ); + } + /* c8 ignore stop */ } diff --git a/src/modules/test.ts b/src/modules/test.ts index 13a012f5..f8ee9166 100644 --- a/src/modules/test.ts +++ b/src/modules/test.ts @@ -1,6 +1,9 @@ /* c8 ignore next */ import { each } from '../configs/each.js'; -import { describe } from './describe.js'; +/* c8 ignore next */ +import { indentation } from '../configs/indentation.js'; +import { format } from '../helpers/format.js'; +import { write } from '../helpers/logs.js'; export async function test( message: string, @@ -30,7 +33,13 @@ export async function test( cb = args[1] as () => unknown | Promise; } else cb = args[0] as () => unknown | Promise; - if (message) describe(message, { icon: '›' }); + /* c8 ignore start */ + if (message) { + indentation.hasTest = true; + + write(`${format.dim('◌')} ${format.bold(message)}`); + } + /* c8 ignore stop */ const resultCb = cb(); @@ -42,4 +51,11 @@ export async function test( /* c8 ignore next */ if (afterResult instanceof Promise) await afterResult; } + + /* c8 ignore start */ + if (message) { + indentation.hasTest = false; + write(`${format.bold(format.success('●'))} ${format.bold(message)}`); + } + /* c8 ignore stop */ }