feat(core): Implement Protocol V2 with enhanced settings and lifecycle hooks
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import * as plugins from './tapbundle.plugins.js';
|
||||
import { tapCreator } from './tapbundle.tapcreator.js';
|
||||
import { TapTools, SkipError } from './tapbundle.classes.taptools.js';
|
||||
import { ProtocolEmitter, type ITestEvent } from '../dist_ts_tapbundle_protocol/index.js';
|
||||
import { setProtocolEmitter } from './tapbundle.expect.wrapper.js';
|
||||
|
||||
// imported interfaces
|
||||
import { Deferred } from '@push.rocks/smartpromise';
|
||||
@@ -32,6 +34,7 @@ export class TapTest<T = unknown> {
|
||||
public testPromise: Promise<TapTest<T>> = this.testDeferred.promise;
|
||||
private testResultDeferred: Deferred<T> = plugins.smartpromise.defer();
|
||||
public testResultPromise: Promise<T> = this.testResultDeferred.promise;
|
||||
private protocolEmitter = new ProtocolEmitter();
|
||||
/**
|
||||
* constructor
|
||||
*/
|
||||
@@ -48,6 +51,13 @@ export class TapTest<T = unknown> {
|
||||
this.testFunction = optionsArg.testFunction;
|
||||
}
|
||||
|
||||
/**
|
||||
* Emit an event
|
||||
*/
|
||||
private emitEvent(event: ITestEvent) {
|
||||
console.log(this.protocolEmitter.emitEvent(event));
|
||||
}
|
||||
|
||||
/**
|
||||
* run the test
|
||||
*/
|
||||
@@ -55,11 +65,74 @@ export class TapTest<T = unknown> {
|
||||
this.testKey = testKeyArg;
|
||||
const testNumber = testKeyArg + 1;
|
||||
|
||||
// Emit test:queued event
|
||||
this.emitEvent({
|
||||
eventType: 'test:queued',
|
||||
timestamp: Date.now(),
|
||||
data: {
|
||||
testNumber,
|
||||
description: this.description
|
||||
}
|
||||
});
|
||||
|
||||
// Handle todo tests
|
||||
if (this.isTodo) {
|
||||
const todoText = this.todoReason ? `# TODO ${this.todoReason}` : '# TODO';
|
||||
console.log(`ok ${testNumber} - ${this.description} ${todoText}`);
|
||||
const testResult = {
|
||||
ok: true,
|
||||
testNumber,
|
||||
description: this.description,
|
||||
directive: {
|
||||
type: 'todo' as const,
|
||||
reason: this.todoReason
|
||||
}
|
||||
};
|
||||
const lines = this.protocolEmitter.emitTest(testResult);
|
||||
lines.forEach((line: string) => console.log(line));
|
||||
this.status = 'success';
|
||||
|
||||
// Emit test:completed event for todo test
|
||||
this.emitEvent({
|
||||
eventType: 'test:completed',
|
||||
timestamp: Date.now(),
|
||||
data: {
|
||||
testNumber,
|
||||
description: this.description,
|
||||
duration: 0,
|
||||
error: undefined
|
||||
}
|
||||
});
|
||||
|
||||
this.testDeferred.resolve(this);
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle pre-marked skip tests
|
||||
if (this.tapTools.isSkipped) {
|
||||
const testResult = {
|
||||
ok: true,
|
||||
testNumber,
|
||||
description: this.description,
|
||||
directive: {
|
||||
type: 'skip' as const,
|
||||
reason: this.tapTools.skipReason || 'Marked as skip'
|
||||
}
|
||||
};
|
||||
const lines = this.protocolEmitter.emitTest(testResult);
|
||||
lines.forEach((line: string) => console.log(line));
|
||||
this.status = 'skipped';
|
||||
|
||||
// Emit test:completed event for skipped test
|
||||
this.emitEvent({
|
||||
eventType: 'test:completed',
|
||||
timestamp: Date.now(),
|
||||
data: {
|
||||
testNumber,
|
||||
description: this.description,
|
||||
duration: 0,
|
||||
error: undefined
|
||||
}
|
||||
});
|
||||
|
||||
this.testDeferred.resolve(this);
|
||||
return;
|
||||
}
|
||||
@@ -71,6 +144,20 @@ export class TapTest<T = unknown> {
|
||||
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
||||
this.hrtMeasurement.start();
|
||||
|
||||
// Emit test:started event
|
||||
this.emitEvent({
|
||||
eventType: 'test:started',
|
||||
timestamp: Date.now(),
|
||||
data: {
|
||||
testNumber,
|
||||
description: this.description,
|
||||
retry: attempt > 0 ? attempt : undefined
|
||||
}
|
||||
});
|
||||
|
||||
// Set protocol emitter for enhanced expect
|
||||
setProtocolEmitter(this.protocolEmitter);
|
||||
|
||||
try {
|
||||
// Set up timeout if specified
|
||||
let timeoutHandle: any;
|
||||
@@ -97,10 +184,32 @@ export class TapTest<T = unknown> {
|
||||
}
|
||||
|
||||
this.hrtMeasurement.stop();
|
||||
console.log(
|
||||
`ok ${testNumber} - ${this.description} # time=${this.hrtMeasurement.milliSeconds}ms`,
|
||||
);
|
||||
const testResult = {
|
||||
ok: true,
|
||||
testNumber,
|
||||
description: this.description,
|
||||
metadata: {
|
||||
time: this.hrtMeasurement.milliSeconds,
|
||||
tags: this.tags.length > 0 ? this.tags : undefined,
|
||||
file: this.fileName
|
||||
}
|
||||
};
|
||||
const lines = this.protocolEmitter.emitTest(testResult);
|
||||
lines.forEach((line: string) => console.log(line));
|
||||
this.status = 'success';
|
||||
|
||||
// Emit test:completed event
|
||||
this.emitEvent({
|
||||
eventType: 'test:completed',
|
||||
timestamp: Date.now(),
|
||||
data: {
|
||||
testNumber,
|
||||
description: this.description,
|
||||
duration: this.hrtMeasurement.milliSeconds,
|
||||
error: undefined
|
||||
}
|
||||
});
|
||||
|
||||
this.testDeferred.resolve(this);
|
||||
this.testResultDeferred.resolve(testReturnValue);
|
||||
return; // Success, exit retry loop
|
||||
@@ -110,8 +219,31 @@ export class TapTest<T = unknown> {
|
||||
|
||||
// Handle skip
|
||||
if (err instanceof SkipError || err.name === 'SkipError') {
|
||||
console.log(`ok ${testNumber} - ${this.description} # SKIP ${err.message.replace('Skipped: ', '')}`);
|
||||
const testResult = {
|
||||
ok: true,
|
||||
testNumber,
|
||||
description: this.description,
|
||||
directive: {
|
||||
type: 'skip' as const,
|
||||
reason: err.message.replace('Skipped: ', '')
|
||||
}
|
||||
};
|
||||
const lines = this.protocolEmitter.emitTest(testResult);
|
||||
lines.forEach((line: string) => console.log(line));
|
||||
this.status = 'skipped';
|
||||
|
||||
// Emit test:completed event for skipped test
|
||||
this.emitEvent({
|
||||
eventType: 'test:completed',
|
||||
timestamp: Date.now(),
|
||||
data: {
|
||||
testNumber,
|
||||
description: this.description,
|
||||
duration: this.hrtMeasurement.milliSeconds,
|
||||
error: undefined
|
||||
}
|
||||
});
|
||||
|
||||
this.testDeferred.resolve(this);
|
||||
return;
|
||||
}
|
||||
@@ -120,17 +252,48 @@ export class TapTest<T = unknown> {
|
||||
|
||||
// If we have retries left, try again
|
||||
if (attempt < maxRetries) {
|
||||
console.log(
|
||||
`# Retry ${attempt + 1}/${maxRetries} for test: ${this.description}`,
|
||||
);
|
||||
console.log(this.protocolEmitter.emitComment(`Retry ${attempt + 1}/${maxRetries} for test: ${this.description}`));
|
||||
this.tapTools._incrementRetryCount();
|
||||
continue;
|
||||
}
|
||||
|
||||
// Final failure
|
||||
console.log(
|
||||
`not ok ${testNumber} - ${this.description} # time=${this.hrtMeasurement.milliSeconds}ms`,
|
||||
);
|
||||
const testResult = {
|
||||
ok: false,
|
||||
testNumber,
|
||||
description: this.description,
|
||||
metadata: {
|
||||
time: this.hrtMeasurement.milliSeconds,
|
||||
retry: this.tapTools.retryCount,
|
||||
maxRetries: maxRetries > 0 ? maxRetries : undefined,
|
||||
error: {
|
||||
message: lastError.message || String(lastError),
|
||||
stack: lastError.stack,
|
||||
code: lastError.code
|
||||
},
|
||||
tags: this.tags.length > 0 ? this.tags : undefined,
|
||||
file: this.fileName
|
||||
}
|
||||
};
|
||||
const lines = this.protocolEmitter.emitTest(testResult);
|
||||
lines.forEach((line: string) => console.log(line));
|
||||
|
||||
// Emit test:completed event for failed test
|
||||
this.emitEvent({
|
||||
eventType: 'test:completed',
|
||||
timestamp: Date.now(),
|
||||
data: {
|
||||
testNumber,
|
||||
description: this.description,
|
||||
duration: this.hrtMeasurement.milliSeconds,
|
||||
error: {
|
||||
message: lastError.message || String(lastError),
|
||||
stack: lastError.stack,
|
||||
type: 'runtime' as const
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
this.testDeferred.resolve(this);
|
||||
this.testResultDeferred.resolve(err);
|
||||
|
||||
|
Reference in New Issue
Block a user