From ce4aeeaa56d851d39ddd1b9bd37c65b305ebbd43 Mon Sep 17 00:00:00 2001 From: Amine Saissi Hassani Date: Fri, 27 Dec 2024 20:05:38 +0100 Subject: [PATCH 1/6] shortest accepts an array of string tests --- packages/shortest/src/index.ts | 25 +++++++++++++++++++++++-- packages/shortest/src/types/test.ts | 1 + packages/shortest/tests/test-chained.ts | 8 ++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 packages/shortest/tests/test-chained.ts diff --git a/packages/shortest/src/index.ts b/packages/shortest/src/index.ts index 959d8890..57d7920f 100644 --- a/packages/shortest/src/index.ts +++ b/packages/shortest/src/index.ts @@ -110,12 +110,33 @@ export function getConfig(): ShortestConfig { } function createTestChain( - nameOrFn: string | ((context: TestContext) => Promise), + nameOrFn: string | string[] | ((context: TestContext) => Promise), payloadOrFn?: ((context: TestContext) => Promise) | any, fn?: (context: TestContext) => Promise ): 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++; @@ -181,7 +202,7 @@ function createTestChain( } export const test: TestAPI = Object.assign( - (nameOrFn: string | ((context: TestContext) => Promise), payloadOrFn?: ((context: TestContext) => Promise) | any, fn?: (context: TestContext) => Promise) => + (nameOrFn: string | string[] | ((context: TestContext) => Promise), payloadOrFn?: ((context: TestContext) => Promise) | any, fn?: (context: TestContext) => Promise) => createTestChain(nameOrFn, payloadOrFn, fn), { beforeAll: (nameOrFn: string | ((ctx: TestContext) => Promise)) => { diff --git a/packages/shortest/src/types/test.ts b/packages/shortest/src/types/test.ts index 8bd013a3..34e837ad 100644 --- a/packages/shortest/src/types/test.ts +++ b/packages/shortest/src/types/test.ts @@ -63,6 +63,7 @@ export type TestChain = { export type TestAPI = { (fn: (context: TestContext) => Promise): TestChain; (name: string): TestChain; + (names: string[]): TestChain; (name: string, fn?: (context: TestContext) => Promise): TestChain; (name: string, payload?: any, fn?: (context: TestContext) => Promise): TestChain; diff --git a/packages/shortest/tests/test-chained.ts b/packages/shortest/tests/test-chained.ts new file mode 100644 index 00000000..b532e70a --- /dev/null +++ b/packages/shortest/tests/test-chained.ts @@ -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'); + + + From 7b4bfd24a9a5fd7755185f1b0562ae0b0bb6bf91 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Fri, 27 Dec 2024 22:50:28 +0000 Subject: [PATCH 2/6] [autofix.ci] apply automated fixes --- packages/shortest/src/index.ts | 13 ++++++++----- packages/shortest/tests/test-chained.ts | 12 ++++++------ 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/packages/shortest/src/index.ts b/packages/shortest/src/index.ts index 18e96f1d..eb8fe760 100644 --- a/packages/shortest/src/index.ts +++ b/packages/shortest/src/index.ts @@ -121,9 +121,9 @@ function createTestChain( const tests = nameOrFn.map((name) => { const test: TestFunction = { name, - expectations: [] + expectations: [], }; - + registry.tests.set(name, [...(registry.tests.get(name) || []), test]); registry.currentFileTests.push(test); return test; @@ -132,7 +132,7 @@ function createTestChain( // Return chain for the last test const lastTest = tests[tests.length - 1]; if (!lastTest.name) { - throw new Error('Test name is required'); + throw new Error("Test name is required"); } return createTestChain(lastTest.name, payloadOrFn, fn); } @@ -202,8 +202,11 @@ function createTestChain( } export const test: TestAPI = Object.assign( - (nameOrFn: string | string[] | ((context: TestContext) => Promise), payloadOrFn?: ((context: TestContext) => Promise) | any, fn?: (context: TestContext) => Promise) => - createTestChain(nameOrFn, payloadOrFn, fn), + ( + nameOrFn: string | string[] | ((context: TestContext) => Promise), + payloadOrFn?: ((context: TestContext) => Promise) | any, + fn?: (context: TestContext) => Promise, + ) => createTestChain(nameOrFn, payloadOrFn, fn), { beforeAll: (nameOrFn: string | ((ctx: TestContext) => Promise)) => { const hook = typeof nameOrFn === "function" ? nameOrFn : undefined; diff --git a/packages/shortest/tests/test-chained.ts b/packages/shortest/tests/test-chained.ts index b532e70a..a5a49b37 100644 --- a/packages/shortest/tests/test-chained.ts +++ b/packages/shortest/tests/test-chained.ts @@ -1,8 +1,8 @@ -import { shortest } from '../src'; +import { shortest } from "../src"; // Sequential tests -shortest(['Login to the application', 'Navigate to invoices', 'Send invoice']) - .expect('All steps should complete successfully'); - - - +shortest([ + "Login to the application", + "Navigate to invoices", + "Send invoice", +]).expect("All steps should complete successfully"); From 7f7d5118948e5b6a7d1f418f861daa88316d61a0 Mon Sep 17 00:00:00 2001 From: Amine Saissi Hassani Date: Mon, 30 Dec 2024 20:00:25 +0100 Subject: [PATCH 3/6] add chained tests in readme --- README.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/README.md b/README.md index 3b249b03..b615882e 100644 --- a/README.md +++ b/README.md @@ -130,6 +130,30 @@ shortest.afterAll(async ({ page }) => { }); ``` +### Chained Tests + +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 From 03f035e3b4a0e050c832d2f9169d74c6ae937e80 Mon Sep 17 00:00:00 2001 From: Crabest <46903845+crabest@users.noreply.github.com> Date: Mon, 30 Dec 2024 22:26:58 +0100 Subject: [PATCH 4/6] Update README.md Co-authored-by: Sahil Lavingia --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b615882e..70532de6 100644 --- a/README.md +++ b/README.md @@ -130,7 +130,7 @@ shortest.afterAll(async ({ page }) => { }); ``` -### Chained Tests +### Chaining tests Shortest supports flexible test chaining patterns: From f7dc8df0aad5c68308c629f6f031423cadf5b8a0 Mon Sep 17 00:00:00 2001 From: Sahil Lavingia Date: Tue, 31 Dec 2024 13:05:24 -0500 Subject: [PATCH 5/6] Update test-chained.ts --- packages/shortest/tests/test-chained.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/shortest/tests/test-chained.ts b/packages/shortest/tests/test-chained.ts index a5a49b37..bc5e3d18 100644 --- a/packages/shortest/tests/test-chained.ts +++ b/packages/shortest/tests/test-chained.ts @@ -5,4 +5,4 @@ shortest([ "Login to the application", "Navigate to invoices", "Send invoice", -]).expect("All steps should complete successfully"); +]); From 7ba4f7be49f81e8d26c76b9da83fb78bf07fe0bc Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Tue, 31 Dec 2024 18:06:18 +0000 Subject: [PATCH 6/6] [autofix.ci] apply automated fixes --- packages/shortest/tests/test-chained.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/shortest/tests/test-chained.ts b/packages/shortest/tests/test-chained.ts index bc5e3d18..e2241798 100644 --- a/packages/shortest/tests/test-chained.ts +++ b/packages/shortest/tests/test-chained.ts @@ -1,8 +1,4 @@ import { shortest } from "../src"; // Sequential tests -shortest([ - "Login to the application", - "Navigate to invoices", - "Send invoice", -]); +shortest(["Login to the application", "Navigate to invoices", "Send invoice"]);