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: add test helper #71

Merged
merged 1 commit into from
Mar 4, 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
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,25 @@ deno run npm:poku

---

## Available Methods

### Essentials

- `poku` (_test runner_)
- `assert` and `assertPromise` (_test assertion_)

### Helpers

- `beforeEach` and `afterEach`
- `test`
- `describe` and `log`
- `listFiles`
- `exit`

[**See the complete documentation**](https://poku.io/docs).

---

## Overview

### `poku`
Expand Down
6 changes: 6 additions & 0 deletions src/configs/each.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export type Control = {

export type EachConfigs = {
status: boolean;
assert?: boolean;
test?: boolean;
cb?: () => unknown | Promise<unknown>;
};

Expand All @@ -16,9 +18,13 @@ export const each: {
before: {
status: true,
cb: undefined,
assert: true,
test: true,
},
after: {
status: true,
cb: undefined,
assert: true,
test: true,
},
};
4 changes: 2 additions & 2 deletions src/helpers/parseAsssetion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ export const parseAssertion = async (
const FILE = process.env.FILE;

try {
if (typeof each.before.cb === 'function') {
if (typeof each.before.cb === 'function' && each.before.assert) {
const beforeResult = each.before.cb();
if (beforeResult instanceof Promise) await beforeResult;
}

const cbResult = cb();
if (cbResult instanceof Promise) await cbResult;

if (typeof each.after.cb === 'function') {
if (typeof each.after.cb === 'function' && each.after.assert) {
const afterResult = each.after.cb();
if (afterResult instanceof Promise) await afterResult;
}
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export { describe, log } from './modules/describe.js';
export { assertPromise } from './modules/assert-promise.js';
export { beforeEach, afterEach } from './modules/each.js';
export { publicListFiles as listFiles } from './modules/list-files.js';
export { test } from './modules/test.js';
export type { Code } from './@types/code.ts';
export type { Configs } from './@types/poku.ts';
export type { Configs as ListFilesConfigs } from './@types/list-files.ts';
15 changes: 14 additions & 1 deletion src/modules/each.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Control, each } from '../configs/each.js';

type EachOptions = {
immediate?: boolean;
test?: boolean;
assert?: boolean;
};

/**
Expand Down Expand Up @@ -29,6 +31,10 @@ export const beforeEach = (
callback: () => unknown,
options?: EachOptions
): Control => {
each.before.test = typeof options?.test === 'boolean' ? options.test : true;
each.before.assert =
typeof options?.assert === 'boolean' ? options.assert : true;

options?.immediate && callback();

each.before.cb = () => {
Expand Down Expand Up @@ -71,7 +77,14 @@ export const beforeEach = (
* after.reset();
* ```
*/
export const afterEach = (callback: () => unknown): Control => {
export const afterEach = (
callback: () => unknown,
options: Omit<EachOptions, 'immediate'>
): Control => {
each.after.test = typeof options?.test === 'boolean' ? options.test : true;
each.after.assert =
typeof options?.assert === 'boolean' ? options.assert : true;

each.after.cb = () => {
if (each.after.status) callback();
};
Expand Down
20 changes: 20 additions & 0 deletions src/modules/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { each } from '../configs/each.js';

export async function test(cb: () => Promise<unknown>): Promise<void>;
export function test(cb: () => unknown): void;
export async function test(
cb: () => unknown | Promise<unknown>
): Promise<void> {
if (typeof each.before.cb === 'function' && each.before.test) {
const beforeResult = each.before.cb();
if (beforeResult instanceof Promise) await beforeResult;
}

const resultCb = cb();
if (resultCb instanceof Promise) await resultCb;

if (typeof each.after.cb === 'function' && each.after.test) {
const afterResult = each.after.cb();
if (afterResult instanceof Promise) await afterResult;
}
}
2 changes: 1 addition & 1 deletion website/docs/documentation/helpers/log.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 3
sidebar_position: 4
---

# log
Expand Down
66 changes: 66 additions & 0 deletions website/docs/documentation/helpers/test.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
---
sidebar_position: 3
---

# test

> `test(() => void)`

`test` is a helper to assist you in such cases:

- Use the beforeEach and afterEach for each `test` performed
- Isolate or group your tests in the same file

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

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

beforeEach(() => prepareService(), {
assert: false,
});

afterEach(() => resetService(), {
assert: false,
});

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

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

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

## By using promises

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

const prepareService = () =>
new Promise((resolve) => resolve(true), {
assert: false,
});

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

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

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

await test(async () => {
// do anything you want
});
```
13 changes: 8 additions & 5 deletions website/docusaurus.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,16 @@ const config: Config = {
defaultMode: 'dark',
respectPrefersColorScheme: false,
},
algolia: {
appId: '8W3D1A9OL6',
apiKey: '7e1ef3de299364cedc6f3240f7f00063',
indexName: 'poku',
searchPagePath: false,
contextualSearch: false,
},
} satisfies Preset.ThemeConfig,

plugins: [
'docusaurus-plugin-sass',
'@easyops-cn/docusaurus-search-local',
navbarLocalePlugin,
],
plugins: ['docusaurus-plugin-sass', navbarLocalePlugin],
};

export default config;
Loading
Loading