Skip to content

Commit

Permalink
fix(logs): respect quiet option (#492)
Browse files Browse the repository at this point in the history
  • Loading branch information
wellwelwel committed Jul 1, 2024
1 parent 812744d commit aabb1b3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 37 deletions.
39 changes: 22 additions & 17 deletions src/services/each.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import type { Configs } from '../@types/poku.js';
import { format } from './format.js';
import { Write } from '../services/write.js';
import { isQuiet } from '../parsers/output.js';

const eachCore = async (
type: keyof Required<Pick<Configs, 'beforeEach' | 'afterEach'>>,
Expand All @@ -14,33 +15,37 @@ const eachCore = async (
}

const cb = configs[type];
const showLogs = !isQuiet(configs);

const cbName = cb.name !== type ? cb.name : 'anonymous function';

Write.log(
` ${format('◯').dim().info()} ${format(`${type}: ${cbName}`)
.dim()
.italic()}`
);
showLogs &&
Write.log(
` ${format('◯').dim().info()} ${format(`${type}: ${cbName}`)
.dim()
.italic()}`
);

try {
await cb();

return true;
} catch (error) {
Write.log(
format(` ✘ ${type} callback failed ${format(`› ${cbName}`).dim()}`)
.fail()
.bold()
);
Write.log(format(` ├─ Who's trying to run this ${type}?`).fail());
Write.log(
format(` │ └─ ${format(fileRelative).fail().underline()}`).fail()
);
if (showLogs) {
Write.log(
format(` ✘ ${type} callback failed ${format(`› ${cbName}`).dim()}`)
.fail()
.bold()
);
Write.log(format(` ├─ Who's trying to run this ${type}?`).fail());
Write.log(
format(` │ └─ ${format(fileRelative).fail().underline()}`).fail()
);

if (error instanceof Error) {
Write.log(format(' ├─ Message:').fail());
Write.log(format(` │ └─ ${error.message}`).fail());
if (error instanceof Error) {
Write.log(format(' ├─ Message:').fail());
Write.log(format(` │ └─ ${error.message}`).fail());
}
}

return false;
Expand Down
19 changes: 13 additions & 6 deletions src/services/run-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,17 @@ export const runTests = async (
passed = false;

if (configs?.failFast) {
Write.hr();
Write.log(
` ${format('ℹ').fail()} ${format('fail-fast').bold()} is enabled`
);
if (showLogs) {
Write.hr();
Write.log(
` ${format('ℹ').fail()} ${format('fail-fast').bold()} is enabled`
);
}

break;
}
}

/* c8 ignore stop */
}

Expand All @@ -99,6 +103,7 @@ export const runTestsParallel = async (
const filesByConcurrency: string[][] = [];
const concurrencyLimit = configs?.concurrency || 0;
const concurrencyResults: (boolean | undefined)[][] = [];
const showLogs = !isQuiet(configs);

if (concurrencyLimit > 0) {
for (let i = 0; i < files.length; i += concurrencyLimit) {
Expand Down Expand Up @@ -143,8 +148,10 @@ export const runTestsParallel = async (
return concurrencyResults.every((group) => group.every((result) => result));
/* c8 ignore start */
} catch (error) {
Write.hr();
error instanceof Error && console.error(error.message);
if (showLogs) {
Write.hr();
error instanceof Error && console.error(error.message);
}

return false;
}
Expand Down
15 changes: 1 addition & 14 deletions test/e2e/before-and-after-each.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ test(async () => {
await it(async () => {
const code = await poku('./fixtures/success', {
noExit: true,
quiet: true,

beforeEach: prepareService,
afterEach: resetService,
});
Expand All @@ -31,7 +31,6 @@ test(async () => {
await it(async () => {
const code = await poku('./fixtures/fail', {
noExit: true,
quiet: true,
beforeEach: prepareService,
afterEach: resetService,
});
Expand All @@ -48,7 +47,6 @@ test(async () => {
await it(async () => {
const code = await poku('./fixtures/success', {
noExit: true,
quiet: true,
beforeEach: () => prepareService(),
afterEach: () => resetService(),
});
Expand All @@ -63,7 +61,6 @@ test(async () => {
await it(async () => {
const code = await poku('./fixtures/fail', {
noExit: true,
quiet: true,
beforeEach: () => prepareService(),
afterEach: () => resetService(),
});
Expand All @@ -80,7 +77,6 @@ test(async () => {
await it(async () => {
const code = await poku('./fixtures/success', {
noExit: true,
quiet: true,
beforeEach: async () => await prepareService(),
afterEach: async () => await resetService(),
});
Expand All @@ -95,7 +91,6 @@ test(async () => {
await it(async () => {
const code = await poku('./fixtures/fail', {
noExit: true,
quiet: true,
beforeEach: async () => await prepareService(),
afterEach: async () => await resetService(),
});
Expand All @@ -112,7 +107,6 @@ test(async () => {
await it(async () => {
const code = await poku('./fixtures/success', {
noExit: true,
quiet: true,
beforeEach: () => true,
afterEach: () => true,
});
Expand All @@ -127,7 +121,6 @@ test(async () => {
await it(async () => {
const code = await poku('./fixtures/fail', {
noExit: true,
quiet: true,
beforeEach: () => true,
afterEach: () => true,
});
Expand All @@ -144,7 +137,6 @@ test(async () => {
await it(async () => {
const code = await poku('./fixtures/success', {
noExit: true,
quiet: true,
beforeEach: function () {
return;
},
Expand All @@ -163,7 +155,6 @@ test(async () => {
await it(async () => {
const code = await poku('./fixtures/fail', {
noExit: true,
quiet: true,
beforeEach: function () {
return;
},
Expand All @@ -184,7 +175,6 @@ test(async () => {
await it(async () => {
const code = await poku('./fixtures/success', {
noExit: true,
quiet: true,
beforeEach: crashIt,
afterEach: resetService,
});
Expand All @@ -199,7 +189,6 @@ test(async () => {
await it(async () => {
const code = await poku('./fixtures/success', {
noExit: true,
quiet: true,
beforeEach: prepareService,
afterEach: crashIt,
});
Expand All @@ -214,7 +203,6 @@ test(async () => {
await it(async () => {
const code = await poku('./fixtures/success', {
noExit: true,
quiet: true,
beforeEach: crashItAgain,
afterEach: resetService,
});
Expand All @@ -229,7 +217,6 @@ test(async () => {
await it(async () => {
const code = await poku('./fixtures/success', {
noExit: true,
quiet: true,
beforeEach: prepareService,
afterEach: crashItAgain,
});
Expand Down

0 comments on commit aabb1b3

Please sign in to comment.