Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add chained tests #136

Merged
merged 10 commits into from
Dec 31, 2024
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,30 @@ shortest.afterAll(async ({ page }) => {
});
```

### Chained Tests
crabest marked this conversation as resolved.
Show resolved Hide resolved

Shortest supports flexible test chaining patterns:

```typescript
// Sequential test chain
shortest([
"user can login with email and password",
"user can modify their account-level refund policy"
])

// Reusable test flows
const loginAsLawyer = "login as lawyer with valid credentials"
const loginAsContractor = "login as contractor with valid credentials"
const allAppActions = [
"send invoice to company",
"view invoices"
]

// Combine flows with spread operator
shortest([loginAsLawyer, ...allAppActions])
shortest([loginAsContractor, ...allAppActions])
```

### Running tests

```bash
Expand Down
25 changes: 23 additions & 2 deletions packages/shortest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,33 @@ export function getConfig(): ShortestConfig {
}

function createTestChain(
nameOrFn: string | ((context: TestContext) => Promise<void>),
nameOrFn: string | string[] | ((context: TestContext) => Promise<void>),
payloadOrFn?: ((context: TestContext) => Promise<void>) | any,
fn?: (context: TestContext) => Promise<void>,
): TestChain {
const registry = global.__shortest__.registry;

// Handle array of test names
if (Array.isArray(nameOrFn)) {
const tests = nameOrFn.map((name) => {
const test: TestFunction = {
name,
expectations: [],
};

registry.tests.set(name, [...(registry.tests.get(name) || []), test]);
registry.currentFileTests.push(test);
return test;
});

// Return chain for the last test
const lastTest = tests[tests.length - 1];
if (!lastTest.name) {
throw new Error("Test name is required");
}
return createTestChain(lastTest.name, payloadOrFn, fn);
}

// Handle direct execution
if (typeof nameOrFn === "function") {
registry.directTestCounter++;
Expand Down Expand Up @@ -182,7 +203,7 @@ function createTestChain(

export const test: TestAPI = Object.assign(
(
nameOrFn: string | ((context: TestContext) => Promise<void>),
nameOrFn: string | string[] | ((context: TestContext) => Promise<void>),
payloadOrFn?: ((context: TestContext) => Promise<void>) | any,
fn?: (context: TestContext) => Promise<void>,
) => createTestChain(nameOrFn, payloadOrFn, fn),
Expand Down
1 change: 1 addition & 0 deletions packages/shortest/src/types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export type TestChain = {
export type TestAPI = {
(fn: (context: TestContext) => Promise<void>): TestChain;
(name: string): TestChain;
(names: string[]): TestChain;
(name: string, fn?: (context: TestContext) => Promise<void>): TestChain;
(
name: string,
Expand Down
8 changes: 8 additions & 0 deletions packages/shortest/tests/test-chained.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { shortest } from "../src";

// Sequential tests
shortest([
"Login to the application",
"Navigate to invoices",
"Send invoice",
]).expect("All steps should complete successfully");
Loading