tstest/test/tapbundle/test.describe.ts

101 lines
3.2 KiB
TypeScript

import { tap, expect } from '../../ts_tapbundle/index.js';
// Global state for testing lifecycle hooks
const lifecycleOrder: string[] = [];
tap.describe('Test Suite A', () => {
tap.beforeEach(async (toolsArg) => {
lifecycleOrder.push('Suite A - beforeEach');
});
tap.afterEach(async (toolsArg) => {
lifecycleOrder.push('Suite A - afterEach');
});
tap.test('test 1 in suite A', async (toolsArg) => {
lifecycleOrder.push('Test 1');
expect(true).toBeTrue();
});
tap.test('test 2 in suite A', async (toolsArg) => {
lifecycleOrder.push('Test 2');
expect(true).toBeTrue();
});
tap.describe('Nested Suite B', () => {
tap.beforeEach(async (toolsArg) => {
lifecycleOrder.push('Suite B - beforeEach');
});
tap.afterEach(async (toolsArg) => {
lifecycleOrder.push('Suite B - afterEach');
});
tap.test('test 1 in nested suite B', async (toolsArg) => {
lifecycleOrder.push('Nested Test 1');
expect(true).toBeTrue();
});
});
});
// Test outside any suite
tap.test('test outside suites', async (toolsArg) => {
lifecycleOrder.push('Outside Test');
expect(true).toBeTrue();
});
tap.describe('Test Suite with errors', () => {
tap.beforeEach(async (toolsArg) => {
// Setup that might fail
const data = await Promise.resolve({ value: 42 });
toolsArg.testData = data;
});
tap.test('test with error', async (toolsArg) => {
// Verify that data from beforeEach is available
expect(toolsArg.testData).toBeDefined();
expect(toolsArg.testData.value).toEqual(42);
// Test that error handling works by catching an error
try {
throw new Error('Intentional error');
} catch (error) {
expect(error.message).toEqual('Intentional error');
}
});
tap.test('test with skip in suite', async (toolsArg) => {
toolsArg.skip('Skipping this test in a suite');
expect(false).toBeTrue();
});
});
// Verify lifecycle order - this test runs last to check if all hooks were called properly
tap.test('verify lifecycle hook order', async (toolsArg) => {
// Wait a bit to ensure all tests have completed
await new Promise(resolve => setTimeout(resolve, 50));
console.log('Lifecycle order:', lifecycleOrder);
// Check that the tests we expect to have run actually did
expect(lifecycleOrder).toContain('Test 1');
expect(lifecycleOrder).toContain('Test 2');
expect(lifecycleOrder).toContain('Nested Test 1');
// Check that beforeEach was called before each test in Suite A
const test1Index = lifecycleOrder.indexOf('Test 1');
expect(test1Index).toBeGreaterThan(-1);
const beforeTest1 = lifecycleOrder.slice(0, test1Index);
expect(beforeTest1).toContain('Suite A - beforeEach');
// Check that afterEach was called after test 1
const afterTest1 = lifecycleOrder.slice(test1Index + 1);
expect(afterTest1).toContain('Suite A - afterEach');
// Check nested suite lifecycle
const nestedTest1Index = lifecycleOrder.indexOf('Nested Test 1');
expect(nestedTest1Index).toBeGreaterThan(-1);
const beforeNestedTest1 = lifecycleOrder.slice(0, nestedTest1Index);
expect(beforeNestedTest1).toContain('Suite B - beforeEach');
});
tap.start();