Compare commits

..

16 Commits
v3.3.0 ... main

Author SHA1 Message Date
7490da22c0 v3.6.2 2026-03-27 17:19:43 +00:00
3b2a5982a2 fix(cli,chromium-runtime): clean up long-lived test runner resources after runs to prevent hanging processes 2026-03-27 17:19:43 +00:00
099eff0b1e v3.6.1 2026-03-26 08:19:42 +00:00
3a56040c5b fix(chromium runtime): encode Chromium test bundle names correctly for nested file paths 2026-03-26 08:19:42 +00:00
8435c45b4b v3.6.0 2026-03-24 19:52:31 +00:00
18904d18e8 feat(config): migrate project metadata to .smartconfig and refresh build configuration 2026-03-24 19:52:31 +00:00
a048c5a4d0 v3.5.1 2026-03-23 14:40:54 +00:00
5563154fd9 fix(runtime): handle expected exitCode rejection after terminating timed out test processes 2026-03-23 14:40:54 +00:00
8e53e5143e v3.5.0 2026-03-19 15:38:02 +00:00
cf4e5c33e8 feat(tstest): add support for package.json before scripts during test execution 2026-03-19 15:38:02 +00:00
bc39793682 v3.4.0 2026-03-18 01:56:04 +00:00
edce15b20a feat(tapbundle,deno): replace smarts3 test tooling with smartstorage and pre-resolve Deno test dependencies 2026-03-18 01:56:04 +00:00
9d34a3511a v3.3.2 2026-03-09 17:32:38 +00:00
300e03628c fix(deps): bump dependency versions and reorder smartserve in package.json 2026-03-09 17:32:38 +00:00
893d6e40dc v3.3.1 2026-03-09 16:23:48 +00:00
8b16ba1d9a fix(serve): migrate test HTTP server to @push.rocks/smartserve and update related dependencies 2026-03-09 16:23:48 +00:00
23 changed files with 2029 additions and 2596 deletions

View File

