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

feat: add option exclude #7

Merged
merged 6 commits into from
Feb 18, 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
110 changes: 102 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@

<img align="right" width="128" height="128" alt="Logo" src=".github/assets/readme/poku.svg">

🖇️ A flexible and easy-to-use **Test Runner** for [**Node**][node-version-url], [**Bun**][bun-version-url] and [**Deno**][deno-version-url], which allows parallel or sequential runs and high isolation level.

> **Poku** starts from the premise where tests come to help, not overcomplicate.
A flexible and easy-to-use **Test Runner** for [Node][node-version-url], [Bun][bun-version-url] and [Deno][deno-version-url] that allows you to run **parallel** and **sequential** tests, plus **high isolation level per test file**.

[![Node.js Version][node-version-image]][node-version-url]
[![Bun Version][bun-version-image]][bun-version-url]
Expand All @@ -33,10 +31,10 @@

## Why Poku?

Runs test files in an individual process, shows progress and exits 🪄
> **Poku** starts from the premise where tests come to help, not overcomplicate: runs test files in an individual process per file, shows progress and exits 🧙🏻

- **Poku** is designed to be highly intuitive
- Supports **ESM** and **CJS**
- Designed to be highly intuitive
- No need to compile **TypeScript**
- Compatible with **Coverage** tools
- Allows both **in-code** and **CLI** usage
Expand Down Expand Up @@ -170,7 +168,9 @@ npx poku --include='./targetDirA,./targetDirB'

### `poku(string | string[], configs: Configs)`

#### `filter`
#### `filter: RexExp`

By default, **Poku** searches for _`*.test.*`_ files, but you can customize it using the `filter` option.

> Filter by path using **Regex** to match only the files that should be performed.

