Files
tstest/test/tapbundle/test.new-lifecycle.ts

171 lines
5.4 KiB
TypeScript

import { tap, expect } from '../../ts_tapbundle/index.js';
// Global state for testing new lifecycle features
const executionOrder: string[] = [];
let postTaskRan = false;
// Test preTask and postTask
tap.preTask('setup environment', async () => {
executionOrder.push('preTask');
console.log('🔧 PreTask: Setting up environment');
});
tap.postTask('cleanup environment', async () => {
postTaskRan = true;
executionOrder.push('postTask');
console.log('🧹 PostTask: Cleaning up environment');
});
// Test suite-level beforeAll and afterAll
tap.describe('Suite with beforeAll/afterAll', () => {
tap.beforeAll(async () => {
executionOrder.push('suite-beforeAll');
console.log('🔰 Suite beforeAll executed');
});
tap.afterAll(async () => {
executionOrder.push('suite-afterAll');
console.log('🏁 Suite afterAll executed');
});
tap.beforeEach(async () => {
executionOrder.push('suite-beforeEach');
});
tap.afterEach(async () => {
executionOrder.push('suite-afterEach');
});
tap.test('first test in suite', async () => {
executionOrder.push('test-1');
expect(executionOrder).toContain('preTask');
expect(executionOrder).toContain('suite-beforeAll');
console.log('✓ Test 1 executed');
});
tap.test('second test in suite', async () => {
executionOrder.push('test-2');
expect(executionOrder).toContain('suite-beforeAll');
console.log('✓ Test 2 executed');
});
});
// Test nested suites with beforeAll/afterAll
tap.describe('Parent Suite', () => {
tap.beforeAll(async () => {
executionOrder.push('parent-beforeAll');
console.log('🔰 Parent beforeAll executed');
});
tap.afterAll(async () => {
executionOrder.push('parent-afterAll');
console.log('🏁 Parent afterAll executed');
});
tap.test('test in parent', async () => {
executionOrder.push('parent-test');
expect(executionOrder).toContain('parent-beforeAll');
});
tap.describe('Child Suite', () => {
tap.beforeAll(async () => {
executionOrder.push('child-beforeAll');
console.log('🔰 Child beforeAll executed');
});
tap.afterAll(async () => {
executionOrder.push('child-afterAll');
console.log('🏁 Child afterAll executed');
});
tap.test('test in child', async () => {
executionOrder.push('child-test');
expect(executionOrder).toContain('parent-beforeAll');
expect(executionOrder).toContain('child-beforeAll');
});
});
});
// Test parallel() fluent API
tap.parallel().test('parallel test 1', async () => {
await new Promise(resolve => setTimeout(resolve, 10));
executionOrder.push('parallel-1');
console.log('⚡ Parallel test 1 executed');
expect(true).toBeTrue();
});
tap.parallel().test('parallel test 2', async () => {
await new Promise(resolve => setTimeout(resolve, 5));
executionOrder.push('parallel-2');
console.log('⚡ Parallel test 2 executed');
expect(true).toBeTrue();
});
// Test parallel() with configuration
tap
.parallel()
.tags('integration', 'parallel')
.timeout(1000)
.test('configured parallel test', async () => {
executionOrder.push('parallel-configured');
console.log('⚡ Configured parallel test executed');
expect(true).toBeTrue();
});
// Verify execution order
tap.test('verify lifecycle execution order', async () => {
// Give a moment for any async operations to complete
await new Promise(resolve => setTimeout(resolve, 100));
console.log('📊 Execution order:', executionOrder);
// Verify preTask ran first
expect(executionOrder[0]).toEqual('preTask');
// Verify suite beforeAll ran before tests
const suiteBeforeAllIndex = executionOrder.indexOf('suite-beforeAll');
const test1Index = executionOrder.indexOf('test-1');
expect(suiteBeforeAllIndex).toBeLessThan(test1Index);
// Verify beforeEach ran before each test
const beforeEachIndices = executionOrder
.map((item, index) => item === 'suite-beforeEach' ? index : -1)
.filter(index => index !== -1);
expect(beforeEachIndices.length).toBeGreaterThanOrEqual(2);
// Verify afterEach ran after each test
const afterEachIndices = executionOrder
.map((item, index) => item === 'suite-afterEach' ? index : -1)
.filter(index => index !== -1);
expect(afterEachIndices.length).toBeGreaterThanOrEqual(2);
// Verify afterAll ran after all tests
const suiteAfterAllIndex = executionOrder.indexOf('suite-afterAll');
const test2Index = executionOrder.indexOf('test-2');
expect(suiteAfterAllIndex).toBeGreaterThan(test2Index);
// Verify nested suite lifecycle
expect(executionOrder).toContain('parent-beforeAll');
expect(executionOrder).toContain('parent-test');
expect(executionOrder).toContain('child-beforeAll');
expect(executionOrder).toContain('child-test');
expect(executionOrder).toContain('child-afterAll');
expect(executionOrder).toContain('parent-afterAll');
// Verify parallel tests ran
expect(executionOrder).toContain('parallel-1');
expect(executionOrder).toContain('parallel-2');
expect(executionOrder).toContain('parallel-configured');
console.log('✅ All lifecycle hooks executed in correct order');
});
// This test will verify postTask ran (after tap.start() completes)
tap.test('verify postTask execution', async () => {
// PostTask hasn't run yet because tests are still running
expect(postTaskRan).toBeFalse();
console.log('✓ Verified postTask will run after all tests');
});
export default tap.start();