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

refactor: reduce polyfills usage #437

Merged
merged 1 commit into from
Jun 21, 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
4 changes: 1 addition & 3 deletions src/helpers/find-file.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/* c8 ignore start */
import { EOL } from 'node:os';

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

let file = '';

Expand Down
4 changes: 2 additions & 2 deletions src/helpers/get-arg.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* c8 ignore start */
import process from 'node:process';
import { argv } from 'node:process';

const [, , ...processArgs] = process.argv;
const [, , ...processArgs] = argv;

/**
* Gets the value of an argument.
Expand Down
6 changes: 2 additions & 4 deletions src/helpers/get-runtime.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* c8 ignore start */

import process from 'node:process';
import { version } from 'node:process';
import type { Configs } from '../@types/poku.js';

declare const Deno: unknown;
Expand Down Expand Up @@ -37,8 +37,6 @@ export const getRuntime = (
};

export const nodeVersion =
getRuntime() === 'node'
? Number(process.version.match(/v(\d+)\./)?.[1])
: undefined;
getRuntime() === 'node' ? Number(version.match(/v(\d+)\./)?.[1]) : undefined;

/* c8 ignore stop */
9 changes: 3 additions & 6 deletions src/helpers/hr.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
/* c8 ignore start */

import process from 'node:process';
import { EOL } from 'node:os';
import { stdout } from 'node:process';
import { write } from './logs.js';

export const hr = () => {
const line = '⎯'.repeat(process.stdout.columns - 10 || 40);
const line = '⎯'.repeat(stdout.columns - 10 || 40);

write(`${EOL}\x1b[2m\x1b[90m${line}\x1b[0m${EOL}`);
write(`\n\x1b[2m\x1b[90m${line}\x1b[0m\n`);
};

/* c8 ignore stop */
7 changes: 3 additions & 4 deletions src/helpers/logs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* c8 ignore start */

import process from 'node:process';
import { EOL } from 'node:os';
import { stdout } from 'node:process';
import type { Configs } from '../@types/poku.js';

export const isQuiet = (configs?: Configs): boolean =>
Expand All @@ -10,7 +9,7 @@ export const isQuiet = (configs?: Configs): boolean =>
export const isDebug = (configs?: Configs): boolean => Boolean(configs?.debug);

export const write = (data: string | Uint8Array) =>
process.stdout.write(`${String(data)}\n`);
stdout.write(`${String(data)}\n`);

