Skip to content

Commit

Permalink
feat(test): support for Node.js familiar API usage (#352)
Browse files Browse the repository at this point in the history
* feat: support for Node.js `test` familiar API usage

* refactor: better organize types and configs

* docs: include all `test` approaches

* ci: use imports from modules
  • Loading branch information
wellwelwel authored Jun 9, 2024
1 parent 6e7def5 commit 1c425e1
Show file tree
Hide file tree
Showing 29 changed files with 192 additions and 88 deletions.
4 changes: 2 additions & 2 deletions benchmark/test/poku/failure.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { test, assert } from 'poku';
import { sum } from '../../src/sum.js';

test(() => {
assert.equal(sum(4, 4), 4, 'should add 4 + 4');
test('should add 4 + 4', () => {
assert.equal(sum(4, 4), 4);
});
4 changes: 2 additions & 2 deletions benchmark/test/poku/success.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { test, assert } from 'poku';
import { sum } from '../../src/sum.js';

test(() => {
assert.equal(sum(2, 2), 4, 'should add 2 + 2');
test('should add 2 + 2', () => {
assert.equal(sum(2, 2), 4);
});
18 changes: 18 additions & 0 deletions src/@types/describe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* c8 ignore start */

import { type backgroundColor } from '../helpers/format.js';

export type DescribeOptions = {
background?: keyof typeof backgroundColor | boolean;
/**
* @default "☰"
*/
icon?: string;
/** @deprecated */
pad?: boolean;
/**
* @default "grey"
*/
};

/* c8 ignore stop */
2 changes: 2 additions & 0 deletions src/configs/indentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
export const indentation = {
test: ' ',
stdio: ' ',
describeCounter: 0,
testCounter: 0,
};

/* c8 ignore stop */
4 changes: 2 additions & 2 deletions src/helpers/parse-assertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { format } from './format.js';
import { hr } from './hr.js';
import { findFile } from './find-file.js';
import { each } from '../configs/each.js';
import { describeCounter } from '../modules/describe.js';
import { indentation } from '../configs/indentation.js';
import { fromEntries, entries } from '../polyfills/object.js';
import { nodeVersion } from './get-runtime.js';
import { write } from './logs.js';
Expand Down Expand Up @@ -56,7 +56,7 @@ export const parseAssertion = async (
const isPoku =
typeof process.env?.FILE === 'string' && process.env?.FILE.length > 0;
const FILE = process.env.FILE;
const preIdentation = describeCounter > 0 ? ' ' : '';
const preIdentation = indentation.describeCounter > 0 ? ' ' : '';

try {
if (typeof each.before.cb === 'function' && each.before.assert) {
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
export { poku } from './modules/poku.js';
export { exit } from './modules/exit.js';
export { assert } from './modules/assert.js';
export { describe, log } from './modules/describe.js';
export { describe } from './modules/describe.js';
export { log } from './modules/log.js';
export { assertPromise } from './modules/assert-promise.js';
export { beforeEach, afterEach } from './modules/each.js';
export { publicListFiles as listFiles } from './modules/list-files-sync.js';
Expand Down
34 changes: 6 additions & 28 deletions src/modules/describe.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,8 @@
/* c8 ignore start */

import { format, backgroundColor } from '../helpers/format.js';
import { write } from '../helpers/logs.js';

export let describeCounter = 0;

export type DescribeOptions = {
/** @deprecated */
pad?: boolean;
/**
* @default "grey"
*/
background?: keyof typeof backgroundColor | boolean;
/**
* @default "☰"
*/
icon?: string;
};

/**
* By default **Poku** only shows outputs generated from itself.
* This helper allows you to use an alternative to `console.log` with **Poku**.
*
* Need to debug? Just use the [`debug`](https://poku.io/docs/documentation/poku/options/debug) option from `poku`.
*/
export const log = (message: string) => write(`\x1b[0m${message}\x1b[0m`);
import { indentation } from '../configs/indentation.js';
/* c8 ignore next */
import type { DescribeOptions } from '../@types/describe.js';

/**
* On **Poku**, `describe` is just a pretty `console.log` to title your test suites in the terminal.
Expand All @@ -35,8 +13,9 @@ export const describe = (title: string, options?: DescribeOptions) => {
const message = `${icon || '☰'} ${title}`;
const noBackground = !background;

describeCounter++;
indentation.describeCounter++;

/* c8 ignore start */
if (noBackground) {
write(`${format.bold(message)}`);
return;
Expand All @@ -45,6 +24,5 @@ export const describe = (title: string, options?: DescribeOptions) => {
write(
`${format.bg(backgroundColor[typeof background === 'string' ? background : 'grey'], message)}`
);
/* c8 ignore stop */
};

/* c8 ignore stop */
13 changes: 13 additions & 0 deletions src/modules/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* c8 ignore start */

import { write } from '../helpers/logs.js';

/**
* By default **Poku** only shows outputs generated from itself.
* This helper allows you to use an alternative to `console.log` with **Poku**.
*
* Need to debug? Just use the [`debug`](https://poku.io/docs/documentation/poku/options/debug) option from `poku`.
*/
export const log = (message: string) => write(`\x1b[0m${message}\x1b[0m`);

/* c8 ignore stop */
23 changes: 22 additions & 1 deletion src/modules/test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,39 @@
/* c8 ignore next */
import { each } from '../configs/each.js';
import { describe } from './describe.js';

export async function test(
message: string,
cb: () => Promise<unknown>
): Promise<void>;
export async function test(message: string, cb: () => unknown): Promise<void>;
export async function test(cb: () => Promise<unknown>): Promise<void>;
export function test(cb: () => unknown): void;
export async function test(
cb: () => unknown | Promise<unknown>
...args: [
string | (() => unknown | Promise<unknown>),
(() => unknown | Promise<unknown>)?,
]
): Promise<void> {
let message: string | undefined;
let cb: () => unknown | Promise<unknown>;

if (typeof each.before.cb === 'function' && each.before.test) {
const beforeResult = each.before.cb();

/* c8 ignore next */
if (beforeResult instanceof Promise) await beforeResult;
}

if (typeof args[0] === 'string') {
message = args[0];
cb = args[1] as () => unknown | Promise<unknown>;
} else cb = args[0] as () => unknown | Promise<unknown>;

if (message) describe(message, { icon: '›' });

const resultCb = cb();

/* c8 ignore next */
if (resultCb instanceof Promise) await resultCb;

Expand Down
2 changes: 1 addition & 1 deletion test/ci.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { poku } from '../src/index.js';
import { poku } from '../src/modules/poku.js';

poku(['./test/compatibility'], {
parallel: true,
Expand Down
15 changes: 4 additions & 11 deletions test/e2e/background-process.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import {
startService,
assert,
test,
describe,
startScript,
} from '../../src/index.js';
import { describe } from '../../src/modules/describe.js';
import { test } from '../../src/modules/test.js';
import { assert } from '../../src/modules/assert.js';
import { startScript, startService } from '../../src/modules/create-service.js';
import { legacyFetch } from '../helpers/legacy-fetch.test.js';
import { ext, isProduction } from '../helpers/capture-cli.test.js';
import { getRuntime } from '../../src/helpers/get-runtime.js';
Expand Down Expand Up @@ -57,7 +54,6 @@ import { getRuntime } from '../../src/helpers/get-runtime.js';

await test(async () => {
describe('Start Service (Multiple Ports)', {
background: false,
icon: '🔀',
});

Expand All @@ -81,7 +77,6 @@ import { getRuntime } from '../../src/helpers/get-runtime.js';

await test(async () => {
describe('Start Script (Multiple Ports)', {
background: false,
icon: '🔀',
});

Expand All @@ -107,7 +102,6 @@ import { getRuntime } from '../../src/helpers/get-runtime.js';
if (runtime === 'node') {
await test(async () => {
describe('Start Service (No Ports)', {
background: false,
icon: '🔀',
});

Expand All @@ -133,7 +127,6 @@ import { getRuntime } from '../../src/helpers/get-runtime.js';

await test(async () => {
describe('Start Script (No Ports)', {
background: false,
icon: '🔀',
});

Expand Down
4 changes: 3 additions & 1 deletion test/e2e/cli.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { assert, describe, test } from '../../src/index.js';
import { describe } from '../../src/modules/describe.js';
import { test } from '../../src/modules/test.js';
import { assert } from '../../src/modules/assert.js';
import { executeCLI, ext, isProduction } from '../helpers/capture-cli.test.js';
import { getRuntime } from '../../src/helpers/get-runtime.js';

Expand Down
5 changes: 4 additions & 1 deletion test/e2e/exit-code.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { poku, assert, describe, test } from '../../src/index.js';
import { describe } from '../../src/modules/describe.js';
import { test } from '../../src/modules/test.js';
import { poku } from '../../src/modules/poku.js';
import { assert } from '../../src/modules/assert.js';

describe('Poku Runner Suite', { icon: '🐷' });

Expand Down
1 change: 0 additions & 1 deletion test/integration/assert/assert-promise-no-message.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { nodeVersion } from '../../../src/helpers/get-runtime.js';
import { assertPromise as assert, describe, test } from '../../../src/index.js';

describe('Assert Promise Suite (No Message)', {
background: false,
icon: '🔬',
});

Expand Down
2 changes: 2 additions & 0 deletions test/integration/describe/describe.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// import { describe } from '../../../src/modules/describe.js';
// import { test } from '../../../src/modules/test.js';
12 changes: 4 additions & 8 deletions test/integration/each/each-promise.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import {
describe,
assert,
beforeEach,
afterEach,
test,
} from '../../../src/index.js';
import { describe } from '../../../src/modules/describe.js';
import { test } from '../../../src/modules/test.js';
import { assert } from '../../../src/modules/assert.js';
import { beforeEach, afterEach } from '../../../src/modules/each.js';

describe('Asynchronous Before and After Each Suite', {
background: false,
icon: '🔬',
});

Expand Down
12 changes: 4 additions & 8 deletions test/integration/each/each.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import {
describe,
assert,
beforeEach,
afterEach,
test,
} from '../../../src/index.js';
import { describe } from '../../../src/modules/describe.js';
import { test } from '../../../src/modules/test.js';
import { assert } from '../../../src/modules/assert.js';
import { beforeEach, afterEach } from '../../../src/modules/each.js';

describe('Before and After Each Suite', {
background: false,
icon: '🔬',
});

Expand Down
26 changes: 23 additions & 3 deletions test/integration/test/test.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
import { test } from '../../../src/index.js';
import { describe } from '../../../src/modules/describe.js';
import { test } from '../../../src/modules/test.js';

(async () => {
describe('Testing "test" method', {
icon: '🔬',
});

test(async () => {
test(() => {});
test(() => true);
test(() => false);
test(() => undefined);
test(() => new Promise((resolve) => resolve(undefined)));
test(async () => await new Promise((resolve) => resolve(undefined)));

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

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

await test('', () => new Promise((resolve) => resolve(undefined)));
await test('', async () =>
await new Promise((resolve) => resolve(undefined)));
});
2 changes: 1 addition & 1 deletion test/run.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { poku } from '../src/index.js';
import { poku } from '../src/modules/poku.js';

poku(['test/unit', 'test/integration', 'test/e2e'], {
parallel: true,
Expand Down
4 changes: 3 additions & 1 deletion test/unit/assert.result-type.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { test, assert, describe } from '../../src/index.js';
import { describe } from '../../src/modules/describe.js';
import { test } from '../../src/modules/test.js';
import { assert } from '../../src/modules/assert.js';
import { parseResultType } from '../../src/helpers/parse-assertion.js';
import { nodeVersion } from '../../src/helpers/get-runtime.js';

Expand Down
4 changes: 3 additions & 1 deletion test/unit/deno/allow.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { assert, describe, test } from '../../../src/index.js';
import { describe } from '../../../src/modules/describe.js';
import { test } from '../../../src/modules/test.js';
import { assert } from '../../../src/modules/assert.js';
import { runner } from '../../../src/helpers/runner.js';

describe('Deno Permissions (Allow)', { icon: '🔬' });
Expand Down
4 changes: 3 additions & 1 deletion test/unit/deno/deny.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { assert, describe, test } from '../../../src/index.js';
import { describe } from '../../../src/modules/describe.js';
import { test } from '../../../src/modules/test.js';
import { assert } from '../../../src/modules/assert.js';
import { runner } from '../../../src/helpers/runner.js';

describe('Deno Permissions (Deny)', { icon: '🔬' });
Expand Down
2 changes: 1 addition & 1 deletion test/unit/pad.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assert } from '../../src/index.js';
import { assert } from '../../src/modules/assert.js';
import { padStart } from '../../src/polyfills/pad.js';

assert.deepStrictEqual(padStart('', 0, ''), '');
Expand Down
4 changes: 3 additions & 1 deletion test/unit/run-test-file.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import process from 'node:process';
import { assert, describe, test } from '../../src/index.js';
import { describe } from '../../src/modules/describe.js';
import { test } from '../../src/modules/test.js';
import { assert } from '../../src/modules/assert.js';
import { runTestFile } from '../../src/services/run-test-file.js';
import { getRuntime } from '../../src/helpers/get-runtime.js';

Expand Down
4 changes: 3 additions & 1 deletion test/unit/run-tests.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { assert, describe, test } from '../../src/index.js';
import { describe } from '../../src/modules/describe.js';
import { test } from '../../src/modules/test.js';
import { assert } from '../../src/modules/assert.js';
import { runTests } from '../../src/services/run-tests.js';

describe('Service: runTests', { icon: '🔬' });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
"link": {
"type": "generated-index"
},
"position": 2
"position": 3
}
Loading

0 comments on commit 1c425e1

Please sign in to comment.