Expand Down Expand Up @@ -207,6 +207,12 @@ poku(['...'], {
npx poku --include='...' --filter='some-file'
```

```bash
# Testing only a specific file

npx poku --include='...' --filter='some-file|other-file'
```

```bash
# Testing only paths that contains "unit"

Expand All @@ -223,6 +229,12 @@ npx poku --include='...' --filter='unit'
FILTER='some-file' npx poku --include='...'
```

```bash
# Testing only a specific file

FILTER='some-file|other-file' npx poku --include='...'
```

```bash
# Testing only paths that contains "unit"

Expand All @@ -231,9 +243,9 @@ FILTER='unit' npx poku --include='...'

---

#### `parallel`
#### `parallel: boolean`

Determines the mode of test execution across **parallelism** or **sequential** modes.
Determines the mode of test execution across **sequential** or **parallel** modes.

```ts
/**
Expand All @@ -259,6 +271,88 @@ poku(['...'], {

---

#### `exclude: RexExp | RexExp[]`

> Exclude by path using Regex to match only the files that should be performed.

- **in-code**:

```ts
/**
* Excluding directories from tests
*/

poku(['...'], {
exclude: /\/(helpers|tools)\//,
});
```

```ts
/**
* Excluding directories from tests
*/

poku(['...'], {
exclude: [/\/helpers\//, /\/tools\//],
});
```

```ts
/**
* Excluding specific files from tests
*/

poku(['...'], {
exclude: /(index|common).test.ts/,
});
```

```ts
/**
* Excluding specific files from tests
*/

poku(['...'], {
exclude: [/index.test.ts/, /common.test.ts/],
});
```

```ts
/**
* Excluding directories and files from tests
*/

poku(['...'], {
exclude: /\/(helpers|tools)\/|(index|common).test.ts/,
});
```

```ts
/**
* Excluding directories and files from tests
*/

poku(['...'], {
exclude: [/\/helpers\//, /\/tools\//, /index.test.ts/, /common.test.ts/],
});
```

- **CLI**

```bash
# Excluding directories and files from tests

npx poku --include='...' --exclude='some-file-or-dir'
```

```bash
# Excluding directories and files from tests

npx poku --include='...' --exclude='some-file-or-dir|other-file-or-dir'
```

---

## Documentation in Progress...

> 🧑🏻‍🎓 Soon documenting all options and **Poku**'s usage variations.
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "poku",
"version": "1.1.1",
"description": "🐷 A flexible and easy-to-use Test Runner for Node, Bun and Deno, which allows parallel or sequential runs and high isolation level",
"description": "🐷 Poku is a flexible and easy-to-use Test Runner for Node, Bun and Deno that allows you to run parallel and sequential tests, plus high isolation level per test file",
"main": "./lib/index.js",
"scripts": {
"test": "npx tsx --tsconfig ./tsconfig.test.json ./test/run.test.ts",
Expand Down Expand Up @@ -43,14 +43,19 @@
"typescript",
"filter",
"queue",
"queuing"
"queuing",
"nodejs",
"node",
"bun",
"deno"
],
"author": "https://github.com/wellwelwel",
"bugs": {
"url": "https://github.com/wellwelwel/poku/issues"
},
"engines": {
"node": ">=6.0.0",
"bun": ">=0.5.3",
"deno": ">=1.17.0"
},
"files": [
Expand Down
14 changes: 14 additions & 0 deletions src/@types/get-files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export type Configs = {
/**
* Filter by path to match only the files that should be performed.
*
* @default /\.test\./i
*/
filter?: RegExp;
/**
* Exclude by path to match only the files that should be performed.
*
* @default undefined
*/
exclude?: RegExp | RegExp[];
};
10 changes: 3 additions & 7 deletions src/@types/poku.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Configs as GetFileOptions } from './get-files.ts';

export type Configs = {
/**
* By setting `true`, **Poku** won't exit the process and will return the exit code (`0` or `1`).
Expand Down Expand Up @@ -26,16 +28,10 @@ export type Configs = {
* @default false
*/
quiet?: boolean;
/**
* Filter by path to match only the files that should be performed.
*
* @default /\.test\./i
*/
filter?: RegExp;
/**
* Determines the mode of test execution.
*
* @default false
*/
parallel?: boolean;
};
} & GetFileOptions;
4 changes: 3 additions & 1 deletion src/bin/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#! /usr/bin/env node

import { escapeRegExp } from '../helpers/get-files.js';
import { escapeRegExp } from '../modules/get-files.js';
import { getArg } from '../helpers/get-arg.js';
import { poku } from '../index.js';

const rawDirs = getArg('include');
const rawFilter = getArg('filter');
const rawExclude = getArg('exclude');

const dirs = rawDirs?.split(',') || [];

poku(dirs, {
filter: rawFilter ? new RegExp(escapeRegExp(rawFilter)) : undefined,
exclude: rawExclude ? new RegExp(escapeRegExp(rawExclude)) : undefined,
});
33 changes: 0 additions & 33 deletions src/helpers/get-files.ts

This file was deleted.

43 changes: 43 additions & 0 deletions src/modules/get-files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import process from 'node:process';
import fs from 'node:fs';
import path from 'node:path';
import type { Configs } from '../@types/get-files.ts';

export const escapeRegExp = (string: string) =>
string.replace(/[.*+?^${}()[\]\\]/g, '\\$&');

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

export const getFiles = (
dirPath: string,
files: string[] = [],
configs?: Configs
) => {
const currentFiles = fs.readdirSync(dirPath);
const defaultRegExp = /\.test\./i;
const filter: RegExp =
(envFilter
? envFilter
: configs?.filter instanceof RegExp
? configs.filter
: defaultRegExp) || defaultRegExp;

const exclude: Configs['exclude'] = configs?.exclude
? Array.isArray(configs.exclude)
? configs.exclude
: [configs.exclude]
: undefined;

for (const file of currentFiles) {
const fullPath = path.join(dirPath, file);

if (exclude && exclude.some((regex) => regex.test(fullPath))) continue;

if (fs.statSync(fullPath).isDirectory()) getFiles(fullPath, files, configs);
else if (filter.test(fullPath)) files.push(fullPath);
}

return files;
};
2 changes: 1 addition & 1 deletion src/modules/poku.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Code } from '../@types/code.js';
import { Configs } from '../@types/poku.js';
import { forceArray } from '../helpers/force-array.js';
import { runTests, runTestsParallel } from '../services/runTests.js';
import { runTests, runTestsParallel } from '../services/run-tests.js';
import { exit } from './exit.js';

export async function poku(
Expand Down
File renamed without changes.
5 changes: 3 additions & 2 deletions src/services/runTests.ts → src/services/run-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { EOL } from 'node:os';
import path from 'node:path';
import { runner } from '../helpers/runner.js';
import { indentation } from '../helpers/indentation.js';
import { getFiles } from '../helpers/get-files.js';
import { getFiles } from '../modules/get-files.js';
import { hr } from '../helpers/hr.js';
import { format } from '../helpers/format.js';
import { runTestFile } from './runTestFile.js';
import { runTestFile } from './run-test-file.js';
import { Configs } from '../@types/poku.js';
import { isQuiet } from '../helpers/logs.js';

Expand Down Expand Up @@ -80,5 +80,6 @@ export const runTestsParallel = async (
});

const results = await Promise.all(promises);

return results.every((result) => result);
};
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading
Loading