Skip to content

Commit

Permalink
feat: add test name as optional
Browse files Browse the repository at this point in the history
  • Loading branch information
m2rads committed Dec 16, 2024
1 parent 4b82575 commit 810a86a
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 39 deletions.
2 changes: 2 additions & 0 deletions packages/shortest/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ declare module '@antiwork/shortest' {
};

export type TestChain = {
expect(fn: (context: TestContextProps) => Promise<void>): TestChain;
expect(description: string): TestChain;
expect(description: string, fn?: (context: TestContextProps) => Promise<void>): TestChain;
expect(description: string, payload?: any, fn?: (context: TestContextProps) => Promise<void>): TestChain;
};

export type TestAPI = {
(fn: (context: TestContextProps) => Promise<void>): void;
(name: string): TestChain;
(name: string, fn?: (context: TestContextProps) => Promise<void>): TestChain;
(name: string, payload?: any, fn?: (context: TestContextProps) => Promise<void>): TestChain;
Expand Down
14 changes: 14 additions & 0 deletions packages/shortest/src/core/runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,20 @@ export class TestRunner {
}

private async executeTest(test: TestFunction, context: BrowserContext) {
// If it's direct execution, skip AI
if (test.directExecution) {
try {
const testContext = await this.createTestContext(context);
await test.fn?.(testContext);
return { result: 'pass' as const, reason: 'Direct execution successful' };
} catch (error) {
return {
result: 'fail' as const,
reason: error instanceof Error ? error.message : 'Direct execution failed'
};
}
}

// Use the shared context
const testContext = await this.createTestContext(context);
const browserTool = new BrowserTool(testContext.page, this.browserManager, {
Expand Down
70 changes: 43 additions & 27 deletions packages/shortest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,45 +108,61 @@ export function getConfig(): ShortestConfig {
return globalConfig;
}

// New Test API Implementation
function createTestChain(
name: string,
nameOrFn: string | ((context: TestContext) => Promise<void>),
payloadOrFn?: ((context: TestContext) => Promise<void>) | any,
fn?: (context: TestContext) => Promise<void>
): TestChain {
// If second argument is a function, it's the callback
if (typeof payloadOrFn === 'function') {
fn = payloadOrFn;
payloadOrFn = undefined;
const registry = global.__shortest__.registry;

// Handle direct execution
if (typeof nameOrFn === 'function') {
const test: TestFunction = {
directExecution: true,
fn: nameOrFn
};
registry.currentFileTests.push(test);
// Return empty chain for type compatibility
return {
expect: () => {
throw new Error('expect() cannot be called on direct execution test');
}
};
}

// Rest of existing createTestChain implementation...
const test: TestFunction = {
name,
payload: payloadOrFn,
fn,
name: nameOrFn,
payload: typeof payloadOrFn === 'function' ? undefined : payloadOrFn,
fn: typeof payloadOrFn === 'function' ? payloadOrFn : fn,
expectations: []
};

global.__shortest__.registry.tests.set(name,
[...(global.__shortest__.registry.tests.get(name) || []), test]
);

global.__shortest__.registry.currentFileTests.push(test);
registry.tests.set(nameOrFn, [...(registry.tests.get(nameOrFn) || []), test]);
registry.currentFileTests.push(test);

const chain: TestChain = {
expect(description: string, payloadOrFn?: any, fn?: (context: TestContext) => Promise<void>) {
test.expectations = test.expectations || [];

// Handle different overloads
if (typeof payloadOrFn === 'function') {
fn = payloadOrFn;
payloadOrFn = undefined;
expect(
descriptionOrFn: string | ((context: TestContext) => Promise<void>),
payloadOrFn?: any,
fn?: (context: TestContext) => Promise<void>
) {
// Handle direct execution for expect
if (typeof descriptionOrFn === 'function') {
test.expectations = test.expectations || [];
test.expectations.push({
directExecution: true,
fn: descriptionOrFn
});
return chain;
}


// Existing expect implementation...
test.expectations = test.expectations || [];
test.expectations.push({
description,
payload: payloadOrFn,
fn
description: descriptionOrFn,
payload: typeof payloadOrFn === 'function' ? undefined : payloadOrFn,
fn: typeof payloadOrFn === 'function' ? payloadOrFn : fn
});
return chain;
}
Expand All @@ -156,8 +172,8 @@ function createTestChain(
}

export const test: TestAPI = Object.assign(
(name: string, payload?: any, fn?: (context: TestContext) => Promise<void>) =>
createTestChain(name, payload, fn),
(nameOrFn: string | ((context: TestContext) => Promise<void>), payloadOrFn?: ((context: TestContext) => Promise<void>) | any, fn?: (context: TestContext) => Promise<void>) =>
createTestChain(nameOrFn, payloadOrFn, fn),
{
beforeAll: (nameOrFn: string | ((ctx: TestContext) => Promise<void>)) => {
const hook = typeof nameOrFn === 'function' ? nameOrFn : undefined;
Expand Down
12 changes: 8 additions & 4 deletions packages/shortest/src/types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,28 @@ export type TestContext = {

export type TestHookFunction = (context: TestContext) => Promise<void>;

export type TestFunction = {
name: string;
export interface TestFunction {
name?: string;
payload?: any;
fn?: (context: TestContext) => Promise<void>;
expectations?: {
description: string;
description?: string;
payload?: any;
fn?: (context: TestContext) => Promise<void>;
directExecution?: boolean;
}[];
};
directExecution?: boolean;
}

export type TestChain = {
expect(fn: (context: TestContext) => Promise<void>): TestChain;
expect(description: string): TestChain;
expect(description: string, fn?: (context: TestContext) => Promise<void>): TestChain;
expect(description: string, payload?: any, fn?: (context: TestContext) => Promise<void>): TestChain;
};

export type TestAPI = {
(fn: (context: TestContext) => Promise<void>): TestChain;
(name: string): TestChain;
(name: string, fn?: (context: TestContext) => Promise<void>): TestChain;
(name: string, payload?: any, fn?: (context: TestContext) => Promise<void>): TestChain;
Expand Down
16 changes: 8 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 810a86a

Please sign in to comment.