Skip to content

Commit

Permalink
fix: conflict when using "eachs" for both test and assert (#117)
Browse files Browse the repository at this point in the history
* docs: improve description

* docs: improve documentation

* fix: conflict when using "eachs" for both `test` and `assert`

* docs: improve overview
  • Loading branch information
wellwelwel authored Mar 16, 2024
1 parent b5f4539 commit 68e72eb
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 64 deletions.
1 change: 1 addition & 0 deletions .github/assets/readme/check.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .github/assets/readme/parallel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .github/assets/readme/sequential.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
61 changes: 39 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,49 +15,59 @@
[downloads-image]: https://img.shields.io/npm/dt/poku.svg?&color=FFC312&label=Downloads
[downloads-url]: https://npmjs.org/package/poku

# Poku

<img align="right" width="128" height="128" alt="Logo" src="https://raw.githubusercontent.com/wellwelwel/poku/main/.github/assets/readme/poku.svg">
<div align="center">
<img width="170" height="170" alt="Logo" src="https://raw.githubusercontent.com/wellwelwel/poku/main/.github/assets/readme/poku.svg">

[**English**](https://poku.io/docs) | [**Português (BR)**](https://poku.io/pt-BR/docs)
# Poku

**Poku** is your Test Runner Pet for [**Node.js**][node-version-url], [**Bun**][bun-version-url] and [**Deno**][deno-version-url].
**Poku** can show you _how simple testing can be_ 🌱

[![Node.js Version][node-version-image]][node-version-url]
[![Bun Version][bun-version-image]][bun-version-url]
[![Deno Version][deno-version-image]][deno-version-url]
[![TypeScript Version][typescript-version-image]][typescript-url]
[![TypeScript Version][typescript-version-image]][typescript-url]<br />
[![GitHub Workflow Status (with event)][ci-image]][ci-url]
[![GitHub Workflow Status (with event)][ql-image]][ql-url]
[![NPM Downloads][downloads-image]][downloads-url]
[![License][license-image]][license-url]

<!-- ![Codecov](https://img.shields.io/codecov/c/github/wellwelwel/poku?label=Coverage) -->

Enjoying **Poku**? Consider giving him a star ⭐️

</div>

---

🐷 [**Documentation**](https://poku.io) • 🧪 [**Examples**](https://poku.io/docs/category/examples) • 🔬 [**Compare the Most Popular Test Runners**](https://poku.io/docs/comparing)
<div align="center">

🐷 [**Documentation**](https://poku.io)<span>&nbsp;&nbsp;&nbsp;&nbsp;</span>🧪 [**Examples**](https://poku.io/docs/category/examples)<span>&nbsp;&nbsp;&nbsp;&nbsp;</span>🔬 [**Compare the Most Popular Test Runners**](https://poku.io/docs/comparing)

</div>

---

## Why Poku?
## Reasons to Adopt a Poku →

<img width="16" height="16" alt="check" src="https://raw.githubusercontent.com/wellwelwel/poku/main/.github/assets/readme/check.svg"> No configurations<br />
<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><img width="16" height="16" alt="check" src="https://raw.githubusercontent.com/wellwelwel/poku/main/.github/assets/readme/check.svg"> Auto detect **ESM** and **CJS**<br />
<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><img width="16" height="16" alt="check" src="https://raw.githubusercontent.com/wellwelwel/poku/main/.github/assets/readme/check.svg"> Auto detect **Typescript** files<br />
<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><img width="16" height="16" alt="check" src="https://raw.githubusercontent.com/wellwelwel/poku/main/.github/assets/readme/check.svg"> Don't export your **server** (_just run it_) 🚀<br />

**Poku** can show you how simple testing can be 🌱
<img width="16" height="16" alt="check" src="https://raw.githubusercontent.com/wellwelwel/poku/main/.github/assets/readme/check.svg"> Less verbose<br />
<img width="16" height="16" alt="check" src="https://raw.githubusercontent.com/wellwelwel/poku/main/.github/assets/readme/check.svg"> High **isolation** level per file<br />
<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><img width="16" height="16" alt="check" src="https://raw.githubusercontent.com/wellwelwel/poku/main/.github/assets/readme/check.svg"> No eval needed 🔐<br />
<img width="16" height="16" alt="check" src="https://raw.githubusercontent.com/wellwelwel/poku/main/.github/assets/readme/check.svg"> **Parallel** and **Sequential** runs 🏃🏽🏃🏻<br />

- No configurations
- Supports **ESM** and **CJS**
- High **isolation** level per file
- **Poku** is [**100%** documented](https://poku.io/docs)
- Zero external dependencies
- **Parallel** and **Sequential** runs
- Designed to be highly intuitive
- Compatible with **Coverage** tools
- [**Node.js**][node-version-url], [**Bun**][bun-version-url] and [**Deno**][deno-version-url] compatibility
- You don't need to learn it all at once to get started 🧑🏻‍🎓
<img width="16" height="16" alt="check" src="https://raw.githubusercontent.com/wellwelwel/poku/main/.github/assets/readme/check.svg"> **Poku** is [**100%** documented](https://poku.io/docs)<br />
<img width="16" height="16" alt="check" src="https://raw.githubusercontent.com/wellwelwel/poku/main/.github/assets/readme/check.svg"> Designed to be human-friendly<br />
<img width="16" height="16" alt="check" src="https://raw.githubusercontent.com/wellwelwel/poku/main/.github/assets/readme/check.svg"> **Poku** doesn't use a global state<br />
<img width="16" height="16" alt="check" src="https://raw.githubusercontent.com/wellwelwel/poku/main/.github/assets/readme/check.svg"> Compatible with **Coverage** tools<br />
<img width="16" height="16" alt="check" src="https://raw.githubusercontent.com/wellwelwel/poku/main/.github/assets/readme/check.svg"> [**Node.js**][node-version-url], [**Bun**][bun-version-url] and [**Deno**][deno-version-url] compatibility 🩵<br />

> _Poku adapts to your test, not the other way around._
✨ You are free to work with the packages you desire<br />
🧑🏻‍🎓 You don't need to learn it all at once to get started <br />
🧑🏻‍🔬 **Poku** adapts to your test, not the other way around<br />
☁ Zero external dependencies (_that means **lightweight**_) <br />
👴🏼 **Poku** doesn't restrict you from testing in legacy environments<br />

---

Expand Down Expand Up @@ -251,6 +261,13 @@ Please check the [**SECURITY.md**](./SECURITY.md) and the section [**Is Poku Saf

---

## Limitations

- **Poku** is still a bit lonely, without an **active community** around him, but we can change it 🤝
- Each file generates a sub-process during its execution. This can be reflected in projects with an extremely high volume of test files when run in parallel.

---

## Acknowledgements

[![Contributors](https://img.shields.io/github/contributors/wellwelwel/poku)](https://github.com/wellwelwel/poku/graphs/contributors)
Expand Down
4 changes: 2 additions & 2 deletions src/configs/each.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ export const each: {
before: {
status: true,
cb: undefined,
assert: true,
assert: false,
test: true,
},
after: {
status: true,
cb: undefined,
assert: true,
assert: false,
test: true,
},
};
4 changes: 2 additions & 2 deletions src/modules/each.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const beforeEach = (
): Control => {
each.before.test = typeof options?.test === 'boolean' ? options.test : true;
each.before.assert =
typeof options?.assert === 'boolean' ? options.assert : true;
typeof options?.assert === 'boolean' ? options.assert : false;

options?.immediate && callback();

Expand Down Expand Up @@ -83,7 +83,7 @@ export const afterEach = (
): Control => {
each.after.test = typeof options?.test === 'boolean' ? options.test : true;
each.after.assert =
typeof options?.assert === 'boolean' ? options.assert : true;
typeof options?.assert === 'boolean' ? options.assert : false;

each.after.cb = () => {
if (each.after.status) callback();
Expand Down
2 changes: 1 addition & 1 deletion website/docs/comparing.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import TabItem from '@theme/TabItem';

# Comparing Test Runners

**Comparison using **Node.js**, **TypeScript** (_no compile_) and **ESM** to show a simple error test:**
**Comparison using **TypeScript** (_no compile_) and **ESM** to show a simple error test:**

- Let's starting from installation 🔬

Expand Down
56 changes: 50 additions & 6 deletions website/docs/documentation/helpers/before-after-each/in-code.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,56 @@ import TabItem from '@theme/TabItem';

# In-Code

## How can I combine `beforeEach`, `afterEach` and `assert`?
## `beforeEach` and `afterEach`

Both `beforeEach`, `afterEach` are recommended for tests that consume a particular global state for each test.<br />
For example, by populating or resetting a database before and/or after multiple assertions.

### See how Poku solve it 🧙🏻
### Basic usage

```ts
import { test, beforeEach, afterEach } from 'poku';

const prepareService = () => true;
const resetService = () => true;

beforeEach(() => prepareService());

afterEach(() => resetService());

test(() => {
// do anything you want
});

test(() => {
// do anything you want
});
```

### By using promises

```ts
import { test, beforeEach, afterEach } from 'poku';

const prepareService = () => new Promise((resolve) => resolve(true));

const resetService = () => new Promise((resolve) => resolve(true));

beforeEach(async () => await prepareService());
afterEach(async () => await resetService());

await test(async () => {
// do anything you want
});

await test(async () => {
// do anything you want
});
```

## Combine `beforeEach`, `afterEach` and `assert`

Since `test` is completely optional on **Poku**, you can use `beforeEach`, `afterEach` and `assert` together:

<Tabs>
<TabItem default value='Using beforeEach and afterEach'>
Expand All @@ -23,8 +67,8 @@ For example, by populating or resetting a database before and/or after multiple
const prepareService = () => true;
const resetService = () => true;

beforeEach(() => prepareService());
afterEach(() => resetService());
beforeEach(() => prepareService(), { assert: true });
afterEach(() => resetService(), { assert: true });

assert(true, 'Test A');
assert(true, 'Test B');
Expand All @@ -51,8 +95,8 @@ import { assert, beforeEach, afterEach } from 'poku';
let value = 0;
beforeEach(() => ++value, { immediate: true });
afterEach(() => ++value);
beforeEach(() => ++value, { immediate: true, assert: true });
afterEach(() => ++value, { assert: true });
assert.equal(value, 1); // ✅
assert.equal(value, 3); // ✅
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ import Failure from '@site/static/img/each-fail.png';

<hr />

:::tip
The "per-file" mode comes close to plugins, even though that wasn't the original intention.
:::

:::danger
Although it also works with `parallel` runs, it's strongly discouraged to use these features for concurrent tests.
:::
55 changes: 24 additions & 31 deletions website/docs/documentation/helpers/test.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,59 +8,52 @@ sidebar_position: 3
`test` is a helper to assist you in such cases:

- Use the beforeEach and afterEach for each `test` performed
- Use the [`beforeEach` and `afterEach`](/docs/documentation/helpers/before-after-each/in-code) for each `test` performed
- Isolate or group your tests in the same file
- Run tests in the same file in parallel

```ts
import { test, beforeEach, afterEach } from 'poku';
## Basic Usage

const prepareService = () => true;
const resetService = () => true;
```ts
import { test, assert } from 'poku';

beforeEach(() => prepareService(), {
assert: false,
});
test(() => {
const myVar = 'a';

afterEach(() => resetService(), {
assert: false,
assert.strictEqual(myVar, 'a', 'My first test helper');
});

test(() => {
// do anything you want
});
const myVar = 'b';

test(() => {
// do anything you want
assert.strictEqual(myVar, 'b', 'My second test helper');
});
```

:::info
Ensure you disabled the assert on **beforeEach** and **afterEach**.
:::

## By using promises

```ts
import { test, beforeEach, afterEach } from 'poku';
import { test, assert } from 'poku';

await test(async () => {
// do anything you want
});

const prepareService = () =>
new Promise((resolve) => resolve(true), {
assert: false,
});
await test(async () => {
// do anything you want
});
```

const resetService = () =>
new Promise((resolve) => resolve(true), {
assert: false,
});
## Running in Parallel

beforeEach(async () => await prepareService());
afterEach(async () => await resetService());
```ts
import { test, assert } from 'poku';

await test(async () => {
test(async () => {
// do anything you want
});

await test(async () => {
test(async () => {
// do anything you want
});
```
Binary file modified website/static/img/parallel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified website/static/img/sequential.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 68e72eb

Please sign in to comment.