tstest/test/tapbundle/test.toolsarg.ts

85 lines
2.6 KiB
TypeScript

import { tap, expect } from '../../ts_tapbundle/index.js';
// Test skip functionality
tap.test('should skip a test with skip()', async (toolsArg) => {
toolsArg.skip('This test is skipped');
// This code should not run
expect(false).toBeTrue();
});
tap.test('should conditionally skip with skipIf()', async (toolsArg) => {
const shouldSkip = true;
toolsArg.skipIf(shouldSkip, 'Condition met, skipping');
// This code should not run
expect(false).toBeTrue();
});
tap.test('should not skip when skipIf condition is false', async (toolsArg) => {
const shouldSkip = false;
toolsArg.skipIf(shouldSkip, 'Should not skip');
// This code should run
expect(true).toBeTrue();
});
// Test todo functionality
tap.test('should mark test as todo', async (toolsArg) => {
toolsArg.todo('Not implemented yet');
// Test code that would be implemented later
expect(true).toBeTrue();
});
// Test timeout functionality
tap.test('should set custom timeout', async (toolsArg) => {
toolsArg.timeout(5000);
// Simulate a task that takes 100ms
await toolsArg.delayFor(100);
expect(true).toBeTrue();
});
// This test is expected to fail due to timeout
tap.test('should timeout when exceeding limit', async (toolsArg) => {
toolsArg.timeout(100);
// This test will timeout and be marked as failed by the test runner
await toolsArg.delayFor(2000);
// This line should not be reached due to timeout
});
tap.test('timeout should work properly', async (toolsArg) => {
toolsArg.timeout(200);
// This test should complete successfully within the timeout
await toolsArg.delayFor(50);
expect(true).toBeTrue();
});
// Test retry functionality
tap.retry(3)
.test('should retry on failure', async (toolsArg) => {
// Use retry count to determine success
const currentRetry = toolsArg.retryCount;
// Fail on first two attempts (0 and 1), succeed on third (2)
if (currentRetry < 2) {
throw new Error(`Attempt ${currentRetry + 1} failed`);
}
expect(currentRetry).toEqual(2);
});
tap.test('should expose retry count', async (toolsArg) => {
toolsArg.retry(2);
// The retry count should be available
expect(toolsArg.retryCount).toBeLessThanOrEqual(2);
expect(true).toBeTrue();
});
// Test allowFailure
tap.test('should allow failure', async (toolsArg) => {
// Just verify that allowFailure() can be called without throwing
toolsArg.allowFailure();
expect(true).toBeTrue();
// Note: In a real implementation, we would see "please note: failure allowed!"
// in the output when this test fails, but the test itself will still be marked as failed
});
tap.start();