Skip to content

Commit

Permalink
fix(logs): improve output for test, describe and it (#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
wellwelwel authored Jun 10, 2024
1 parent c7b2b48 commit a6facf0
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 10 deletions.
5 changes: 3 additions & 2 deletions src/configs/indentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
export const indentation = {
test: ' ',
stdio: ' ',
describeCounter: 0,
testCounter: 0,
hasDescribe: false,
hasTest: false,
hasIt: false,
};

/* c8 ignore stop */
7 changes: 6 additions & 1 deletion src/helpers/parse-assertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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) {
Expand Down
14 changes: 11 additions & 3 deletions src/modules/describe.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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(
Expand All @@ -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 */
}
23 changes: 21 additions & 2 deletions src/modules/it.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -30,7 +33,14 @@ export async function it(
cb = args[1] as () => unknown | Promise<unknown>;
} else cb = args[0] as () => unknown | Promise<unknown>;

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();

Expand All @@ -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 */
}
20 changes: 18 additions & 2 deletions src/modules/test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -30,7 +33,13 @@ export async function test(
cb = args[1] as () => unknown | Promise<unknown>;
} else cb = args[0] as () => unknown | Promise<unknown>;

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();

Expand All @@ -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 */
}

0 comments on commit a6facf0

Please sign in to comment.