Skip to content

Commit

Permalink
ci(coverage): add tests (#440)
Browse files Browse the repository at this point in the history
* ci(coverage): add tests

* ci: debug

* ci: debug

* chore: remove comments

* ci: remove debug
  • Loading branch information
wellwelwel authored Jun 22, 2024
1 parent 1ab72ed commit 23d0b92
Show file tree
Hide file tree
Showing 21 changed files with 277 additions and 96 deletions.
7 changes: 3 additions & 4 deletions src/helpers/parse-assertion.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
/* c8 ignore next */
import type { ParseAssertionOptions } from '../@types/assert.js';
import { cwd as processCWD, env, exit } from 'node:process';
import path from 'node:path';
import assert from 'node:assert';
import { format } from './format.js';
import { hr } from './hr.js';
import { findFile } from './find-file.js';
/* c8 ignore next */
import { each } from '../configs/each.js';
/* c8 ignore next */
import { indentation } from '../configs/indentation.js';
import { fromEntries, entries } from '../polyfills/object.js';
import { nodeVersion } from './get-runtime.js';
import { write } from './logs.js';
/* c8 ignore next */
import type { ParseAssertionOptions } from '../@types/assert.js';

const cwd = processCWD();

Expand Down Expand Up @@ -93,6 +91,7 @@ export const parseAssertion = async (
isPoku &&
!indentation.hasDescribe &&
!indentation.hasIt &&
/* c8 ignore next */ // c8 bug?
!indentation.hasTest
? /* c8 ignore next */
`${preIdentation}${format(`${format(`✔ ${options.message}`).bold()} ${format(`› ${FILE}`).success().dim()}`).success()}`
Expand Down
30 changes: 18 additions & 12 deletions src/modules/assert-promise.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* c8 ignore next */
import type { ParseAssertionOptions } from '../@types/assert.js';
import nodeAssert from 'node:assert';
import { parseAssertion } from '../helpers/parse-assertion.js';
import { nodeVersion } from '../helpers/get-runtime.js';
/* c8 ignore next */
import type { ParseAssertionOptions } from '../@types/assert.js';

const ok = async (
value: unknown,
Expand Down Expand Up @@ -144,7 +144,9 @@ function doesNotThrow(
): Promise<void>;
async function doesNotThrow(
block: () => unknown,
errorOrMessage?: nodeAssert.AssertPredicate | string | Error,
errorOrMessage?:
| nodeAssert.AssertPredicate
| ParseAssertionOptions['message'],
message?: ParseAssertionOptions['message']
): Promise<void> {
await parseAssertion(
Expand Down Expand Up @@ -210,17 +212,19 @@ async function throws(

function rejects(
block: (() => Promise<unknown>) | Promise<unknown>,
message?: string | Error
message?: ParseAssertionOptions['message']
): Promise<void>;
function rejects(
block: (() => Promise<unknown>) | Promise<unknown>,
error: nodeAssert.AssertPredicate,
message?: string | Error
message?: ParseAssertionOptions['message']
): Promise<void>;
async function rejects(
block: (() => Promise<unknown>) | Promise<unknown>,
errorOrMessage?: nodeAssert.AssertPredicate | string | Error,
message?: string | Error
errorOrMessage?:
| nodeAssert.AssertPredicate
| ParseAssertionOptions['message'],
message?: ParseAssertionOptions['message']
): Promise<void> {
await parseAssertion(
async () => {
Expand All @@ -247,17 +251,19 @@ async function rejects(

function doesNotReject(
block: (() => Promise<unknown>) | Promise<unknown>,
message?: string | Error
message?: ParseAssertionOptions['message']
): Promise<void>;
function doesNotReject(
block: (() => Promise<unknown>) | Promise<unknown>,
error: nodeAssert.AssertPredicate,
message?: string | Error
message?: ParseAssertionOptions['message']
): Promise<void>;
async function doesNotReject(
block: (() => Promise<unknown>) | Promise<unknown>,
errorOrMessage?: nodeAssert.AssertPredicate | string | Error,
message?: string | Error
errorOrMessage?:
| nodeAssert.AssertPredicate
| ParseAssertionOptions['message'],
message?: ParseAssertionOptions['message']
): Promise<void> {
await parseAssertion(
async () => {
Expand Down Expand Up @@ -338,5 +344,5 @@ export const assertPromise = Object.assign(
fail,
rejects,
}
/* c8 ignore next */
/* c8 ignore next */ // c8 bug
);
30 changes: 18 additions & 12 deletions src/modules/assert.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* c8 ignore next */
import type { ParseAssertionOptions } from '../@types/assert.js';
import nodeAssert from 'node:assert';
import { parseAssertion } from '../helpers/parse-assertion.js';
import { nodeVersion } from '../helpers/get-runtime.js';
/* c8 ignore next */
import type { ParseAssertionOptions } from '../@types/assert.js';

const ok = (
value: unknown,
Expand Down Expand Up @@ -136,7 +136,9 @@ function doesNotThrow(
): void;
function doesNotThrow(
block: () => unknown,
errorOrMessage?: nodeAssert.AssertPredicate | string | Error,
errorOrMessage?:
| nodeAssert.AssertPredicate
| ParseAssertionOptions['message'],
message?: ParseAssertionOptions['message']
): void {
parseAssertion(
Expand Down Expand Up @@ -202,17 +204,19 @@ function throws(

function rejects(
block: (() => Promise<unknown>) | Promise<unknown>,
message?: string | Error
message?: ParseAssertionOptions['message']
): Promise<void>;
function rejects(
block: (() => Promise<unknown>) | Promise<unknown>,
error: nodeAssert.AssertPredicate,
message?: string | Error
message?: ParseAssertionOptions['message']
): Promise<void>;
async function rejects(
block: (() => Promise<unknown>) | Promise<unknown>,
errorOrMessage?: nodeAssert.AssertPredicate | string | Error,
message?: string | Error
errorOrMessage?:
| nodeAssert.AssertPredicate
| ParseAssertionOptions['message'],
message?: ParseAssertionOptions['message']
): Promise<void> {
await parseAssertion(
async () => {
Expand All @@ -239,17 +243,19 @@ async function rejects(

function doesNotReject(
block: (() => Promise<unknown>) | Promise<unknown>,
message?: string | Error
message?: ParseAssertionOptions['message']
): Promise<void>;
function doesNotReject(
block: (() => Promise<unknown>) | Promise<unknown>,
error: nodeAssert.AssertPredicate,
message?: string | Error
message?: ParseAssertionOptions['message']
): Promise<void>;
async function doesNotReject(
block: (() => Promise<unknown>) | Promise<unknown>,
errorOrMessage?: nodeAssert.AssertPredicate | string | Error,
message?: string | Error
errorOrMessage?:
| nodeAssert.AssertPredicate
| ParseAssertionOptions['message'],
message?: ParseAssertionOptions['message']
): Promise<void> {
await parseAssertion(
async () => {
Expand Down Expand Up @@ -330,5 +336,5 @@ export const assert = Object.assign(
fail,
rejects,
}
/* c8 ignore next */
/* c8 ignore next */ // c8 bug
);
22 changes: 12 additions & 10 deletions src/modules/create-service.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import process from 'node:process';
import { spawn } from 'node:child_process';
import { isWindows, runner, scriptRunner } from '../helpers/runner.js';
import { normalize } from 'node:path';
import { sanitizePath } from './list-files.js';
import { kill } from './processes.js';
/* c8 ignore next */
import type {
End,
StartScriptOptions,
StartServiceOptions,
} from '../@types/background-process.js';
import process from 'node:process';
import { spawn } from 'node:child_process';
import { isWindows, runner, scriptRunner } from '../helpers/runner.js';
import { normalize } from 'node:path';
import { sanitizePath } from './list-files.js';
import { kill } from './processes.js';
import { write } from '../helpers/logs.js';

const runningProcesses: Map<number, { end: End; port?: number | number[] }> =
Expand Down Expand Up @@ -37,10 +37,8 @@ const backgroundProcess = (
stdio: ['inherit', 'pipe', 'pipe'],
/* c8 ignore next */
shell: isWindows,
cwd: options?.cwd
? sanitizePath(normalize(options.cwd))
: /* c8 ignore next */
undefined,
/* c8 ignore next */
cwd: options?.cwd ? sanitizePath(normalize(options.cwd)) : undefined,
env: process.env,
/* c8 ignore next */
detached: !isWindows,
Expand Down Expand Up @@ -166,6 +164,7 @@ const backgroundProcess = (
}
}, options.startAfter);
}
/* c8 ignore next */ // c8 bug
} catch {}
});

Expand All @@ -191,6 +190,7 @@ export const startService = async (
);
};

/* c8 ignore start */ // c8 bug
/**
*
* Starts a script (package.json) or task (deno.json) in a background process
Expand All @@ -203,6 +203,8 @@ export const startScript = async (
script: string,
options?: StartScriptOptions
): Promise<{ end: End }> => {
/* c8 ignore stop */
/* c8 ignore next */
const runner = options?.runner || 'npm';
const runtimeOptions = scriptRunner(runner);
const runtime = runtimeOptions.shift()!;
Expand Down
12 changes: 3 additions & 9 deletions src/modules/describe.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
/* c8 ignore next */
import type { DescribeOptions } from '../@types/describe.js';
import { hrtime, env } from 'node:process';
import { format } from '../helpers/format.js';
import { write } from '../helpers/logs.js';
/* c8 ignore next */
import { indentation } from '../configs/indentation.js';
/* c8 ignore next */
import type { DescribeOptions } from '../@types/describe.js';

/* c8 ignore start */
/* c8 ignore start */ // c8 bug
/**
* On **Poku**, `describe` also can be used just as a pretty `console.log` to title your test suites in the terminal.
*/
Expand Down Expand Up @@ -41,12 +39,11 @@ export async function describe(
options = arg2 as DescribeOptions;
}

/* c8 ignore start */
if (title) {
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(isPoku ? `${title}${format(`${FILE}`).italic().gray()}` : /* c8 ignore next */ title).dim() : format(title).bold() || ''}`;
const noBackground = !background;

if (noBackground) write(format(message).bold());
Expand All @@ -58,7 +55,6 @@ export async function describe(
);
}
}
/* c8 ignore stop */

if (typeof cb !== 'function') return;

Expand All @@ -69,7 +65,6 @@ export async function describe(
if (resultCb instanceof Promise) await resultCb;
const end = hrtime(start);

/* c8 ignore start */
if (title) {
const total = (end[0] * 1e3 + end[1] / 1e6).toFixed(6);

Expand All @@ -78,5 +73,4 @@ export async function describe(
`${format(`● ${title}`).success().bold()} ${format(`› ${total}ms`).success().dim()}`
);
}
/* c8 ignore stop */
}
2 changes: 2 additions & 0 deletions src/modules/each.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export const beforeEach = (
return { pause, continue: continueFunc, reset };
};

/* c8 ignore start */ // c8 bug
/**
* - ✅ Handling **global** and **external** services (_preparing a database, for example_)
* - ✅ It's made for **exclusive use** in combination with **Poku**'s **`assert`** methods
Expand All @@ -77,6 +78,7 @@ export const afterEach = (
callback: () => unknown,
options?: Omit<EachOptions, 'immediate'>
): Control => {
/* c8 ignore stop */
each.after.test = typeof options?.test === 'boolean' ? options.test : true;
each.after.assert =
typeof options?.assert === 'boolean' ? options.assert : false;
Expand Down
15 changes: 5 additions & 10 deletions src/modules/it.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
/* c8 ignore next */
/* c8 ignore next */ // c8 bug
import { hrtime, env } from 'node:process';
/* c8 ignore next */
import { each } from '../configs/each.js';
/* c8 ignore next */
import { indentation } from '../configs/indentation.js';
import { format } from '../helpers/format.js';
import { write } from '../helpers/logs.js';

/* c8 ignore start */
/* c8 ignore start */ // c8 bug
export async function it(
message: string,
cb: () => Promise<unknown>
Expand Down Expand Up @@ -40,16 +38,15 @@ export async function it(
cb = args[1] as () => unknown | Promise<unknown>;
} else cb = args[0] as () => unknown | Promise<unknown>;

/* c8 ignore start */
if (message) {
indentation.hasIt = true;
write(
isPoku && !indentation.hasDescribe
? `${indentation.hasDescribe ? ' ' : ''}${format(`◌ ${message}${format(`${FILE}`).italic().gray()}`).dim()}`
: `${indentation.hasDescribe ? ' ' : ''}${format(`◌ ${message}`).dim()}`
? `${indentation.hasDescribe ? ' ' : ''}${format(`◌ ${message}${format(`${FILE}`).italic().gray()}`).dim()}` /* c8 ignore next */ // c8 bug
: /* c8 ignore next */
`${indentation.hasDescribe ? ' ' : ''}${format(`◌ ${message}`).dim()}`
);
}
/* c8 ignore end */

const start = hrtime();
const resultCb = cb();
Expand All @@ -64,7 +61,6 @@ export async function it(
if (afterResult instanceof Promise) await afterResult;
}

/* c8 ignore start */
if (message) {
const total = (end[0] * 1e3 + end[1] / 1e6).toFixed(6);

Expand All @@ -73,5 +69,4 @@ export async function it(
`${indentation.hasDescribe ? ' ' : ''}${format(`● ${message}`).success().bold()} ${format(`› ${total}ms`).success().dim()}`
);
}
/* c8 ignore stop */
}
5 changes: 2 additions & 3 deletions src/modules/list-files.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/* c8 ignore next */
import type { Configs } from '../@types/list-files.js';
import { env } from 'node:process';
import { sep, join } from 'node:path';
import { readdir, stat as fsStat } from '../polyfills/fs.js';
/* c8 ignore next */
import type { Configs } from '../@types/list-files.js';

export const sanitizePath = (input: string, ensureTarget?: boolean): string => {
const sanitizedPath = input
Expand Down Expand Up @@ -83,7 +82,7 @@ export const getAllFiles = async (
return files;
};

/* c8 ignore start */
/* c8 ignore start */ // c8 bug
export const listFiles = async (
targetDir: string,
configs?: Configs
Expand Down
2 changes: 1 addition & 1 deletion src/modules/poku.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* c8 ignore start */
/* c8 ignore start */ // c8 bug (incompatibility) =>
/**
* Both CLI, API, noExit, sequential and parallel runs are strictly tested, but these tests use deep child process for it
*/
Expand Down
Loading

0 comments on commit 23d0b92

Please sign in to comment.