export const printOutput = (options: {
output: string;
Expand Down Expand Up @@ -38,7 +37,7 @@ export const printOutput = (options: {

const mappedOutputs = outputs.map((current) => `${pad}${current}`);

write(mappedOutputs.join(EOL));
write(mappedOutputs.join('\n'));
};

/* c8 ignore stop */
16 changes: 7 additions & 9 deletions src/helpers/parse-assertion.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import process from 'node:process';
import { cwd as processCWD, env, exit } from 'node:process';
import path from 'node:path';
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';
Expand All @@ -15,7 +14,7 @@ import { write } from './logs.js';
/* c8 ignore next */
import type { ParseAssertionOptions } from '../@types/assert.js';

const cwd = process.cwd();
const cwd = processCWD();

export const parseResultType = (type?: unknown): string => {
const recurse = (value: unknown): unknown => {
Expand Down Expand Up @@ -64,9 +63,8 @@ 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;
const isPoku = typeof env?.FILE === 'string' && env?.FILE.length > 0;
const FILE = env.FILE;
let preIdentation = '';

if (indentation.hasDescribe || indentation.hasTest) preIdentation += ' ';
Expand Down Expand Up @@ -125,7 +123,7 @@ export const parseAssertion = async (

file && write(`${format.dim(`${preIdentation} File`)} ${file}`);
write(`${format.dim(`${preIdentation} Code`)} ${code}`);
write(`${format.dim(`${preIdentation} Operator`)} ${operator}${EOL}`);
write(`${format.dim(`${preIdentation} Operator`)} ${operator}\n`);

if (!options?.hideDiff) {
const splitActual = parseResultType(actual).split('\n');
Expand All @@ -137,7 +135,7 @@ export const parseAssertion = async (
);

write(
`${EOL}${preIdentation} ${format.dim(`${options?.expected || 'Expected'}:`)}`
`\n${preIdentation} ${format.dim(`${options?.expected || 'Expected'}:`)}`
);
splitExpected.forEach((line) =>
write(`${preIdentation} ${format.bold(format.success(line))}`)
Expand All @@ -149,7 +147,7 @@ export const parseAssertion = async (
hr();
}

process.exit(1);
exit(1);
}

// Non-assertion errors
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/runner.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* c8 ignore start */

import process from 'node:process';
import { platform } from 'node:process';
import { extname } from 'node:path';
import { getRuntime } from './get-runtime.js';
import type { Configs } from '../@types/poku.js';
import type { Runner } from '../@types/runner.js';

export const isWindows = process.platform === 'win32';
export const isWindows = platform === 'win32';

export const runner = (filename: string, configs?: Configs): string[] => {
const runtime = getRuntime(configs);
Expand Down
6 changes: 3 additions & 3 deletions src/modules/describe.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* c8 ignore next */
import process from 'node:process';
import { hrtime } from 'node:process';
import { format, backgroundColor } from '../helpers/format.js';
import { write } from '../helpers/logs.js';
/* c8 ignore next */
Expand Down Expand Up @@ -57,12 +57,12 @@ export async function describe(

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

const start = process.hrtime();
const start = hrtime();
const resultCb = cb();

/* c8 ignore next */
if (resultCb instanceof Promise) await resultCb;
const end = process.hrtime(start);
const end = hrtime(start);

/* c8 ignore start */
if (title) {
Expand Down
6 changes: 3 additions & 3 deletions src/modules/it.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* c8 ignore next */
import process from 'node:process';
import { hrtime } from 'node:process';
/* c8 ignore next */
import { each } from '../configs/each.js';
/* c8 ignore next */
Expand Down Expand Up @@ -46,12 +46,12 @@ export async function it(
}
/* c8 ignore end */

const start = process.hrtime();
const start = hrtime();
const resultCb = cb();

/* c8 ignore next */
if (resultCb instanceof Promise) await resultCb;
const end = process.hrtime(start);
const end = hrtime(start);

if (typeof each.after.cb === 'function' && each.after.test) {
const afterResult = each.after.cb();
Expand Down
6 changes: 3 additions & 3 deletions src/modules/list-files-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
* This method will be removed in a future release to use `list-files.ts` instead.
*/

import process from 'node:process';
import { env } from 'node:process';
import { readdirSync, statSync } from 'node:fs';
import path from 'node:path';
import type { Configs } from '../@types/list-files.js';
import { escapeRegExp, sanitizePath } from './list-files.js';

const isDir = (fullPath: string) => statSync(fullPath).isDirectory();

const envFilter = process.env.FILTER?.trim()
? new RegExp(escapeRegExp(process.env.FILTER), 'i')
const envFilter = env.FILTER?.trim()
? new RegExp(escapeRegExp(env.FILTER), 'i')
: null;

const listFiles = (
Expand Down
6 changes: 3 additions & 3 deletions src/modules/list-files.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* c8 ignore next */
import process from 'node:process';
import { env } from 'node:process';
import { sep, join } from 'node:path';
import { readdir, stat as fsStat } from '../polyfills/fs.js';
/* c8 ignore next */
Expand Down Expand Up @@ -28,8 +28,8 @@ export const escapeRegExp = (string: string) =>
/* c8 ignore stop */

/* c8 ignore start */
const envFilter = process.env.FILTER?.trim()
? new RegExp(escapeRegExp(process.env.FILTER), 'i')
const envFilter = env.FILTER?.trim()
? new RegExp(escapeRegExp(env.FILTER), 'i')
: undefined;
/* c8 ignore stop */

Expand Down
7 changes: 3 additions & 4 deletions src/modules/poku.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import process from 'node:process';
import { EOL } from 'node:os';
import { runTests, runTestsParallel } from '../services/run-tests.js';
import { exit } from './exit.js';
import { format } from '../helpers/format.js';
Expand Down Expand Up @@ -68,7 +67,7 @@ export async function poku(
// Parallel
if (showLogs) {
hr();
write(`${format.bold('Running the Test Suite in Parallel')}${EOL}`);
write(`${format.bold('Running the Test Suite in Parallel')}\n`);
}

try {
Expand Down Expand Up @@ -100,7 +99,7 @@ export async function poku(
([file, time]) =>
`${indentation.test}${format.success('✔')} ${format.dim(`${file} › ${time}ms`)}`
)
.join(EOL)
.join('\n')
);
}

Expand All @@ -111,7 +110,7 @@ export async function poku(
([file, time]) =>
`${indentation.test}${format.fail('✘')} ${format.dim(`${file} › ${time}ms`)}`
)
.join(EOL)
.join('\n')
);
}

Expand Down
6 changes: 3 additions & 3 deletions src/modules/test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* c8 ignore next */
import process from 'node:process';
import { hrtime } from 'node:process';
/* c8 ignore next */
import { each } from '../configs/each.js';
/* c8 ignore next */
Expand Down Expand Up @@ -45,12 +45,12 @@ export async function test(
}
/* c8 ignore stop */

const start = process.hrtime();
const start = hrtime();
const resultCb = cb();

/* c8 ignore next */
if (resultCb instanceof Promise) await resultCb;
const end = process.hrtime(start);
const end = hrtime(start);

if (typeof each.after.cb === 'function' && each.after.test) {
const afterResult = each.after.cb();
Expand Down
5 changes: 2 additions & 3 deletions src/services/each.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { EOL } from 'node:os';
import { format } from '../helpers/format.js';
import { write } from '../helpers/logs.js';
/* c8 ignore next */
Expand Down Expand Up @@ -32,12 +31,12 @@
);
write(
format.fail(
` ├─ Who's trying to run this ${type}?${EOL} │ └─ ${format.underline(fileRelative)}`
` ├─ Who's trying to run this ${type}?\n │ └─ ${format.underline(fileRelative)}`

Check warning on line 34 in src/services/each.ts

View check run for this annotation

Codecov / codecov/patch

src/services/each.ts#L34

Added line #L34 was not covered by tests
)
);

error instanceof Error &&
write(format.fail(` ├─ Message:${EOL} │ └─ ${error.message}`));
write(format.fail(` ├─ Message:\n │ └─ ${error.message}`));

Check warning on line 39 in src/services/each.ts

View check run for this annotation

Codecov / codecov/patch

src/services/each.ts#L39

Added line #L39 was not covered by tests

return false;
}
Expand Down
5 changes: 2 additions & 3 deletions src/services/pid.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* c8 ignore start */

import { spawn } from 'node:child_process';
import { EOL } from 'node:os';
import { forceArray } from '../helpers/force-array.js';

export const setPortsAndPIDs = (portOrPID: number | number[]) =>
Expand Down Expand Up @@ -53,7 +52,7 @@ export const getPIDs = {
]);

service.stdout.on('data', (data: Buffer) => {
const output = data.toString().trim().split(EOL);
const output = data.toString().trim().split('\n');

output.forEach((pid) => {
if (pid) PIDs.add(Number(pid));
Expand All @@ -74,7 +73,7 @@ export const getPIDs = {

service.stdout.on('data', (data: Buffer) => {
const output = data.toString().trim();
const lines = output.trim().split(EOL);
const lines = output.trim().split('\n');

/**
* TODO: Chack line for "/:\d+\s+\w+\s+\d+\s+(\d+)/" regex match to safe support multiple Windows versions
Expand Down
14 changes: 7 additions & 7 deletions src/services/run-test-file.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* c8 ignore start */ // c8 bug
import process from 'node:process';
import { cwd as processCWD, hrtime, env } from 'node:process';
import { relative } from 'node:path';
import { spawn } from 'node:child_process';
import { indentation } from '../configs/indentation.js';
Expand All @@ -11,7 +11,7 @@ import { beforeEach, afterEach } from './each.js';
/* c8 ignore next */
import type { Configs } from '../@types/poku.js';

const cwd = process.cwd();
const cwd = processCWD();
/* c8 ignore stop */ // c8 bug

/* c8 ignore next */ // c8 bug
Expand Down Expand Up @@ -49,8 +49,8 @@ export const runTestFile = (
);
}

const start = process.hrtime();
let end: ReturnType<typeof process.hrtime>;
const start = hrtime();
let end: ReturnType<typeof hrtime>;

/* c8 ignore next */
if (!(await beforeEach(fileRelative, configs))) return false;
Expand All @@ -61,7 +61,7 @@ export const runTestFile = (
/* c8 ignore next */
shell: isWindows,
env: {
...process.env,
...env,
FILE: configs?.parallel || configs?.deno?.cjs ? fileRelative : '',
},
});
Expand All @@ -71,7 +71,7 @@ export const runTestFile = (
child.stderr.on('data', stdOut);

child.on('close', async (code) => {
end = process.hrtime(start);
end = hrtime(start);

const result = code === 0;

Expand All @@ -95,7 +95,7 @@ export const runTestFile = (

/* c8 ignore start */
child.on('error', (err) => {
end = process.hrtime(start);
end = hrtime(start);

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

Expand Down
Loading
Loading