@@ -1,7 +1,7 @@
{
"json.schemas": [
{
"fileMatch": ["/npmextra.json"],
"fileMatch": ["/.smartconfig.json"],
"schema": {
"type": "object",
"properties": {

View File

@@ -1,5 +1,61 @@
# Changelog
## 2026-03-27 - 3.6.2 - fix(cli,chromium-runtime)
clean up long-lived test runner resources after runs to prevent hanging processes
- call TsTest.cleanup() after non-watch CLI runs so smartexit handlers are deregistered
- terminate active WebSocket clients before closing the Chromium runtime server to release open connections cleanly
## 2026-03-26 - 3.6.1 - fix(chromium runtime)
encode Chromium test bundle names correctly for nested file paths
- Use replaceAll() so bundle cache filenames consistently handle test paths with multiple directory separators.
- URL-encode the bundleName query parameter before opening the Chromium test page to avoid lookup issues with generated bundle filenames.
## 2026-03-24 - 3.6.0 - feat(config)
migrate project metadata to .smartconfig and refresh build configuration
- replace npmextra.json with .smartconfig.json and update packaged files accordingly
- add module-level README documentation under ts/readme.md
- bump build and runtime dependencies including tsbuild, tsbundle, smartstorage, smartwatch, smartserve, and ws
- relax TypeScript compiler strictness and include Node types in tsconfig
- update README license links to point to license.md
## 2026-03-23 - 3.5.1 - fix(runtime)
handle expected exitCode rejection after terminating timed out test processes
- swallow the child process exitCode rejection triggered by timeout termination
- reduce unhandled rejection noise when test files exceed the configured timeout
## 2026-03-19 - 3.5.0 - feat(tstest)
add support for package.json before scripts during test execution
- load test:before or test:before:once once per test run from package.json scripts
- run test:before:testfile before each test file execution and pass TSTEST_FILE and TSTEST_RUNTIME environment variables
- log before-script lifecycle events and abort or skip execution when setup scripts fail
## 2026-03-18 - 3.4.0 - feat(tapbundle,deno)
replace smarts3 test tooling with smartstorage and pre-resolve Deno test dependencies
- switch TapNodeTools storage helper from @push.rocks/smarts3 to @push.rocks/smartstorage and rename createSmarts3() to createSmartStorage()
- update node tests and tapbundle server-side documentation to use the new smartstorage helper
- run `deno install --entrypoint` before executing Deno tests to resolve dependencies up front
- bump supporting development dependencies including @types/node and @push.rocks/smartshell
## 2026-03-09 - 3.3.2 - fix(deps)
bump dependency versions and reorder smartserve in package.json
- bump @push.rocks/smartbrowser from ^2.0.10 to ^2.0.11
- bump @push.rocks/smartfs from ^1.4.0 to ^1.5.0
- move @push.rocks/smartserve to later position in dependencies (version unchanged: ^2.0.1)
## 2026-03-09 - 3.3.1 - fix(serve)
migrate test HTTP server to @push.rocks/smartserve and update related dependencies
- Replace @api.global/typedserver with @push.rocks/smartserve and FileServer; use SmartServe.setHandler to serve static assets and a custom /test response.
- Export smartserve from ts/tstest.plugins.ts and remove typedserver import/export.
- Update package.json dependencies: add @push.rocks/smartserve@^2.0.1 and bump @push.rocks/smartbrowser to ^2.0.10.
## 2026-03-06 - 3.3.0 - feat(testfile-directives)
Add per-test file directives to control runtime permissions and flags (Deno, Node, Bun, Chromium)

View File

@@ -9,5 +9,5 @@
"target": "ES2022"
},
"nodeModulesDir": true,
"version": "3.3.0"
"version": "3.6.2"
}

View File

@@ -1,6 +1,6 @@
{
"name": "@git.zone/tstest",
"version": "3.3.0",
"version": "3.6.2",
"private": false,
"description": "A powerful, modern test runner for TypeScript with multi-runtime support (Node.js, Deno, Bun, Chromium) and a batteries-included test framework.",
"exports": {
@@ -25,22 +25,21 @@
"buildDocs": "tsdoc"
},
"devDependencies": {
"@git.zone/tsbuild": "^4.3.0",
"@types/node": "^25.3.5"
"@git.zone/tsbuild": "^4.4.0",
"@types/node": "^25.5.0"
},
"dependencies": {
"@api.global/typedserver": "^8.4.2",
"@git.zone/tsbundle": "^2.9.1",
"@git.zone/tsbundle": "^2.10.0",
"@git.zone/tsrun": "^2.0.1",
"@push.rocks/consolecolor": "^2.0.3",
"@push.rocks/qenv": "^6.1.3",
"@push.rocks/smartbrowser": "^2.0.8",
"@push.rocks/smartbrowser": "^2.0.11",
"@push.rocks/smartcrypto": "^2.0.4",
"@push.rocks/smartdelay": "^3.0.5",
"@push.rocks/smartenv": "^6.0.0",
"@push.rocks/smartexpect": "^2.5.0",
"@push.rocks/smartfile": "^13.1.2",
"@push.rocks/smartfs": "^1.4.0",
"@push.rocks/smartfs": "^1.5.0",
"@push.rocks/smartjson": "^6.0.0",
"@push.rocks/smartlog": "^3.2.1",
"@push.rocks/smartmongo": "^5.1.0",
@@ -48,13 +47,14 @@
"@push.rocks/smartpath": "^6.0.0",
"@push.rocks/smartpromise": "^4.2.3",
"@push.rocks/smartrequest": "^5.0.1",
"@push.rocks/smarts3": "^5.3.0",
"@push.rocks/smartshell": "^3.3.7",
"@push.rocks/smartserve": "^2.0.3",
"@push.rocks/smartshell": "^3.3.8",
"@push.rocks/smartstorage": "^6.3.2",
"@push.rocks/smarttime": "^4.2.3",
"@push.rocks/smartwatch": "^6.3.0",
"@push.rocks/smartwatch": "^6.4.0",
"@types/ws": "^8.18.1",
"figures": "^6.1.0",
"ws": "^8.19.0"
"ws": "^8.20.0"
},
"files": [
"ts/**/*",
@@ -65,7 +65,7 @@
"dist_ts_web/**/*",
"assets/**/*",
"cli.js",
"npmextra.json",
".smartconfig.json",
"readme.md"
],
"browserslist": [

3757
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@@ -624,7 +624,7 @@ const messages = parser.parseLine('ok 1 - test ⟦TSTEST:time:42⟧');
## License and Legal Information
This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the [LICENSE](./LICENSE) file.
This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the [license](./license.md) file.
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.

View File

@@ -20,9 +20,9 @@ tap.test('should create a smartmongo instance', async () => {
await smartmongo.stop();
});
tap.test('should create a smarts3 instance', async () => {
const smarts3 = await tapNodeTools.createSmarts3();
await smarts3.stop();
tap.test('should create a smartstorage instance', async () => {
const smartstorage = await tapNodeTools.createSmartStorage();
await smartstorage.stop();
});
tap.start();

View File

@@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@git.zone/tstest',
version: '3.3.0',
version: '3.6.2',
description: 'A powerful, modern test runner for TypeScript with multi-runtime support (Node.js, Deno, Bun, Chromium) and a batteries-included test framework.'
}

View File

@@ -193,6 +193,7 @@ export const runCli = async () => {
await tsTestInstance.runWatch(watchIgnorePatterns);
} else {
await tsTestInstance.run();
await tsTestInstance.cleanup();
}
};

147
ts/readme.md Normal file
View File

@@ -0,0 +1,147 @@
# @git.zone/tstest
> 🚀 Multi-runtime TypeScript test runner with beautiful output and intelligent test orchestration
## Installation
```bash
# tstest is the main CLI entry point
pnpm install --save-dev @git.zone/tstest
```
## Issue Reporting and Security
For reporting bugs, issues, or security vulnerabilities, please visit [community.foss.global/](https://community.foss.global/). This is the central community hub for all issue reporting. Developers who sign and comply with our contribution agreement and go through identification can also get a [code.foss.global/](https://code.foss.global/) account to submit Pull Requests directly.
## Overview
This is the core test runner module of `@git.zone/tstest`. It provides the CLI, test discovery, multi-runtime execution, TAP output parsing, structured logging, and test orchestration. It ties together the entire testing pipeline — from finding test files to dispatching them across Node.js, Chromium, Deno, Bun, and Docker runtimes.
## Key Features
- 🔍 **Smart Test Discovery** — Glob patterns, directory scanning, and single-file mode
- 🏃 **Multi-Runtime Execution** — Node.js, Chromium, Deno, Bun, and Docker adapters
- 📊 **TAP Protocol Parsing** — Full Protocol V2 support with structured metadata
- 🎨 **Rich Logging** — Color-coded output, JSON mode, per-file log files, diff tracking
-**Parallel Execution** — File-level parallel groups via `*.para__N.*` naming
- 👀 **Watch Mode** — Re-run tests on file changes with debouncing
- 🏷️ **Tag Filtering** — Run only tests matching specific tags
- ⏱️ **Timeout Management** — Per-file timeouts with process tree termination
- 📝 **Test File Directives** — Inline comments to configure runtime permissions and flags
- 🔄 **Before Scripts**`package.json`-based lifecycle hooks (`test:before`, `test:before:testfile`)
- 🐳 **Docker Testing** — Run tests inside Docker containers via `*.docker.sh` files
- 🔀 **Legacy Migration** — Automatic rename of `.browser.ts``.chromium.ts` patterns
## Architecture
### Runtime Adapter System
Each runtime is implemented as a `RuntimeAdapter` with a unified interface:
| Adapter | Runtime | How It Executes Tests |
|---|---|---|
| `NodeRuntimeAdapter` | Node.js | Spawns via `@git.zone/tsrun`, streams TAP output |
| `ChromiumRuntimeAdapter` | Chromium | Bundles with esbuild, serves over HTTP, communicates via WebSocket |
| `DenoRuntimeAdapter` | Deno | Runs `deno run` with configurable permissions |
| `BunRuntimeAdapter` | Bun | Runs `bun run` with auto-discovered config |
| `DockerRuntimeAdapter` | Docker | Builds image from Dockerfile, runs `docker run` |
### Test File Naming Convention
Test filenames determine which runtime(s) execute them:
| Pattern | Runtimes |
|---|---|
| `*.ts` | Node.js (default) |
| `*.node.ts` | Node.js only |
| `*.chromium.ts` | Chromium browser |
| `*.deno.ts` | Deno |
| `*.bun.ts` | Bun |
| `*.all.ts` | All runtimes |
| `*.node+chromium.ts` | Node.js + Chromium |
| `*.para__N.ts` | Parallel group N |
| `*.docker.sh` | Docker container |
### Execution Flow
1. **Discovery**`TestDirectory` scans for test files based on mode (directory/file/glob)
2. **Grouping** — Files are split into serial and parallel groups (via `*.para__N.*` naming)
3. **Before Scripts**`test:before` / `test:before:testfile` from `package.json` run
4. **Dispatch** — Each file is parsed for runtime specifiers and directives, then routed to the appropriate `RuntimeAdapter`
5. **Parsing**`TapParser` processes TAP output using Protocol V2, tracking results and events
6. **Aggregation**`TapCombinator` merges results from all test files
7. **Reporting**`TsTestLogger` outputs structured results (console, JSON, or log files)
### Inline Directives
Test files can include comment directives at the top (before imports) to configure runtime behavior:
```typescript
// tstest:deno:allowAll
// tstest:node:flag:--max-old-space-size=4096
// tstest:env:MY_VAR=value
import { tap, expect } from '@git.zone/tstest/tapbundle';
```
Directives in `00init.ts` files apply to all tests in that directory and are merged with per-file directives.
## CLI Usage
```bash
# Directory mode — scan recursively for test files
tstest test/
# File mode — run a single test
tstest test/test.math.ts
# Glob mode — match patterns
tstest "test/**/*.node.ts"
```
### Options
| Flag | Description |
|---|---|
| `--verbose`, `-v` | Show all console output from tests |
| `--quiet`, `-q` | Minimal output for CI |
| `--json` | Machine-readable JSON output |
| `--logfile` | Write per-file logs to `.nogit/testlogs/` |
| `--tags <tags>` | Filter tests by comma-separated tags |
| `--timeout <sec>` | Per-file timeout in seconds |
| `--startFrom <n>` | Start from file number N |
| `--stopAt <n>` | Stop at file number N |
| `--watch`, `-w` | Re-run tests on file changes |
| `--watch-ignore <pat>` | Comma-separated ignore patterns |
| `--no-color` | Disable ANSI colors |
## Public API
```typescript
import { runCli, TestExecutionMode } from '@git.zone/tstest';
```
| Export | Type | Description |
|---|---|---|
| `runCli()` | `async function` | CLI entry point — parses `process.argv` and runs tests |
| `TestExecutionMode` | `enum` | `DIRECTORY`, `FILE`, or `GLOB` |
## License and Legal Information
This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the [license](../license.md) file.
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
### Trademarks
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH or third parties, and are not included within the scope of the MIT license granted herein.
Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines or the guidelines of the respective third-party owners, and any usage must be approved in writing. Third-party trademarks used herein are the property of their respective owners and used only in a descriptive manner, e.g. for an implementation of an API or similar.
### Company Information
Task Venture Capital GmbH
Registered at District Court Bremen HRB 35230 HB, Germany
For any legal inquiries or further information, please contact us via email at hello@task.vc.
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.

View File

@@ -0,0 +1,86 @@
import * as plugins from './tstest.plugins.js';
import type { TsTestLogger } from './tstest.logging.js';
import type { Smartshell } from '@push.rocks/smartshell';
export interface IBeforeScripts {
/** The "test:before" or "test:before:once" script command, or null if not defined */
beforeOnce: string | null;
/** The "test:before:testfile" script command, or null if not defined */
beforeTestfile: string | null;
}
/**
* Load before-script commands from the project's package.json scripts section.
*/
export function loadBeforeScripts(cwd: string): IBeforeScripts {
const result: IBeforeScripts = { beforeOnce: null, beforeTestfile: null };
try {
const packageJsonPath = plugins.path.join(cwd, 'package.json');
const packageJson = JSON.parse(plugins.fs.readFileSync(packageJsonPath, 'utf8'));
const scripts = packageJson?.scripts;
if (!scripts) return result;
// test:before takes precedence over test:before:once (they are aliases)
if (scripts['test:before']) {
result.beforeOnce = scripts['test:before'];
if (scripts['test:before:once']) {
console.warn('Warning: Both "test:before" and "test:before:once" are defined. Using "test:before".');
}
} else if (scripts['test:before:once']) {
result.beforeOnce = scripts['test:before:once'];
}
if (scripts['test:before:testfile']) {
result.beforeTestfile = scripts['test:before:testfile'];
}
} catch {
// No package.json or parse error — return defaults
}
return result;
}
/**
* Execute a before-script command and return whether it succeeded.
*/
export async function runBeforeScript(
smartshellInstance: Smartshell,
command: string,
label: string,
logger: TsTestLogger,
env?: { TSTEST_FILE?: string; TSTEST_RUNTIME?: string },
): Promise<boolean> {
logger.beforeScriptStart(label, command);
const startTime = Date.now();
// Set environment variables if provided
const envKeysSet: string[] = [];
if (env) {
for (const [key, value] of Object.entries(env)) {
if (value !== undefined) {
process.env[key] = value;
envKeysSet.push(key);
}
}
}
try {
const execResult = await smartshellInstance.execStreaming(command);
const result = await execResult.finalPromise;
const durationMs = Date.now() - startTime;
const success = result.exitCode === 0;
logger.beforeScriptEnd(label, success, durationMs);
return success;
} catch {
const durationMs = Date.now() - startTime;
logger.beforeScriptEnd(label, false, durationMs);
return false;
} finally {
// Clean up environment variables
for (const key of envKeysSet) {
delete process.env[key];
}
}
}

View File

@@ -103,7 +103,7 @@ export class ChromiumRuntimeAdapter extends RuntimeAdapter {
// lets get all our paths sorted
const tsbundleCacheDirPath = plugins.path.join(paths.cwd, './.nogit/tstest_cache');
const bundleFileName = testFile.replace('/', '__') + '.js';
const bundleFileName = testFile.replaceAll('/', '__') + '.js';
const bundleFilePath = plugins.path.join(tsbundleCacheDirPath, bundleFileName);
// lets bundle the test
@@ -116,13 +116,12 @@ export class ChromiumRuntimeAdapter extends RuntimeAdapter {
// Find free ports for HTTP and WebSocket
const { httpPort, wsPort } = await this.findFreePorts();
// lets create a server
const server = new plugins.typedserver.TypedServer({
cors: true,
port: httpPort,
serveDir: tsbundleCacheDirPath,
});
server.addRoute('/test', 'GET', async () => {
// Use SmartServe with setHandler() to bypass global ControllerRegistry
const fileServer = new plugins.smartserve.FileServer({ root: tsbundleCacheDirPath });
const server = new plugins.smartserve.SmartServe({ port: httpPort });
server.setHandler(async (request: Request) => {
const url = new URL(request.url);
if (url.pathname === '/test') {
return new Response(`
<html>
<head>
@@ -134,6 +133,10 @@ export class ChromiumRuntimeAdapter extends RuntimeAdapter {
<body></body>
</html>
`, { headers: { 'Content-Type': 'text/html' } });
}
const staticResponse = await fileServer.serve(request);
if (staticResponse) return staticResponse;
return new Response('Not Found', { status: 404 });
});
await server.start();
@@ -156,7 +159,7 @@ export class ChromiumRuntimeAdapter extends RuntimeAdapter {
await this.smartbrowserInstance.start();
const evaluatePromise = this.smartbrowserInstance.evaluateOnPage(
`http://localhost:${httpPort}/test?bundleName=${bundleFileName}`,
`http://localhost:${httpPort}/test?bundleName=${encodeURIComponent(bundleFileName)}`,
async () => {
// lets enable real time comms
const ws = new WebSocket(`ws://localhost:${globalThis.wsPort}`);
@@ -274,6 +277,9 @@ export class ChromiumRuntimeAdapter extends RuntimeAdapter {
}
try {
for (const client of wss.clients) {
client.terminate();
}
wss.close();
} catch (error) {
// WebSocket server might already be closed

View File

@@ -190,6 +190,17 @@ import '${absoluteTestFile.replace(/\\/g, '/')}';
runCommand = `${loaderCommand.command} ${loaderCommand.args.join(' ')}`;
}
// Pre-resolve dependencies for the Deno test entrypoint
const installTarget = loaderPath || testFile;
const installArgs = ['install', '--entrypoint', installTarget];
if (mergedOptions.configPath) {
installArgs.push('--config', mergedOptions.configPath);
}
const installCommand = `deno ${installArgs.join(' ')}`;
console.log(cs(` ⏳ Resolving Deno dependencies for ${plugins.path.basename(testFile)}...`, 'blue'));
await this.smartshellInstance.execSilent(installCommand, { cwd: process.cwd() });
console.log(cs(` ✓ Deno dependencies resolved`, 'green'));
const execResultStreaming = await this.smartshellInstance.execStreamingSilent(runCommand);
// If we created a loader file, clean it up after test execution

View File

@@ -184,6 +184,8 @@ import '${absoluteTestFile.replace(/\\/g, '/')}';
timeoutId = setTimeout(async () => {
// Use tsrun's terminate() to gracefully kill the process
await tsrunProcess.terminate();
// Swallow the expected rejection from the killed child process
tsrunProcess.exitCode.catch(() => {});
reject(new Error(`Test file timed out after ${this.timeoutSeconds} seconds`));
}, timeoutMs);
});

View File

@@ -27,6 +27,9 @@ import {
hasDirectives,
} from './tstest.classes.testfile.directives.js';
// Before-script support
import { loadBeforeScripts, runBeforeScript, type IBeforeScripts } from './tstest.classes.beforescripts.js';
export class TsTest {
public testDir: TestDirectory;
public executionMode: TestExecutionMode;
@@ -48,6 +51,8 @@ export class TsTest {
public runtimeRegistry = new RuntimeAdapterRegistry();
public dockerAdapter: DockerRuntimeAdapter | null = null;
private beforeScripts: IBeforeScripts | null = null;
constructor(cwdArg: string, testPathArg: string, executionModeArg: TestExecutionMode, logOptions: LogOptions = {}, tags: string[] = [], startFromFile: number | null = null, stopAtFile: number | null = null, timeoutSeconds: number | null = null) {
this.executionMode = executionModeArg;
this.testDir = new TestDirectory(cwdArg, testPathArg, executionModeArg);
@@ -98,6 +103,21 @@ export class TsTest {
await this.movePreviousLogFiles();
}
// Load and execute test:before script (runs once per test run)
this.beforeScripts = loadBeforeScripts(this.testDir.cwd);
if (this.beforeScripts.beforeOnce) {
const success = await runBeforeScript(
this.smartshellInstance,
this.beforeScripts.beforeOnce,
'test:before',
this.logger,
);
if (!success) {
this.logger.error('test:before script failed. Aborting test run.');
process.exit(1);
}
}
const testGroups = await this.testDir.getTestFileGroups();
const allFiles = [...testGroups.serial, ...Object.values(testGroups.parallelGroups).flat()];
@@ -139,6 +159,13 @@ export class TsTest {
tapCombinator.evaluate();
}
/**
* Clean up all long-lived resources so the Node.js event loop can drain naturally.
*/
public async cleanup() {
this.smartshellInstance.smartexit.deregister();
}
public async runWatch(ignorePatterns: string[] = []) {
const smartwatchInstance = new plugins.smartwatch.Smartwatch([this.testDir.cwd]);
@@ -280,6 +307,22 @@ export class TsTest {
if (adapters.length === 1) {
// Single runtime - no sections needed
const adapter = adapters[0];
// Run test:before:testfile if defined
if (this.beforeScripts?.beforeTestfile) {
const success = await runBeforeScript(
this.smartshellInstance,
this.beforeScripts.beforeTestfile,
`test:before:testfile (${fileName})`,
this.logger,
{ TSTEST_FILE: fileNameArg, TSTEST_RUNTIME: adapter.id },
);
if (!success) {
this.logger.error(`test:before:testfile failed for ${fileName}. Skipping test file.`);
return;
}
}
const options = hasDirectives(directives) ? directivesToRuntimeOptions(directives, adapter.id) : undefined;
const tapParser = await adapter.run(fileNameArg, fileIndex, totalFiles, options);
tapCombinator.addTapParser(tapParser);
@@ -288,6 +331,23 @@ export class TsTest {
for (let i = 0; i < adapters.length; i++) {
const adapter = adapters[i];
this.logger.sectionStart(`Part ${i + 1}: ${adapter.displayName}`);
// Run test:before:testfile if defined (runs before each runtime)
if (this.beforeScripts?.beforeTestfile) {
const success = await runBeforeScript(
this.smartshellInstance,
this.beforeScripts.beforeTestfile,
`test:before:testfile (${fileName} on ${adapter.displayName})`,
this.logger,
{ TSTEST_FILE: fileNameArg, TSTEST_RUNTIME: adapter.id },
);
if (!success) {
this.logger.error(`test:before:testfile failed for ${fileName} on ${adapter.displayName}. Skipping.`);
this.logger.sectionEnd();
continue;
}
}
const options = hasDirectives(directives) ? directivesToRuntimeOptions(directives, adapter.id) : undefined;
const tapParser = await adapter.run(fileNameArg, fileIndex, totalFiles, options);
tapCombinator.addTapParser(tapParser);
@@ -310,6 +370,21 @@ export class TsTest {
return;
}
// Run test:before:testfile if defined
if (this.beforeScripts?.beforeTestfile) {
const success = await runBeforeScript(
this.smartshellInstance,
this.beforeScripts.beforeTestfile,
`test:before:testfile (${fileNameArg} on Docker)`,
this.logger,
{ TSTEST_FILE: fileNameArg, TSTEST_RUNTIME: 'docker' },
);
if (!success) {
this.logger.error(`test:before:testfile failed for ${fileNameArg}. Skipping.`);
return;
}
}
try {
const tapParser = await this.dockerAdapter.run(fileNameArg, fileIndex, totalFiles);
tapCombinator.addTapParser(tapParser);
@@ -318,338 +393,6 @@ export class TsTest {
}
}
public async runInNode(fileNameArg: string, index: number, total: number): Promise<TapParser> {
this.logger.testFileStart(fileNameArg, 'node.js', index, total);
const tapParser = new TapParser(fileNameArg + ':node', this.logger);
// tsrun options
let tsrunOptions = '';
if (process.argv.includes('--web')) {
tsrunOptions += ' --web';
}
// Set filter tags as environment variable
if (this.filterTags.length > 0) {
process.env.TSTEST_FILTER_TAGS = this.filterTags.join(',');
}
// Check for 00init.ts file in test directory
const testDir = plugins.path.dirname(fileNameArg);
const initFile = plugins.path.join(testDir, '00init.ts');
let runCommand = `tsrun ${fileNameArg}${tsrunOptions}`;
const initFileExists = await plugins.smartfsInstance.file(initFile).exists();
// If 00init.ts exists, run it first
if (initFileExists) {
// Create a temporary loader file that imports both 00init.ts and the test file
const absoluteInitFile = plugins.path.resolve(initFile);
const absoluteTestFile = plugins.path.resolve(fileNameArg);
const loaderContent = `
import '${absoluteInitFile.replace(/\\/g, '/')}';
import '${absoluteTestFile.replace(/\\/g, '/')}';
`;
const loaderPath = plugins.path.join(testDir, `.loader_${plugins.path.basename(fileNameArg)}`);
await plugins.smartfsInstance.file(loaderPath).write(loaderContent);
runCommand = `tsrun ${loaderPath}${tsrunOptions}`;
}
const execResultStreaming = await this.smartshellInstance.execStreamingSilent(runCommand);
// If we created a loader file, clean it up after test execution
if (initFileExists) {
const loaderPath = plugins.path.join(testDir, `.loader_${plugins.path.basename(fileNameArg)}`);
const cleanup = () => {
try {
if (plugins.fs.existsSync(loaderPath)) {
plugins.fs.rmSync(loaderPath, { force: true });
}
} catch (e) {
// Ignore cleanup errors
}
};
execResultStreaming.childProcess.on('exit', cleanup);
execResultStreaming.childProcess.on('error', cleanup);
}
// Start warning timer if no timeout was specified
let warningTimer: NodeJS.Timeout | null = null;
if (this.timeoutSeconds === null) {
warningTimer = setTimeout(() => {
console.error('');
console.error(cs('⚠️ WARNING: Test file is running for more than 1 minute', 'orange'));
console.error(cs(` File: ${fileNameArg}`, 'orange'));
console.error(cs(' Consider using --timeout option to set a timeout for test files.', 'orange'));
console.error(cs(' Example: tstest test --timeout=300 (for 5 minutes)', 'orange'));
console.error('');
}, 60000); // 1 minute
}
// Handle timeout if specified
if (this.timeoutSeconds !== null) {
const timeoutMs = this.timeoutSeconds * 1000;
let timeoutId: NodeJS.Timeout;
const timeoutPromise = new Promise<void>((_resolve, reject) => {
timeoutId = setTimeout(async () => {
// Use smartshell's terminate() to kill entire process tree
await execResultStreaming.terminate();
reject(new Error(`Test file timed out after ${this.timeoutSeconds} seconds`));
}, timeoutMs);
});
try {
await Promise.race([
tapParser.handleTapProcess(execResultStreaming.childProcess),
timeoutPromise
]);
// Clear timeout if test completed successfully
clearTimeout(timeoutId);
} catch (error) {
// Clear warning timer if it was set
if (warningTimer) {
clearTimeout(warningTimer);
}
// Handle timeout error
tapParser.handleTimeout(this.timeoutSeconds);
// Ensure entire process tree is killed if still running
try {
await execResultStreaming.kill(); // This kills the entire process tree with SIGKILL
} catch (killError) {
// Process tree might already be dead
}
await tapParser.evaluateFinalResult();
}
} else {
await tapParser.handleTapProcess(execResultStreaming.childProcess);
}
// Clear warning timer if it was set
if (warningTimer) {
clearTimeout(warningTimer);
}
return tapParser;
}
private async findFreePorts(): Promise<{ httpPort: number; wsPort: number }> {
const smartnetwork = new plugins.smartnetwork.SmartNetwork();
// Find random free HTTP port in range 30000-40000 to minimize collision chance
const httpPort = await smartnetwork.findFreePort(30000, 40000, { randomize: true });
if (!httpPort) {
throw new Error('Could not find a free HTTP port in range 30000-40000');
}
// Find random free WebSocket port, excluding the HTTP port to ensure they're different
const wsPort = await smartnetwork.findFreePort(30000, 40000, {
randomize: true,
exclude: [httpPort]
});
if (!wsPort) {
throw new Error('Could not find a free WebSocket port in range 30000-40000');
}
// Log selected ports for debugging
if (!this.logger.options.quiet) {
console.log(`Selected ports - HTTP: ${httpPort}, WebSocket: ${wsPort}`);
}
return { httpPort, wsPort };
}
public async runInChrome(fileNameArg: string, index: number, total: number): Promise<TapParser> {
this.logger.testFileStart(fileNameArg, 'chromium', index, total);
// lets get all our paths sorted
const tsbundleCacheDirPath = plugins.path.join(paths.cwd, './.nogit/tstest_cache');
const bundleFileName = fileNameArg.replace('/', '__') + '.js';
const bundleFilePath = plugins.path.join(tsbundleCacheDirPath, bundleFileName);
// lets bundle the test
try { await plugins.smartfsInstance.directory(tsbundleCacheDirPath).recursive().delete(); } catch (e) { /* may not exist */ }
await plugins.smartfsInstance.directory(tsbundleCacheDirPath).recursive().create();
await this.tsbundleInstance.build(process.cwd(), fileNameArg, bundleFilePath, {
bundler: 'esbuild',
});
// Find free ports for HTTP and WebSocket
const { httpPort, wsPort } = await this.findFreePorts();
// lets create a server
const server = new plugins.typedserver.TypedServer({
cors: true,
port: httpPort,
serveDir: tsbundleCacheDirPath,
});
server.addRoute('/test', 'GET', async () => {
return new Response(`
<html>
<head>
<script>
globalThis.testdom = true;
globalThis.wsPort = ${wsPort};
</script>
</head>
<body></body>
</html>
`, { headers: { 'Content-Type': 'text/html' } });
});
await server.start();
// lets handle realtime comms
const tapParser = new TapParser(fileNameArg + ':chrome', this.logger);
const wss = new plugins.ws.WebSocketServer({ port: wsPort });
wss.on('connection', (ws) => {
ws.on('message', (message) => {
const messageStr = message.toString();
if (messageStr.startsWith('console:')) {
const [, level, ...messageParts] = messageStr.split(':');
this.logger.browserConsole(messageParts.join(':'), level);
} else {
tapParser.handleTapLog(messageStr);
}
});
});
// lets do the browser bit with timeout handling
await this.smartbrowserInstance.start();
const evaluatePromise = this.smartbrowserInstance.evaluateOnPage(
`http://localhost:${httpPort}/test?bundleName=${bundleFileName}`,
async () => {
// lets enable real time comms
const ws = new WebSocket(`ws://localhost:${globalThis.wsPort}`);
await new Promise((resolve) => (ws.onopen = resolve));
// Ensure this function is declared with 'async'
const logStore = [];
const originalLog = console.log;
const originalError = console.error;
// Override console methods to capture the logs
console.log = (...args: any[]) => {
logStore.push(args.join(' '));
ws.send(args.join(' '));
originalLog(...args);
};
console.error = (...args: any[]) => {
logStore.push(args.join(' '));
ws.send(args.join(' '));
originalError(...args);
};
const bundleName = new URLSearchParams(window.location.search).get('bundleName');
originalLog(`::TSTEST IN CHROMIUM:: Relevant Script name is: ${bundleName}`);
try {
// Dynamically import the test module
const testModule = await import(`/${bundleName}`);
if (testModule && testModule.default && testModule.default instanceof Promise) {
// Execute the exported test function
await testModule.default;
} else if (testModule && testModule.default && typeof testModule.default.then === 'function') {
console.log('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
console.log('Test module default export is just promiselike: Something might be messing with your Promise implementation.');
console.log('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
await testModule.default;
} else if (globalThis.tapPromise && typeof globalThis.tapPromise.then === 'function') {
console.log('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
console.log('Using globalThis.tapPromise');
console.log('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
await testModule.default;
} else {
console.error('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
console.error('Test module does not export a default promise.');
console.error('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
console.log(`We got: ${JSON.stringify(testModule)}`);
}
} catch (err) {
console.error(err);
}
return logStore.join('\n');
}
);
// Start warning timer if no timeout was specified
let warningTimer: NodeJS.Timeout | null = null;
if (this.timeoutSeconds === null) {
warningTimer = setTimeout(() => {
console.error('');
console.error(cs('⚠️ WARNING: Test file is running for more than 1 minute', 'orange'));
console.error(cs(` File: ${fileNameArg}`, 'orange'));
console.error(cs(' Consider using --timeout option to set a timeout for test files.', 'orange'));
console.error(cs(' Example: tstest test --timeout=300 (for 5 minutes)', 'orange'));
console.error('');
}, 60000); // 1 minute
}
// Handle timeout if specified
if (this.timeoutSeconds !== null) {
const timeoutMs = this.timeoutSeconds * 1000;
let timeoutId: NodeJS.Timeout;
const timeoutPromise = new Promise<void>((_resolve, reject) => {
timeoutId = setTimeout(() => {
reject(new Error(`Test file timed out after ${this.timeoutSeconds} seconds`));
}, timeoutMs);
});
try {
await Promise.race([
evaluatePromise,
timeoutPromise
]);
// Clear timeout if test completed successfully
clearTimeout(timeoutId);
} catch (error) {
// Clear warning timer if it was set
if (warningTimer) {
clearTimeout(warningTimer);
}
// Handle timeout error
tapParser.handleTimeout(this.timeoutSeconds);
}
} else {
await evaluatePromise;
}
// Clear warning timer if it was set
if (warningTimer) {
clearTimeout(warningTimer);
}
// Always clean up resources, even on timeout
try {
await this.smartbrowserInstance.stop();
} catch (error) {
// Browser might already be stopped
}
try {
await server.stop();
} catch (error) {
// Server might already be stopped
}
try {
wss.close();
} catch (error) {
// WebSocket server might already be closed
}
console.log(
`${cs('=> ', 'blue')} Stopped ${cs(fileNameArg, 'orange')} chromium instance and server.`
);
// Always evaluate final result (handleTimeout just sets up the test state)
await tapParser.evaluateFinalResult();
return tapParser;
}
public async runInDeno() {}
private async movePreviousLogFiles() {
const logDir = plugins.path.join('.nogit', 'testlogs');
const previousDir = plugins.path.join('.nogit', 'testlogs', 'previous');

View File

@@ -122,6 +122,40 @@ export class TsTestLogger {
this.log(this.format(`[${'█'.repeat(filled)}${'░'.repeat(empty)}] ${message}`, 'dim'));
}
// Before-script lifecycle hooks
beforeScriptStart(label: string, command: string) {
if (this.options.json) {
this.logJson({ event: 'beforeScript', label, command });
return;
}
if (this.options.quiet) {
this.log(`Running ${label}...`);
} else {
this.log(this.format(`\n🔧 Running ${label}...`, 'cyan'));
this.log(this.format(` Command: ${command}`, 'dim'));
}
}
beforeScriptEnd(label: string, success: boolean, durationMs: number) {
const durationStr = durationMs >= 1000 ? `${(durationMs / 1000).toFixed(1)}s` : `${durationMs}ms`;
if (this.options.json) {
this.logJson({ event: 'beforeScriptEnd', label, success, durationMs });
return;
}
if (this.options.quiet) {
this.log(success ? `${label} done (${durationStr})` : `${label} FAILED`);
} else {
if (success) {
this.log(this.format(`${label} completed (${durationStr})`, 'green'));
} else {
this.log(this.format(`${label} failed (${durationStr})`, 'red'));
}
}
}
// Test discovery
testDiscovery(count: number, pattern: string, executionMode: string) {
if (this.options.json) {

View File

@@ -4,16 +4,10 @@ import * as path from 'path';
export { fs, path };
// @apiglobal scope
import * as typedserver from '@api.global/typedserver';
export {
typedserver
}
// @push.rocks scope
import * as consolecolor from '@push.rocks/consolecolor';
import * as smartbrowser from '@push.rocks/smartbrowser';
import * as smartserve from '@push.rocks/smartserve';
import * as smartdelay from '@push.rocks/smartdelay';
import * as smartfile from '@push.rocks/smartfile';
import * as smartfs from '@push.rocks/smartfs';
@@ -28,6 +22,7 @@ import * as tapbundle from '../dist_ts_tapbundle/index.js';
export {
consolecolor,
smartbrowser,
smartserve,
smartdelay,
smartfile,
smartfs,

View File

@@ -615,7 +615,7 @@ tap.test('should use context', async (tapTools) => {
## License and Legal Information
This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the [LICENSE](../LICENSE) file.
This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the [license](../license.md) file.
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.

View File

@@ -586,7 +586,7 @@ No runtime-specific APIs are used, making it truly portable.
## License and Legal Information
This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the [LICENSE](../LICENSE) file.
This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the [license](../license.md) file.
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.

View File

@@ -83,16 +83,15 @@ class TapNodeTools {
}
/**
* create and return a smarts3 instance
* create and return a smartstorage instance
*/
public async createSmarts3() {
const smarts3Mod = await import('@push.rocks/smarts3');
const smarts3Instance = new smarts3Mod.Smarts3({
public async createSmartStorage() {
const smartstorageMod = await import('@push.rocks/smartstorage');
const smartstorageInstance = await smartstorageMod.SmartStorage.createAndStart({
server: { port: 3003 },
storage: { cleanSlate: true },
});
await smarts3Instance.start();
return smarts3Instance;
return smartstorageInstance;
}
// ============

View File

@@ -207,12 +207,12 @@ Uses [@push.rocks/smartmongo](https://code.foss.global/push.rocks/smartmongo).
Create a local S3-compatible storage instance for testing.
```typescript
const s3 = await tapNodeTools.createSmarts3();
const s3 = await tapNodeTools.createSmartStorage();
// ... run storage tests ...
await s3.stop();
```
Default config: port 3003, clean slate enabled. Uses [@push.rocks/smarts3](https://code.foss.global/push.rocks/smarts3).
Default config: port 3003, clean slate enabled. Uses [@push.rocks/smartstorage](https://code.foss.global/push.rocks/smartstorage).
---
@@ -244,13 +244,13 @@ test/mytest.all.ts ❌ Will fail in Deno/Bun/Chromium
- [@push.rocks/smartshell](https://code.foss.global/push.rocks/smartshell) — Shell command execution
- [@push.rocks/smartcrypto](https://code.foss.global/push.rocks/smartcrypto) — Certificate generation
- [@push.rocks/smartmongo](https://code.foss.global/push.rocks/smartmongo) — MongoDB testing
- [@push.rocks/smarts3](https://code.foss.global/push.rocks/smarts3) — S3 storage testing
- [@push.rocks/smartstorage](https://code.foss.global/push.rocks/smartstorage) — S3 storage testing
- [@push.rocks/smartfile](https://code.foss.global/push.rocks/smartfile) — File operations
- [@push.rocks/smartrequest](https://code.foss.global/push.rocks/smartrequest) — HTTP requests
## License and Legal Information
This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the [LICENSE](../LICENSE) file.
This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the [license](../license.md) file.
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.

View File

@@ -6,7 +6,9 @@
"module": "NodeNext",
"moduleResolution": "NodeNext",
"esModuleInterop": true,
"verbatimModuleSyntax": true
"verbatimModuleSyntax": true,
"strict": false,
"types": ["node"]
},
"exclude": [
"dist_*/**/*.d.ts"