Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(describe): support for Node.js familiar API usage #353

Merged
merged 4 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion benchmark/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ The benchmark is performed by comparing a simple success and a failure test, eac

> [!important]
>
> **Poku** doesn't intend to be the best, but to offer a balance between high performance, compatibility, lightness and ease of use.
> **Poku** doesn't intend to be "the best", but to offer a balance between high performance, isolation, compatibility, lightness and ease of use.

---

Expand Down
59 changes: 45 additions & 14 deletions src/modules/describe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,55 @@ import { indentation } from '../configs/indentation.js';
import type { DescribeOptions } from '../@types/describe.js';

/**
* On **Poku**, `describe` is just a pretty `console.log` to title your test suites in the terminal.
* On **Poku**, `describe` also can be used just as a pretty `console.log` to title your test suites in the terminal.
*/
export const describe = (title: string, options?: DescribeOptions) => {
const { background, icon } = options || {};
export async function describe(
title: string,
cb: () => Promise<unknown>
): Promise<void>;
export function describe(title: string, cb: () => unknown): void;
export async function describe(cb: () => Promise<unknown>): Promise<void>;
export function describe(cb: () => unknown): unknown;
export function describe(title: string, options?: DescribeOptions): void;
export async function describe(
arg1: string | (() => unknown | Promise<unknown>),
arg2?: (() => unknown | Promise<unknown>) | DescribeOptions
): Promise<void> {
let title: string | undefined;
let cb: (() => unknown | Promise<unknown>) | undefined;
let options: DescribeOptions | undefined;

const message = `${icon || '☰'} ${title}`;
const noBackground = !background;
if (typeof arg1 === 'string') {
title = arg1;

indentation.describeCounter++;
if (typeof arg2 === 'function') cb = arg2;
else options = arg2;
} else if (typeof arg1 === 'function') {
cb = arg1;
options = arg2 as DescribeOptions;
}

/* c8 ignore start */
if (noBackground) {
write(`${format.bold(message)}`);
return;
}
if (title) {
const { background, icon } = options || {};
const message = `${cb ? '›' : icon || '☰'} ${title || ''}`;
const noBackground = !background;

write(
`${format.bg(backgroundColor[typeof background === 'string' ? background : 'grey'], message)}`
);
indentation.describeCounter++;

if (noBackground) write(`${format.bold(message)}`);
else {
write(
`${format.bg(backgroundColor[typeof background === 'string' ? background : 'grey'], message)}`
);
}
}
/* c8 ignore stop */
};

if (typeof cb !== 'function') return;

const resultCb = cb();

/* c8 ignore next */
if (resultCb instanceof Promise) await resultCb;
}
40 changes: 38 additions & 2 deletions test/integration/describe/describe.test.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,38 @@
// import { describe } from '../../../src/modules/describe.js';
// import { test } from '../../../src/modules/test.js';
import { describe } from '../../../src/modules/describe.js';
import { test } from '../../../src/modules/test.js';

describe('Testing "describe" method', {
icon: '🔬',
});

test('Using as titles', () => {
describe('');
describe('', {});
});

test('Using as functions', async () => {
describe(() => {});
describe(() => true);
describe(() => false);
describe(() => undefined);
describe(() => new Promise((resolve) => resolve(undefined)));
describe(async () => await new Promise((resolve) => resolve(undefined)));

await describe(() => new Promise((resolve) => resolve(undefined)));
await describe(
async () => await new Promise((resolve) => resolve(undefined))
);
});

test('Using as groups', async () => {
describe('', () => {});
describe('', () => true);
describe('', () => false);
describe('', () => undefined);
describe('', () => new Promise((resolve) => resolve(undefined)));
describe('', async () => await new Promise((resolve) => resolve(undefined)));

await describe('', () => new Promise((resolve) => resolve(undefined)));
await describe('', async () =>
await new Promise((resolve) => resolve(undefined)));
});
72 changes: 33 additions & 39 deletions website/docs/documentation/helpers/describe.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,35 @@ sidebar_position: 2
---

import { FAQ } from '@site/src/components/FAQ';
import Example from '@site/static/img/describe-example.png';

# describe

On **Poku**, `describe` is just a pretty `console.log` to title your test suites in the terminal.
## Using as test titles

On **Poku**, `describe` can be used just as a pretty `console.log` to title your test suites in the terminal.

```ts
import { describe, assert } from 'poku';

describe('Group A');
assert(true, '1');
assert(true, '2');
describe('Test Title');

describe('Group B');
assert(true, '1');
assert(true, '2');
```

## Personalization
### Personalization

> `describe(title: string, options?: DescribeOptions)`

### background
#### background

Change the background color for your personal title.

```ts
import { describe, assert } from 'poku';

describe('Group A', { background: 'blue' });
describe('Test Title', { background: 'blue' });

assert.ok(true, '1');
assert.ok(true, '2');
```
Expand All @@ -57,7 +56,7 @@ assert.ok(true, '2');

</FAQ>

### icon (prefix)
#### icon (prefix)

**Poku** also allows the prefix customization.

Expand All @@ -66,42 +65,37 @@ assert.ok(true, '2');
```ts
import { describe, assert } from 'poku';

describe('Group A', { icon: '🚀' });
describe('Test Title', { icon: '🔬' });

assert.ok(true, '1');
assert.ok(true, '2');
```

<hr />
## Grouping tests (usual)

## Overview

<FAQ title='Source'>

```ts
import { assert, describe } from 'poku';

describe('Needs to Succeed', {
icon: '🚀',
});
```ts
import { describe, assert } from 'poku';

assert.ok(true, 'Test 1');
assert.ok(true, 'Test 2');
describe(() => {
assert.equal(1 + 1, 2, '1 + 1 should be 2');
assert.equal(2 + 2, 4, '2 + 2 should be 4');
});
```

describe('Needs to Fail', {
background: 'yellow',
icon: '🚫',
});
```ts
import { describe, assert } from 'poku';

assert.throws(() => { throw new Error() }, 'Test 1');
assert.throws(() => { throw new Error() }, 'Test 2');
```
describe('Sum tests', () => {
assert.equal(1 + 1, 2);
assert.equal(2 + 2, 4);
});
```

</FAQ>
```ts
import { describe, assert } from 'poku';

<table>
<tr>
<td>
<img src={Example} />
</td>
</tr>
</table>
describe('Sum tests', () => {
assert.equal(1 + 1, 2, '1 + 1 should be 2');
assert.equal(2 + 2, 4, '2 + 2 should be 4');
});
```
Binary file removed website/static/img/describe-example.png
Binary file not shown.
Loading