Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
d9e0f1f758 | |||
42cd08eb1c | |||
553d5f0df7 | |||
6cc883dede | |||
fa9abbc4db | |||
56f0f0be16 |
23
changelog.md
23
changelog.md
@ -1,5 +1,28 @@
|
||||
# Changelog
|
||||
|
||||
## 2025-05-15 - 1.4.0 - feat(logging)
|
||||
Display failed test console logs in default mode
|
||||
|
||||
- Introduce log buffering in TsTestLogger to capture console output for failed tests
|
||||
- Enhance TapParser to collect and display error details when tests fail
|
||||
- Update README and project plan to document log improvements for debugging
|
||||
|
||||
## 2025-05-15 - 1.3.1 - fix(settings)
|
||||
Add local permissions configuration and remove obsolete test output log
|
||||
|
||||
- Added .claude/settings.local.json to configure allowed permissions for web fetch and bash commands
|
||||
- Removed test-output.log to eliminate accidental commit of test artifacts
|
||||
|
||||
## 2025-05-15 - 1.3.0 - feat(logger)
|
||||
Improve logging output and add --logfile support for persistent logs
|
||||
|
||||
- Add new .claude/settings.local.json with logging permissions configuration
|
||||
- Remove obsolete readme.plan.md
|
||||
- Introduce test/test.console.ts to capture and display console outputs during tests
|
||||
- Update CLI in ts/index.ts to replace '--log-file' with '--logfile' flag
|
||||
- Enhance TsTestLogger to support file logging, clean ANSI sequences, and improved JSON output
|
||||
- Forward TAP protocol logs to testConsoleOutput in TapParser for better console distinction
|
||||
|
||||
## 2025-05-15 - 1.2.0 - feat(logging)
|
||||
Improve logging output, CLI option parsing, and test report formatting.
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@git.zone/tstest",
|
||||
"version": "1.2.0",
|
||||
"version": "1.4.0",
|
||||
"private": false,
|
||||
"description": "a test utility to run tests that match test/**/*.ts",
|
||||
"main": "dist_ts/index.js",
|
||||
|
209
readme.md
209
readme.md
@ -1,61 +1,204 @@
|
||||
# @gitzone/tstest
|
||||
a test utility to run tests that match test/**/*.ts
|
||||
🧪 **A powerful, modern test runner for TypeScript** - making your test runs beautiful and informative!
|
||||
|
||||
## Availabililty and Links
|
||||
* [npmjs.org (npm package)](https://www.npmjs.com/package/@gitzone/tstest)
|
||||
* [gitlab.com (source)](https://gitlab.com/gitzone/tstest)
|
||||
* [github.com (source mirror)](https://github.com/gitzone/tstest)
|
||||
* [docs (typedoc)](https://gitzone.gitlab.io/tstest/)
|
||||
* [code.foss.global (source)](https://code.foss.global/gitzone/tstest)
|
||||
|
||||
## Status for master
|
||||
## Why tstest?
|
||||
|
||||
Status Category | Status Badge
|
||||
-- | --
|
||||
GitLab Pipelines | [](https://lossless.cloud)
|
||||
GitLab Pipline Test Coverage | [](https://lossless.cloud)
|
||||
npm | [](https://lossless.cloud)
|
||||
Snyk | [](https://lossless.cloud)
|
||||
TypeScript Support | [](https://lossless.cloud)
|
||||
node Support | [](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||
Code Style | [](https://lossless.cloud)
|
||||
PackagePhobia (total standalone install weight) | [](https://lossless.cloud)
|
||||
PackagePhobia (package size on registry) | [](https://lossless.cloud)
|
||||
BundlePhobia (total size when bundled) | [](https://lossless.cloud)
|
||||
Platform support | [](https://lossless.cloud) [](https://lossless.cloud)
|
||||
**tstest** is a TypeScript test runner that makes testing delightful. It's designed for modern development workflows with beautiful output, flexible test execution, and powerful features that make debugging a breeze.
|
||||
|
||||
### ✨ Key Features
|
||||
|
||||
- 🎯 **Smart Test Execution** - Run all tests, single files, or use glob patterns
|
||||
- 🎨 **Beautiful Output** - Color-coded results with emojis and clean formatting
|
||||
- 📊 **Multiple Output Modes** - Choose from normal, quiet, verbose, or JSON output
|
||||
- 🔍 **Automatic Discovery** - Finds all your test files automatically
|
||||
- 🌐 **Cross-Environment** - Supports Node.js and browser testing
|
||||
- 📝 **Detailed Logging** - Optional file logging for debugging
|
||||
- ⚡ **Performance Metrics** - See which tests are slow
|
||||
- 🤖 **CI/CD Ready** - JSON output mode for automation
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
npm install --save-dev @gitzone/tstest
|
||||
# or with pnpm
|
||||
pnpm add -D @gitzone/tstest
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
## cli usage
|
||||
### Basic Test Execution
|
||||
|
||||
lets assume we have a directory called test/ where all our tests arae defined. Simply type
|
||||
|
||||
```
|
||||
```bash
|
||||
# Run all tests in a directory
|
||||
tstest test/
|
||||
|
||||
# Run a specific test file
|
||||
tstest test/test.mycomponent.ts
|
||||
|
||||
# Use glob patterns
|
||||
tstest "test/**/*.spec.ts"
|
||||
tstest "test/unit/*.ts"
|
||||
```
|
||||
|
||||
to run all tests.
|
||||
### Execution Modes
|
||||
|
||||
## Syntax
|
||||
**tstest** intelligently detects how you want to run your tests:
|
||||
|
||||
tstest supports tap syntax. In other words your testfiles are run in a subprocess, and the console output contains trigger messages for tstest to determine test status. Inside your testfile you should use `@pushrocks/tapbundle` for the best results.
|
||||
1. **Directory mode** - Recursively finds all test files
|
||||
2. **File mode** - Runs a single test file
|
||||
3. **Glob mode** - Uses pattern matching for flexible test selection
|
||||
|
||||
## Environments
|
||||
### Command Line Options
|
||||
|
||||
tstest supports different environments:
|
||||
| Option | Description |
|
||||
|--------|-------------|
|
||||
| `--quiet`, `-q` | Minimal output - perfect for CI environments |
|
||||
| `--verbose`, `-v` | Show all console output from tests |
|
||||
| `--no-color` | Disable colored output |
|
||||
| `--json` | Output results as JSON |
|
||||
| `--logfile` | Save detailed logs to `.nogit/testlogs/[testname].log` |
|
||||
|
||||
- a testfile called `test-something.node.ts` will be run in node
|
||||
- a testfile called `test-something.chrome.ts` will be run in chrome environment (bundled through parcel and run through puppeteer)
|
||||
- a testfile called `test-something.both.ts` will be run in node an chrome, which is good for isomorphic packages.
|
||||
### Example Outputs
|
||||
|
||||
> note: there is alpha support for the deno environment by naming a file test-something.deno.ts
|
||||
#### Normal Output (Default)
|
||||
```
|
||||
🔍 Test Discovery
|
||||
Mode: directory
|
||||
Pattern: test
|
||||
Found: 4 test file(s)
|
||||
|
||||
▶️ test/test.ts (1/4)
|
||||
Runtime: node.js
|
||||
✅ prepare test (1ms)
|
||||
Summary: 1/1 PASSED
|
||||
|
||||
📊 Test Summary
|
||||
┌────────────────────────────────┐
|
||||
│ Total Files: 4 │
|
||||
│ Total Tests: 4 │
|
||||
│ Passed: 4 │
|
||||
│ Failed: 0 │
|
||||
│ Duration: 542ms │
|
||||
└────────────────────────────────┘
|
||||
|
||||
ALL TESTS PASSED! 🎉
|
||||
```
|
||||
|
||||
#### Quiet Mode
|
||||
```
|
||||
Found 4 tests
|
||||
✅ test functionality works
|
||||
✅ api calls return expected data
|
||||
✅ error handling works correctly
|
||||
✅ performance is within limits
|
||||
|
||||
Summary: 4/4 | 542ms | PASSED
|
||||
```
|
||||
|
||||
#### Verbose Mode
|
||||
Shows all console output from your tests, making debugging easier:
|
||||
```
|
||||
▶️ test/api.test.ts (1/1)
|
||||
Runtime: node.js
|
||||
Making API call to /users...
|
||||
Response received: 200 OK
|
||||
Processing user data...
|
||||
✅ api calls return expected data (145ms)
|
||||
Summary: 1/1 PASSED
|
||||
```
|
||||
|
||||
#### JSON Mode
|
||||
Perfect for CI/CD pipelines:
|
||||
```json
|
||||
{"event":"discovery","count":4,"pattern":"test","executionMode":"directory"}
|
||||
{"event":"fileStart","filename":"test/test.ts","runtime":"node.js","index":1,"total":4}
|
||||
{"event":"testResult","testName":"prepare test","passed":true,"duration":1}
|
||||
{"event":"summary","summary":{"totalFiles":4,"totalTests":4,"totalPassed":4,"totalFailed":0,"totalDuration":542}}
|
||||
```
|
||||
|
||||
## Test File Naming Conventions
|
||||
|
||||
tstest supports different test environments through file naming:
|
||||
|
||||
| Pattern | Environment | Example |
|
||||
|---------|-------------|---------|
|
||||
| `*.ts` | Node.js (default) | `test.basic.ts` |
|
||||
| `*.node.ts` | Node.js only | `test.api.node.ts` |
|
||||
| `*.chrome.ts` | Chrome browser | `test.dom.chrome.ts` |
|
||||
| `*.browser.ts` | Browser environment | `test.ui.browser.ts` |
|
||||
| `*.both.ts` | Both Node.js and browser | `test.isomorphic.both.ts` |
|
||||
|
||||
### Writing Tests
|
||||
|
||||
tstest uses TAP (Test Anything Protocol) for test output. Use `@pushrocks/tapbundle` for the best experience:
|
||||
|
||||
```typescript
|
||||
import { expect, tap } from '@push.rocks/tapbundle';
|
||||
|
||||
tap.test('my awesome test', async () => {
|
||||
const result = await myFunction();
|
||||
expect(result).toEqual('expected value');
|
||||
});
|
||||
|
||||
tap.start();
|
||||
```
|
||||
|
||||
## Advanced Features
|
||||
|
||||
### Glob Pattern Support
|
||||
|
||||
Run specific test patterns:
|
||||
```bash
|
||||
# Run all unit tests
|
||||
tstest "test/unit/**/*.ts"
|
||||
|
||||
# Run all integration tests
|
||||
tstest "test/integration/*.test.ts"
|
||||
|
||||
# Run multiple patterns
|
||||
tstest "test/**/*.spec.ts" "test/**/*.test.ts"
|
||||
```
|
||||
|
||||
### Automatic Logging
|
||||
|
||||
Use `--logfile` to automatically save test output:
|
||||
```bash
|
||||
tstest test/ --logfile
|
||||
```
|
||||
|
||||
This creates detailed logs in `.nogit/testlogs/[testname].log` for each test file.
|
||||
|
||||
### Performance Analysis
|
||||
|
||||
In verbose mode, see performance metrics:
|
||||
```
|
||||
⏱️ Performance Metrics:
|
||||
Average per test: 135ms
|
||||
Slowest test: api integration test (486ms)
|
||||
```
|
||||
|
||||
### CI/CD Integration
|
||||
|
||||
For continuous integration, combine quiet and JSON modes:
|
||||
```bash
|
||||
# GitHub Actions example
|
||||
tstest test/ --json > test-results.json
|
||||
|
||||
# Or minimal output
|
||||
tstest test/ --quiet
|
||||
```
|
||||
|
||||
## Contribution
|
||||
|
||||
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
|
||||
|
||||
For further information read the linked docs at the top of this readme.
|
||||
## License
|
||||
|
||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
|
||||
|
||||
[](https://maintainedby.lossless.com)
|
||||
[](https://maintainedby.lossless.com)
|
218
readme.plan.md
218
readme.plan.md
@ -1,199 +1,41 @@
|
||||
# Plan for improving logging and output in tstest
|
||||
# Plan for showing logs for failed tests
|
||||
|
||||
!! FIRST: Reread /home/philkunz/.claude/CLAUDE.md to ensure following all guidelines !!
|
||||
|
||||
## Goal - ✅ MOSTLY COMPLETED
|
||||
- ✅ Make test output cleaner and more visually appealing
|
||||
- ✅ Add structured logging capabilities
|
||||
- ✅ Support different verbosity levels
|
||||
- ✅ Improve CI/CD compatibility
|
||||
- ✅ Add progress indicators and timing summaries
|
||||
## Goal
|
||||
When a test fails, we want to display all the console logs from that failed test in the terminal, even without the --verbose flag. This makes debugging failed tests much easier.
|
||||
|
||||
## Current State - UPDATED
|
||||
- ✅ Clean, modern visual design with Unicode characters
|
||||
- ✅ Structured output format (JSON support)
|
||||
- ✅ Multiple verbosity levels (quiet, normal, verbose)
|
||||
- ✅ Real-time output with cleaner formatting
|
||||
- ✅ Better error aggregation and display
|
||||
- ✅ TAP protocol support integrated with new logger
|
||||
## Current Behavior
|
||||
- Default mode: Only shows test results, no console logs
|
||||
- Verbose mode: Shows all console logs from all tests
|
||||
- When a test fails: Only shows the error message
|
||||
|
||||
## Completed Improvements
|
||||
## Desired Behavior
|
||||
- Default mode: Shows test results, and IF a test fails, shows all console logs from that failed test
|
||||
- Verbose mode: Shows all console logs from all tests (unchanged)
|
||||
- When a test fails: Shows all console logs from that test plus the error
|
||||
|
||||
### 1. ✅ Created new TsTestLogger class
|
||||
- ✅ Centralized logging with consistent formatting
|
||||
- ✅ Support for different output modes (normal, quiet, verbose)
|
||||
- ✅ Better visual hierarchy with modern Unicode characters
|
||||
- ✅ Progress indicators for multiple test files
|
||||
- ✅ Structured error collection and display
|
||||
## Implementation Plan
|
||||
|
||||
### 2. ✅ Updated visual design
|
||||
- ✅ Replaced heavy separators with cleaner alternatives
|
||||
- ✅ Used better emoji and Unicode characters
|
||||
- ✅ Added indentation for hierarchical display
|
||||
- ✅ Grouped related information visually
|
||||
- ✅ Added color coding consistently
|
||||
### 1. Update TapParser
|
||||
- Store console logs for each test temporarily
|
||||
- When a test fails, mark that its logs should be shown
|
||||
|
||||
### 3. ✅ Added command-line options
|
||||
- ✅ `--quiet` for minimal CI-friendly output
|
||||
- ✅ `--verbose` for detailed debugging information
|
||||
- ✅ `--no-color` for environments without color support
|
||||
- ✅ `--json` for structured JSON output
|
||||
- ⏳ `--log-file <path>` for persistent logging (TODO)
|
||||
### 2. Update TsTestLogger
|
||||
- Add a new method to handle failed test logs
|
||||
- Modify testConsoleOutput to buffer logs when not in verbose mode
|
||||
- When a test fails, flush the buffered logs for that test
|
||||
|
||||
### 4. ✅ Improved progress feedback
|
||||
- ⏳ Show progress bar for multiple files (TODO)
|
||||
- ✅ Display current file being executed
|
||||
- ✅ Show real-time test counts
|
||||
- ⏳ Add ETA for long test suites (TODO)
|
||||
### 3. Update test result handling
|
||||
- When a test fails, trigger display of all buffered logs for that test
|
||||
- Clear logs after each test completes successfully
|
||||
|
||||
### 5. ✅ Better error and summary display
|
||||
- ✅ Collect all errors and display at end
|
||||
- ✅ Show timing metrics and performance summary (in verbose mode)
|
||||
- ✅ Highlight slowest tests (in verbose mode)
|
||||
- ✅ Add test failure context
|
||||
## Code Changes Needed
|
||||
1. Add log buffering to TapParser
|
||||
2. Update TsTestLogger to handle failed test logs
|
||||
3. Modify test result processing to show logs on failure
|
||||
|
||||
### 6. ✅ Browser console integration
|
||||
- ✅ Clearly separate browser logs from test output
|
||||
- ⏳ Add browser log filtering options (TODO)
|
||||
- ✅ Format browser errors specially
|
||||
|
||||
## Implementation Steps - COMPLETED
|
||||
|
||||
### Phase 1: ✅ Core Logger Implementation
|
||||
1. ✅ Created `tstest.logging.ts` with TsTestLogger class
|
||||
2. ✅ Added LogOptions interface for configuration
|
||||
3. ✅ Implemented basic formatting methods
|
||||
4. ✅ Added progress and summary methods
|
||||
|
||||
### Phase 2: ✅ Integration
|
||||
1. ✅ Updated CLI to accept new command-line options
|
||||
2. ✅ Modified TsTest class to use new logger
|
||||
3. ✅ Updated TapParser to use structured logging
|
||||
4. ✅ Updated TapCombinator for better summaries
|
||||
|
||||
### Phase 3: ✅ Visual Improvements
|
||||
1. ✅ Replaced all existing separators
|
||||
2. ✅ Updated color scheme
|
||||
3. ✅ Added emoji and Unicode characters
|
||||
4. ✅ Implemented hierarchical output
|
||||
|
||||
### Phase 4: ✅ Advanced Features
|
||||
1. ✅ Add JSON output format
|
||||
2. ⏳ Implement file-based logging (TODO)
|
||||
3. ✅ Add performance metrics collection
|
||||
4. ✅ Create error aggregation system
|
||||
|
||||
### Phase 5: ✅ Browser Integration
|
||||
1. ✅ Update browser console forwarding
|
||||
2. ✅ Add browser log formatting
|
||||
3. ✅ Implement browser-specific indicators
|
||||
|
||||
## Files modified
|
||||
- ✅ `ts/tstest.logging.ts` - New logger implementation (created)
|
||||
- ✅ `ts/index.ts` - Added CLI options parsing
|
||||
- ✅ `ts/tstest.classes.tstest.ts` - Integrated new logger
|
||||
- ✅ `ts/tstest.classes.tap.parser.ts` - Updated output formatting
|
||||
- ✅ `ts/tstest.classes.tap.combinator.ts` - Improved summary display
|
||||
- ❌ `ts/tstest.logprefixes.ts` - Still in use, can be deprecated later
|
||||
- ❌ `package.json` - No new dependencies needed
|
||||
|
||||
## Success Criteria - ACHIEVED
|
||||
- ✅ Cleaner, more readable test output
|
||||
- ✅ Configurable verbosity levels
|
||||
- ✅ Better CI/CD integration
|
||||
- ✅ Improved error visibility
|
||||
- ✅ Performance metrics available
|
||||
- ✅ Consistent visual design
|
||||
- ✅ Maintained backward compatibility
|
||||
|
||||
## Example Output Comparison
|
||||
|
||||
### Current Output
|
||||
```
|
||||
☰☰☰☰☰☰☰☰☰☰☰☰☰☰☰☰☰☰☰☰☰☰☰☰☰☰☰☰☰☰☰☰☰☰☰☰☰☰☰☰
|
||||
**TSTEST** FOUND 4 TESTFILE(S):
|
||||
**TSTEST** test/test.ts
|
||||
------------------------------------------------
|
||||
=> Running test/test.ts in node.js runtime.
|
||||
= = = = = = = = = = = = = = = = = = = = = = = =
|
||||
```
|
||||
|
||||
### Actual Output (IMPLEMENTED)
|
||||
```
|
||||
🔍 Test Discovery
|
||||
Mode: directory
|
||||
Pattern: test
|
||||
Found: 4 test file(s)
|
||||
|
||||
▶️ test/test.ts (1/4)
|
||||
Runtime: node.js
|
||||
✅ prepare test (0ms)
|
||||
Summary: 1/1 PASSED
|
||||
|
||||
▶️ test/test.single.ts (2/4)
|
||||
Runtime: node.js
|
||||
✅ single file test execution (1ms)
|
||||
Summary: 1/1 PASSED
|
||||
|
||||
📊 Test Summary
|
||||
┌────────────────────────────────┐
|
||||
│ Total Files: 4 │
|
||||
│ Total Tests: 4 │
|
||||
│ Passed: 4 │
|
||||
│ Failed: 0 │
|
||||
│ Duration: 5739ms │
|
||||
└────────────────────────────────┘
|
||||
|
||||
ALL TESTS PASSED! 🎉
|
||||
```
|
||||
|
||||
### Additional Features Implemented
|
||||
|
||||
1. **Quiet Mode**:
|
||||
```
|
||||
Found 1 tests
|
||||
✅ single file test execution
|
||||
|
||||
Summary: 1/1 | 1210ms | PASSED
|
||||
```
|
||||
|
||||
2. **JSON Mode**:
|
||||
```json
|
||||
{"event":"discovery","count":1,"pattern":"test/test.single.ts","executionMode":"file"}
|
||||
{"event":"fileStart","filename":"test/test.single.ts","runtime":"node.js","index":1,"total":1}
|
||||
{"event":"testResult","testName":"single file test execution","passed":true,"duration":0}
|
||||
{"event":"summary","summary":{"totalFiles":1,"totalTests":1,"totalPassed":1,"totalFailed":0,"totalDuration":1223,"fileResults":[...]}}
|
||||
```
|
||||
|
||||
3. **Error Display**:
|
||||
```
|
||||
❌ Failed Tests:
|
||||
|
||||
test/test.fail.ts
|
||||
❌ This test should fail
|
||||
|
||||
SOME TESTS FAILED! ❌
|
||||
```
|
||||
|
||||
## Summary of Implementation
|
||||
|
||||
The logging improvement plan has been successfully implemented with the following achievements:
|
||||
|
||||
1. **Created a new centralized TsTestLogger class** that handles all output formatting
|
||||
2. **Added multiple output modes**: quiet, normal, verbose, and JSON
|
||||
3. **Improved visual design** with modern Unicode characters and emojis
|
||||
4. **Added CLI argument parsing** for all new options
|
||||
5. **Integrated the logger throughout the codebase** (TsTest, TapParser, TapCombinator)
|
||||
6. **Better error aggregation and display** with failed tests shown at the end
|
||||
7. **Performance metrics** available in verbose mode
|
||||
8. **Clean, hierarchical output** that's much more readable
|
||||
|
||||
### Remaining TODOs
|
||||
|
||||
Only a few minor features remain unimplemented:
|
||||
- File-based logging (--log-file option)
|
||||
- Progress bar visualization
|
||||
- ETA for long test suites
|
||||
- Browser log filtering options
|
||||
|
||||
The core logging improvements are complete and provide a much better user experience!
|
||||
## Files to Modify
|
||||
- `ts/tstest.classes.tap.parser.ts` - Add log buffering
|
||||
- `ts/tstest.logging.ts` - Add failed test log handling
|
||||
- `ts/tstest.classes.tap.testresult.ts` - May need to store logs
|
11
test/test.console.ts
Normal file
11
test/test.console.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { expect, tap } from '@push.rocks/tapbundle';
|
||||
|
||||
tap.test('Test with console output', async () => {
|
||||
console.log('Log message 1 from test');
|
||||
console.log('Log message 2 from test');
|
||||
console.error('Error message from test');
|
||||
console.warn('Warning message from test');
|
||||
expect(true).toBeTrue();
|
||||
});
|
||||
|
||||
tap.start();
|
23
test/test.failing-with-logs.ts
Normal file
23
test/test.failing-with-logs.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import { expect, tap } from '@push.rocks/tapbundle';
|
||||
|
||||
tap.test('Test that will fail with console logs', async () => {
|
||||
console.log('Starting the test...');
|
||||
console.log('Doing some setup work');
|
||||
console.log('About to check assertion');
|
||||
|
||||
const value = 42;
|
||||
console.log(`The value is: ${value}`);
|
||||
|
||||
// This will fail
|
||||
expect(value).toEqual(100);
|
||||
|
||||
console.log('This log will not be reached');
|
||||
});
|
||||
|
||||
tap.test('Test that passes', async () => {
|
||||
console.log('This test passes');
|
||||
console.log('So these logs should not show in default mode');
|
||||
expect(true).toBeTrue();
|
||||
});
|
||||
|
||||
tap.start();
|
@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@git.zone/tstest',
|
||||
version: '1.2.0',
|
||||
version: '1.4.0',
|
||||
description: 'a test utility to run tests that match test/**/*.ts'
|
||||
}
|
||||
|
@ -33,9 +33,8 @@ export const runCli = async () => {
|
||||
logOptions.json = true;
|
||||
break;
|
||||
case '--log-file':
|
||||
if (i + 1 < args.length) {
|
||||
logOptions.logFile = args[++i];
|
||||
}
|
||||
case '--logfile':
|
||||
logOptions.logFile = true; // Set this as a flag, not a value
|
||||
break;
|
||||
default:
|
||||
if (!arg.startsWith('-')) {
|
||||
@ -52,7 +51,7 @@ export const runCli = async () => {
|
||||
console.error(' --verbose, -v Verbose output');
|
||||
console.error(' --no-color Disable colored output');
|
||||
console.error(' --json Output results as JSON');
|
||||
console.error(' --log-file Write logs to file');
|
||||
console.error(' --logfile Write logs to .nogit/testlogs/[testfile].log');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
|
@ -18,6 +18,8 @@ export class TapParser {
|
||||
|
||||
testStatusRegex = /(ok|not\sok)\s([0-9]+)\s-\s(.*)\s#\stime=(.*)ms$/;
|
||||
activeTapTestResult: TapTestResult;
|
||||
collectingErrorDetails: boolean = false;
|
||||
currentTestError: string[] = [];
|
||||
|
||||
pretaskRegex = /^::__PRETASK:(.*)$/;
|
||||
|
||||
@ -91,6 +93,9 @@ export class TapParser {
|
||||
this.logger.testResult(testSubject, true, testDuration);
|
||||
}
|
||||
} else {
|
||||
// Start collecting error details for failed test
|
||||
this.collectingErrorDetails = true;
|
||||
this.currentTestError = [];
|
||||
if (this.logger) {
|
||||
this.logger.testResult(testSubject, false, testDuration);
|
||||
}
|
||||
@ -101,12 +106,43 @@ export class TapParser {
|
||||
if (this.activeTapTestResult) {
|
||||
this.activeTapTestResult.addLogLine(logLine);
|
||||
}
|
||||
if (this.logger) {
|
||||
this.logger.tapOutput(logLine);
|
||||
|
||||
// Check if we're collecting error details
|
||||
if (this.collectingErrorDetails) {
|
||||
// Check if this line is an error detail (starts with Error: or has stack trace characteristics)
|
||||
if (logLine.trim().startsWith('Error:') || logLine.trim().match(/^\s*at\s/)) {
|
||||
this.currentTestError.push(logLine);
|
||||
} else if (this.currentTestError.length > 0) {
|
||||
// End of error details, show the error
|
||||
const errorMessage = this.currentTestError.join('\n');
|
||||
if (this.logger) {
|
||||
this.logger.testErrorDetails(errorMessage);
|
||||
}
|
||||
this.collectingErrorDetails = false;
|
||||
this.currentTestError = [];
|
||||
}
|
||||
}
|
||||
|
||||
// Don't output TAP error details as console output when we're collecting them
|
||||
if (!this.collectingErrorDetails || (!logLine.trim().startsWith('Error:') && !logLine.trim().match(/^\s*at\s/))) {
|
||||
if (this.logger) {
|
||||
// This is console output from the test file, not TAP protocol
|
||||
this.logger.testConsoleOutput(logLine);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (this.activeTapTestResult && this.activeTapTestResult.testSettled) {
|
||||
// Ensure any pending error is shown before settling the test
|
||||
if (this.collectingErrorDetails && this.currentTestError.length > 0) {
|
||||
const errorMessage = this.currentTestError.join('\n');
|
||||
if (this.logger) {
|
||||
this.logger.testErrorDetails(errorMessage);
|
||||
}
|
||||
this.collectingErrorDetails = false;
|
||||
this.currentTestError = [];
|
||||
}
|
||||
|
||||
this.testStore.push(this.activeTapTestResult);
|
||||
this._getNewTapTestResult();
|
||||
}
|
||||
|
@ -1,12 +1,14 @@
|
||||
import { coloredString as cs } from '@push.rocks/consolecolor';
|
||||
import * as plugins from './tstest.plugins.js';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
|
||||
export interface LogOptions {
|
||||
quiet?: boolean;
|
||||
verbose?: boolean;
|
||||
noColor?: boolean;
|
||||
json?: boolean;
|
||||
logFile?: string;
|
||||
logFile?: boolean;
|
||||
}
|
||||
|
||||
export interface TestFileResult {
|
||||
@ -37,6 +39,9 @@ export class TsTestLogger {
|
||||
private startTime: number;
|
||||
private fileResults: TestFileResult[] = [];
|
||||
private currentFileResult: TestFileResult | null = null;
|
||||
private currentTestLogFile: string | null = null;
|
||||
private currentTestLogs: string[] = []; // Buffer for current test logs
|
||||
private currentTestFailed: boolean = false;
|
||||
|
||||
constructor(options: LogOptions = {}) {
|
||||
this.options = options;
|
||||
@ -51,11 +56,44 @@ export class TsTestLogger {
|
||||
}
|
||||
|
||||
private log(message: string) {
|
||||
if (this.options.json) return;
|
||||
if (this.options.json) {
|
||||
// For JSON mode, skip console output
|
||||
// JSON output is handled by logJson method
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(message);
|
||||
|
||||
if (this.options.logFile) {
|
||||
// TODO: Implement file logging
|
||||
// Log to the current test file log if we're in a test and --logfile is specified
|
||||
if (this.currentTestLogFile) {
|
||||
this.logToTestFile(message);
|
||||
}
|
||||
}
|
||||
|
||||
private logToFile(message: string) {
|
||||
// This method is no longer used since we use logToTestFile for individual test logs
|
||||
// Keeping it for potential future use with a global log file
|
||||
}
|
||||
|
||||
private logToTestFile(message: string) {
|
||||
try {
|
||||
// Remove ANSI color codes for file logging
|
||||
const cleanMessage = message.replace(/\u001b\[[0-9;]*m/g, '');
|
||||
|
||||
// Append to test log file
|
||||
fs.appendFileSync(this.currentTestLogFile, cleanMessage + '\n');
|
||||
} catch (error) {
|
||||
// Silently fail to avoid disrupting the test run
|
||||
}
|
||||
}
|
||||
|
||||
private logJson(data: any) {
|
||||
const jsonString = JSON.stringify(data);
|
||||
console.log(jsonString);
|
||||
|
||||
// Also log to test file if --logfile is specified
|
||||
if (this.currentTestLogFile) {
|
||||
this.logToTestFile(jsonString);
|
||||
}
|
||||
}
|
||||
|
||||
@ -84,7 +122,7 @@ export class TsTestLogger {
|
||||
// Test discovery
|
||||
testDiscovery(count: number, pattern: string, executionMode: string) {
|
||||
if (this.options.json) {
|
||||
console.log(JSON.stringify({ event: 'discovery', count, pattern, executionMode }));
|
||||
this.logJson({ event: 'discovery', count, pattern, executionMode });
|
||||
return;
|
||||
}
|
||||
|
||||
@ -109,8 +147,27 @@ export class TsTestLogger {
|
||||
tests: []
|
||||
};
|
||||
|
||||
// Reset test-specific state
|
||||
this.currentTestLogs = [];
|
||||
this.currentTestFailed = false;
|
||||
|
||||
// Only set up test log file if --logfile option is specified
|
||||
if (this.options.logFile) {
|
||||
const baseFilename = path.basename(filename, '.ts');
|
||||
this.currentTestLogFile = path.join('.nogit', 'testlogs', `${baseFilename}.log`);
|
||||
|
||||
// Ensure the directory exists
|
||||
const logDir = path.dirname(this.currentTestLogFile);
|
||||
if (!fs.existsSync(logDir)) {
|
||||
fs.mkdirSync(logDir, { recursive: true });
|
||||
}
|
||||
|
||||
// Clear the log file for this test
|
||||
fs.writeFileSync(this.currentTestLogFile, '');
|
||||
}
|
||||
|
||||
if (this.options.json) {
|
||||
console.log(JSON.stringify({ event: 'fileStart', filename, runtime, index, total }));
|
||||
this.logJson({ event: 'fileStart', filename, runtime, index, total });
|
||||
return;
|
||||
}
|
||||
|
||||
@ -128,15 +185,24 @@ export class TsTestLogger {
|
||||
this.currentFileResult.passed++;
|
||||
} else {
|
||||
this.currentFileResult.failed++;
|
||||
this.currentTestFailed = true;
|
||||
}
|
||||
this.currentFileResult.duration += duration;
|
||||
}
|
||||
|
||||
if (this.options.json) {
|
||||
console.log(JSON.stringify({ event: 'testResult', testName, passed, duration, error }));
|
||||
this.logJson({ event: 'testResult', testName, passed, duration, error });
|
||||
return;
|
||||
}
|
||||
|
||||
// If test failed and we have buffered logs, show them now
|
||||
if (!passed && this.currentTestLogs.length > 0 && !this.options.verbose) {
|
||||
this.log(this.format(' 📋 Console output from failed test:', 'yellow'));
|
||||
this.currentTestLogs.forEach(logMessage => {
|
||||
this.log(this.format(` ${logMessage}`, 'dim'));
|
||||
});
|
||||
}
|
||||
|
||||
const icon = passed ? '✅' : '❌';
|
||||
const color = passed ? 'green' : 'red';
|
||||
|
||||
@ -148,6 +214,9 @@ export class TsTestLogger {
|
||||
this.log(this.format(` ${error}`, 'red'));
|
||||
}
|
||||
}
|
||||
|
||||
// Clear logs after each test
|
||||
this.currentTestLogs = [];
|
||||
}
|
||||
|
||||
testFileEnd(passed: number, failed: number, duration: number) {
|
||||
@ -157,7 +226,7 @@ export class TsTestLogger {
|
||||
}
|
||||
|
||||
if (this.options.json) {
|
||||
console.log(JSON.stringify({ event: 'fileEnd', passed, failed, duration }));
|
||||
this.logJson({ event: 'fileEnd', passed, failed, duration });
|
||||
return;
|
||||
}
|
||||
|
||||
@ -167,23 +236,48 @@ export class TsTestLogger {
|
||||
const color = failed === 0 ? 'green' : 'red';
|
||||
this.log(this.format(` Summary: ${passed}/${total} ${status}`, color));
|
||||
}
|
||||
|
||||
// Clear the current test log file reference only if using --logfile
|
||||
if (this.options.logFile) {
|
||||
this.currentTestLogFile = null;
|
||||
}
|
||||
}
|
||||
|
||||
// TAP output forwarding
|
||||
// TAP output forwarding (for TAP protocol messages)
|
||||
tapOutput(message: string, isError: boolean = false) {
|
||||
if (this.options.json) return;
|
||||
|
||||
if (this.options.verbose || isError) {
|
||||
const prefix = isError ? ' ⚠️ ' : ' ';
|
||||
const color = isError ? 'red' : 'dim';
|
||||
this.log(this.format(`${prefix}${message}`, color));
|
||||
// Never show raw TAP protocol messages in console
|
||||
// They are already processed by TapParser and shown in our format
|
||||
|
||||
// Always log to test file if --logfile is specified
|
||||
if (this.currentTestLogFile) {
|
||||
this.logToTestFile(` ${message}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Console output from test files (non-TAP output)
|
||||
testConsoleOutput(message: string) {
|
||||
if (this.options.json) return;
|
||||
|
||||
// In verbose mode, show console output immediately
|
||||
if (this.options.verbose) {
|
||||
this.log(this.format(` ${message}`, 'dim'));
|
||||
} else {
|
||||
// In non-verbose mode, buffer the logs
|
||||
this.currentTestLogs.push(message);
|
||||
}
|
||||
|
||||
// Always log to test file if --logfile is specified
|
||||
if (this.currentTestLogFile) {
|
||||
this.logToTestFile(` ${message}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Browser console
|
||||
browserConsole(message: string, level: string = 'log') {
|
||||
if (this.options.json) {
|
||||
console.log(JSON.stringify({ event: 'browserConsole', message, level }));
|
||||
this.logJson({ event: 'browserConsole', message, level });
|
||||
return;
|
||||
}
|
||||
|
||||
@ -194,6 +288,26 @@ export class TsTestLogger {
|
||||
}
|
||||
}
|
||||
|
||||
// Test error details display
|
||||
testErrorDetails(errorMessage: string) {
|
||||
if (this.options.json) {
|
||||
this.logJson({ event: 'testError', error: errorMessage });
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.options.quiet) {
|
||||
this.log(this.format(' Error details:', 'red'));
|
||||
errorMessage.split('\n').forEach(line => {
|
||||
this.log(this.format(` ${line}`, 'red'));
|
||||
});
|
||||
}
|
||||
|
||||
// Always log to test file if --logfile is specified
|
||||
if (this.currentTestLogFile) {
|
||||
this.logToTestFile(` Error: ${errorMessage}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Final summary
|
||||
summary() {
|
||||
const totalDuration = Date.now() - this.startTime;
|
||||
@ -207,7 +321,7 @@ export class TsTestLogger {
|
||||
};
|
||||
|
||||
if (this.options.json) {
|
||||
console.log(JSON.stringify({ event: 'summary', summary }));
|
||||
this.logJson({ event: 'summary', summary });
|
||||
return;
|
||||
}
|
||||
|
||||
@ -266,7 +380,7 @@ export class TsTestLogger {
|
||||
// Error display
|
||||
error(message: string, file?: string, stack?: string) {
|
||||
if (this.options.json) {
|
||||
console.log(JSON.stringify({ event: 'error', message, file, stack }));
|
||||
this.logJson({ event: 'error', message, file, stack });
|
||||
return;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user