Skip to content

Commit

Permalink
feat(logs): simplify the describe (#63)
Browse files Browse the repository at this point in the history
* docs: update philosophy

* chore: add Deno local playground

* feat(logs): simplify the describe

* fix: allows local variables in beforeEach

* refactor(assert): simplify code style
  • Loading branch information
wellwelwel authored Mar 3, 2024
1 parent 9b2d643 commit 790ced0
Show file tree
Hide file tree
Showing 20 changed files with 773 additions and 654 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"test:deno": "FILTER='deno-' npm run test:ci",
"test:bun": "FILTER='bun-' npm run test:ci",
"prebuild": "rm -rf ./lib ./ci",
"predocker:deno": "docker compose -f ./test/docker/playground/deno/docker-compose.yml down",
"docker:deno": "docker compose -f ./test/docker/playground/deno/docker-compose.yml up --build",
"build": "npx tsc; npx tsc -p tsconfig.test.json",
"postbuild": "npx tsx ./tools/compatibility/node.ts; chmod +x lib/bin/index.js; npm audit",
"eslint:checker": "npx eslint . --ext .js,.ts",
Expand Down
24 changes: 24 additions & 0 deletions src/configs/each.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export type Control = {
pause: () => void;
continue: () => void;
reset: () => void;
};

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

export const each: {
before: EachConfigs;
after: EachConfigs;
} = {
before: {
status: true,
cb: undefined,
},
after: {
status: true,
cb: undefined,
},
};
31 changes: 31 additions & 0 deletions src/helpers/find-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { EOL } from 'node:os';

export const findFile = (error: Error) => {
const stackLines = error.stack?.split(EOL) || [];

let file = '';

const basePath = 'poku/lib/';

for (const line of stackLines) {
if (!line.includes(basePath)) {
const match = line.match(
/at\s(\/.+|file:.+)|^(\s+)at\smodule\scode\s\((\/.+|file:.+)\)/i
);

// Node and Deno
if (match && match[1]) {
file = match[1];
break;
}

// Bun
if (match && match[3]) {
file = match[3];
break;
}
}
}

return file;
};
18 changes: 18 additions & 0 deletions src/helpers/format.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import { padStart } from './pad.js';

export const backgroundColor = {
white: 7,
black: 40,
grey: 100,
red: 41,
green: 42,
yellow: 43,
blue: 44,
magenta: 45,
cyan: 46,
brightRed: 101,
brightGreen: 102,
brightYellow: 103,
brightBlue: 104,
brightMagenta: 105,
brightCyan: 106,
} as const;

export const format = {
counter: (current: number, total: number, pad = '0') => {
const totalDigits = String(total).length;
Expand Down
57 changes: 19 additions & 38 deletions src/helpers/parseAsssetion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import assert from 'node:assert';
import { EOL } from 'node:os';
import { format } from './format.js';
import { hr } from './hr.js';
import { findFile } from './find-file.js';
import { each } from '../configs/each.js';

export type ParseAssertionOptions = {
message?: string | Error;
Expand All @@ -14,48 +16,27 @@ export type ParseAssertionOptions = {
hideDiff?: boolean;
};

const findFile = (error: Error) => {
const stackLines = error.stack?.split(EOL) || [];

let file = '';

const basePath = 'poku/lib/';

for (const line of stackLines) {
if (!line.includes(basePath)) {
const match = line.match(
/at\s(\/.+|file:.+)|^(\s+)at\smodule\scode\s\((\/.+|file:.+)\)/i
);

// Node and Deno
if (match && match[1]) {
file = match[1];
break;
}

// Bun
if (match && match[3]) {
file = match[3];
break;
}
}
}

return file;
};

const formatFail = (str: string) => format.bold(format.fail(`✘ ${str}`));

export const parseAssertion = (
cb: () => void,
export const parseAssertion = async (
cb: () => void | Promise<void>,
options: ParseAssertionOptions
) => {
const isPoku =
typeof process.env?.FILE === 'string' && process.env?.FILE.length > 0;
const FILE = process.env.FILE;

try {
cb();
if (typeof each.before.cb === 'function') {
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') {
const afterResult = each.after.cb();
if (afterResult instanceof Promise) await afterResult;
}

if (typeof options.message === 'string') {
const message = isPoku
Expand All @@ -67,7 +48,7 @@ export const parseAssertion = (
} catch (error) {
if (error instanceof assert.AssertionError) {
const { code, actual, expected, operator } = error;
const absoultePath = findFile(error).replace(/file:/, '');
const absoultePath = findFile(error).replace(/file:(\/\/)?/, '');
const file = path.relative(path.resolve(process.cwd()), absoultePath);

let message: string = '';
Expand All @@ -80,8 +61,8 @@ export const parseAssertion = (

const finalMessage =
message?.trim().length > 0
? `${formatFail(message)}`
: `${formatFail('No Message')}`;
? format.bold(format.fail(`✘ ${message}`))
: format.bold(format.fail('✘ No Message'));

console.log(
isPoku
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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 { assertPromise } from './modules/assert-promise.js';
export { beforeEach, afterEach } from './modules/each.js';
export { publicListFiles as listFiles } from './modules/list-files.js';
Expand Down
Loading

0 comments on commit 790ced0

Please sign in to comment.