Skip to content

Commit

Permalink
feat: add stable only modifier to describe, it and test metho…
Browse files Browse the repository at this point in the history
…ds (#901)

* feat: add `only` modifier to `describe`, `it` and `test` methods

* chore: fix lint

* ci: add tests

* ci: add tests

* ci: add tests

* ci: remove old tests

* ci: add ordered tests

* docs: add `only` modifier to `describe`, `it` and `test` methods

* docs: improve only documentation
  • Loading branch information
wellwelwel authored Dec 21, 2024
1 parent 720616b commit d6b6241
Show file tree
Hide file tree
Showing 24 changed files with 760 additions and 563 deletions.
5 changes: 4 additions & 1 deletion src/configs/poku.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { cwd } from 'node:process';
import { env, cwd } from 'node:process';

export const results = {
success: 0,
Expand All @@ -13,4 +13,7 @@ export const deepOptions: string[] = [];

export const GLOBAL = {
cwd: cwd(),
runAsOnly: false,
isPoku: typeof env?.POKU_FILE === 'string' && env?.POKU_FILE.length > 0,
FILE: env.POKU_FILE,
};
1 change: 0 additions & 1 deletion src/modules/essentials/poku.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export async function poku(
const concurrency = await Promise.all(promises);

if (concurrency.some((result) => !result)) code = 1;
} catch {
} finally {
const end = process.hrtime(start);
const total = (end[0] * 1e3 + end[1] / 1e6).toFixed(6);
Expand Down
25 changes: 18 additions & 7 deletions src/modules/helpers/describe.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type { DescribeOptions } from '../../@types/describe.js';
import { hrtime, env } from 'node:process';
import { hrtime } from 'node:process';
import { format } from '../../services/format.js';
import { Write } from '../../services/write.js';
import { indentation } from '../../configs/indentation.js';
import { todo, skip, onlyDescribe } from './modifiers.js';
import { hasDescribeOnly, hasOnly } from '../../parsers/get-arg.js';
import { hasOnly } from '../../parsers/get-arg.js';
import { checkOnly } from '../../parsers/callback.js';
import { GLOBAL } from '../../configs/poku.js';

export async function describeBase(
arg1: string | (() => unknown | Promise<unknown>),
Expand All @@ -14,9 +16,6 @@ export async function describeBase(
let cb: (() => unknown | Promise<unknown>) | undefined;
let options: DescribeOptions | undefined;

const isPoku = typeof env?.FILE === 'string' && env?.FILE.length > 0;
const FILE = env.POKU_FILE;

if (typeof arg1 === 'string') {
title = arg1;

Expand All @@ -31,7 +30,7 @@ export async function describeBase(
indentation.hasDescribe = true;

const { background, icon } = options ?? {};
const message = `${cb ? format('◌').dim() : (icon ?? '☰')} ${cb ? format(isPoku ? `${title}${format(`${FILE}`).italic().gray()}` : title).dim() : format(title).bold()}`;
const message = `${cb ? format('◌').dim() : (icon ?? '☰')} ${cb ? format(title).dim() : format(title).bold()}`;
const noBackground = !background;

if (noBackground) Write.log(format(message).bold());
Expand All @@ -56,6 +55,7 @@ export async function describeBase(

const total = (end[0] * 1e3 + end[1] / 1e6).toFixed(6);

GLOBAL.runAsOnly = false;
indentation.hasDescribe = false;
Write.log(
`${format(`● ${title}`).success().bold()} ${format(`› ${total}ms`).success().dim()}`
Expand All @@ -77,7 +77,18 @@ async function describeCore(
if (typeof messageOrCb === 'string' && typeof cbOrOptions !== 'function')
return describeBase(messageOrCb, cbOrOptions);

if (hasOnly || hasDescribeOnly) return;
if (hasOnly) {
const hasItOnly = checkOnly(
typeof messageOrCb === 'function' ? messageOrCb : cbOrOptions
);

if (!hasItOnly) return;

if (typeof messageOrCb === 'string' && typeof cbOrOptions === 'function')
return describeBase(messageOrCb, cbOrOptions);

if (typeof messageOrCb === 'function') return describeBase(messageOrCb);
}

if (typeof messageOrCb === 'string' && typeof cbOrOptions === 'function')
return describeBase(messageOrCb, cbOrOptions);
Expand Down
22 changes: 12 additions & 10 deletions src/modules/helpers/it/core.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { hrtime, env } from 'node:process';
import { hrtime } from 'node:process';
import { each } from '../../../configs/each.js';
import { indentation } from '../../../configs/indentation.js';
import { format } from '../../../services/format.js';
import { Write } from '../../../services/write.js';
import { todo, skip, onlyIt } from '../modifiers.js';
import { hasItOnly, hasOnly } from '../../../parsers/get-arg.js';
import { hasOnly } from '../../../parsers/get-arg.js';
import { GLOBAL } from '../../../configs/poku.js';

export async function itBase(
...args: [
Expand All @@ -16,10 +17,6 @@ export async function itBase(
let message: string | undefined;
let cb: () => unknown | Promise<unknown>;

const isPoku =
typeof env?.POKU_FILE === 'string' && env?.POKU_FILE.length > 0;
const FILE = env.POKU_FILE;

if (typeof args[0] === 'string') {
message = args[0];
cb = args[1] as () => unknown | Promise<unknown>;
Expand All @@ -29,9 +26,7 @@ export async function itBase(
indentation.hasItOrTest = true;

Write.log(
isPoku
? `${indentation.hasDescribe ? ' ' : ''}${format(`◌ ${message}${format(`${FILE}`).italic().gray()}`).dim()}`
: `${indentation.hasDescribe ? ' ' : ''}${format(`◌ ${message}`).dim()}`
`${indentation.hasDescribe ? ' ' : ''}${format(`◌ ${message}`).dim()}`
);
}

Expand Down Expand Up @@ -86,7 +81,14 @@ async function itCore(
messageOrCb: string | (() => unknown) | (() => Promise<unknown>),
cb?: (() => unknown) | (() => Promise<unknown>)
): Promise<void> {
if (hasOnly || hasItOnly) return;
if (hasOnly) {
if (!GLOBAL.runAsOnly) return;

if (typeof messageOrCb === 'string' && typeof cb === 'function')
return itBase(messageOrCb, cb);

if (typeof messageOrCb === 'function') return itBase(messageOrCb);
}

if (typeof messageOrCb === 'string' && cb) return itBase(messageOrCb, cb);
if (typeof messageOrCb === 'function') return itBase(messageOrCb);
Expand Down
22 changes: 14 additions & 8 deletions src/modules/helpers/modifiers.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { exit } from 'node:process';
import { Write } from '../../services/write.js';
import { indentation } from '../../configs/indentation.js';
import { format } from '../../services/format.js';
import { itBase } from './it/core.js';
import { describeBase } from './describe.js';
import { hasDescribeOnly, hasItOnly, hasOnly } from '../../parsers/get-arg.js';
import { exit } from 'node:process';
import { hasOnly } from '../../parsers/get-arg.js';
import { CheckNoOnly } from '../../parsers/callback.js';
import { GLOBAL } from '../../configs/poku.js';

export function todo(message: string): void;
export async function todo(
Expand Down Expand Up @@ -50,15 +52,19 @@ export async function onlyDescribe(
messageOrCb: string | (() => unknown) | (() => Promise<unknown>),
cb?: (() => unknown) | (() => Promise<unknown>)
): Promise<void> {
if (!(hasOnly || hasDescribeOnly)) {
if (!hasOnly) {
Write.log(
format(
"Can't run `describe.only` tests without `--only` or `--only=describe` flags"
).fail()
format("Can't run `describe.only` tests without `--only` flag").fail()
);
exit(1);
}

const noItOnly = CheckNoOnly(
typeof messageOrCb === 'function' ? messageOrCb : cb
);

if (noItOnly) GLOBAL.runAsOnly = true;

if (typeof messageOrCb === 'string' && cb)
return describeBase(messageOrCb, cb);
if (typeof messageOrCb === 'function') return describeBase(messageOrCb);
Expand All @@ -75,10 +81,10 @@ export async function onlyIt(
messageOrCb: string | (() => unknown) | (() => Promise<unknown>),
cb?: (() => unknown) | (() => Promise<unknown>)
): Promise<void> {
if (!(hasOnly || hasItOnly)) {
if (!hasOnly) {
Write.log(
format(
"Can't run `it.only` and `test.only` tests without `--only`, `--only=it` or `--only=test` flags"
"Can't run `it.only` and `test.only` tests without `--only` flag"
).fail()
);
exit(1);
Expand Down
7 changes: 3 additions & 4 deletions src/modules/helpers/skip.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { exit, env } from 'node:process';
import { exit } from 'node:process';
import { Write } from '../../services/write.js';
import { format } from '../../services/format.js';
import { GLOBAL } from '../../configs/poku.js';

export const skip = (message = 'Skipping') => {
const isPoku =
typeof env?.POKU_FILE === 'string' && env?.POKU_FILE.length > 0;
const FILE = env.POKU_FILE;
const { isPoku, FILE } = GLOBAL;

if (message)
Write.log(
Expand Down
23 changes: 23 additions & 0 deletions src/parsers/callback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export const checkOnly = (cb: unknown): boolean => {
if (typeof cb !== 'function') return false;

const body = cb.toString();

return (
body.includes('it.only') ||
body.includes('test.only') ||
body.includes('describe.only')
);
};

export const CheckNoOnly = (cb: unknown): boolean => {
if (typeof cb !== 'function') return false;

const body = cb.toString();

return !(
body.includes('it.only') ||
body.includes('test.only') ||
body.includes('describe.only')
);
};
8 changes: 1 addition & 7 deletions src/parsers/get-arg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,4 @@ export const argToArray = (
.filter((a) => a);
};

const only = getArg('only');

export const hasOnly = hasArg('only') && !only;

export const hasDescribeOnly = only === 'describe';

export const hasItOnly = only && ['it', 'test'].includes(only);
export const hasOnly = hasArg('only');
5 changes: 1 addition & 4 deletions src/services/assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ const { cwd } = GLOBAL;
const regexFile = /file:(\/\/)?/;

const assertProcessor = () => {
const isPoku =
typeof process.env?.POKU_FILE === 'string' &&
process.env?.POKU_FILE.length > 0;
const FILE = process.env.POKU_FILE;
const { isPoku, FILE } = GLOBAL;

let preIdentation = '';

Expand Down
6 changes: 2 additions & 4 deletions src/services/run-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ import { runTestFile } from './run-test-file.js';
import { isQuiet } from '../parsers/output.js';
import { deepOptions, GLOBAL, results } from '../configs/poku.js';
import { availableParallelism } from '../polyfills/os.js';
import { hasOnly, hasDescribeOnly, hasItOnly } from '../parsers/get-arg.js';
import { hasOnly } from '../parsers/get-arg.js';

const { cwd } = GLOBAL;
const failFastError = ` ${format('ℹ').fail()} ${format('failFast').bold()} is enabled`;

if (hasDescribeOnly) deepOptions.push('--only=describe');
else if (hasItOnly) deepOptions.push('--only=it');
else if (hasOnly) deepOptions.push('--only');
if (hasOnly) deepOptions.push('--only');

export const runTests = async (
dir: string,
Expand Down
29 changes: 0 additions & 29 deletions test/__fixtures__/e2e/only/--describe-only/basic-logs.test.ts

This file was deleted.

41 changes: 0 additions & 41 deletions test/__fixtures__/e2e/only/--it-only/basic-logs.test.ts

This file was deleted.

Loading

0 comments on commit d6b6241

Please sign in to comment.