Skip to content

Commit

Permalink
add additional examples in docs
Browse files Browse the repository at this point in the history
  • Loading branch information
m2rads committed Dec 6, 2024
1 parent ae836d2 commit 10ec5a9
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
65 changes: 65 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,71 @@ import { test } from '@antiwork/shortest'
test('Login to the app using email and password', { username: process.env.GITHUB_USERNAME, password: process.env.GITHUB_PASSWORD })
```

## Using callback functions
You can also use callback functions to add additoinal assertions and other logic. AI will execute the callback function after the test
execution in browser is completed.

```typescript
import { test } from '@antiwork/shortest';
import { db } from '@/lib/db/drizzle';
import { users } from '@/lib/db/schema';
import { eq } from 'drizzle-orm';

test('Login to the app using Github login', {
username: process.env.GITHUB_USERNAME,
password: process.env.GITHUB_PASSWORD
}, async ({ page }) => {
// Get current user's clerk ID from the page
const clerkId = await page.evaluate(() => {
return window.localStorage.getItem('clerk-user');
});

if (!clerkId) {
throw new Error('User not found in database');
}

// Query the database
const [user] = await db
.select()
.from(users)
.where(eq(users.clerkId, clerkId))
.limit(1);

expect(user).toBeDefined();
});
```

## Lifecycle hooks
You can use lifecycle hooks to run code before and after the test.

```typescript
import { test } from '@antiwork/shortest';

test.beforeAll(async ({ page }) => {
await clerkSetup({
frontendApiUrl: process.env.PLAYWRIGHT_TEST_BASE_URL ?? "http://localhost:3000",
});
});

test.beforeEach(async ({ page }) => {
await clerk.signIn({
page,
signInParams: {
strategy: "email_code",
identifier: "iffy+clerk_test@example.com"
},
});
});

test.afterEach(async ({ page }) => {
await page.close();
});

test.afterAll(async ({ page }) => {
await clerk.signOut({ page });
});
```

## Running Tests
```bash
shortest # Run all tests
Expand Down
65 changes: 65 additions & 0 deletions packages/shortest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,71 @@ import { test } from '@antiwork/shortest'
test('Login to the app using email and password', { username: process.env.GITHUB_USERNAME, password: process.env.GITHUB_PASSWORD })
```

## Using callback functions
You can also use callback functions to add additoinal assertions and other logic. AI will execute the callback function after the test
execution in browser is completed.

```typescript
import { test } from '@antiwork/shortest';
import { db } from '@/lib/db/drizzle';
import { users } from '@/lib/db/schema';
import { eq } from 'drizzle-orm';

test('Login to the app using Github login', {
username: process.env.GITHUB_USERNAME,
password: process.env.GITHUB_PASSWORD
}, async ({ page }) => {
// Get current user's clerk ID from the page
const clerkId = await page.evaluate(() => {
return window.localStorage.getItem('clerk-user');
});

if (!clerkId) {
throw new Error('User not found in database');
}

// Query the database
const [user] = await db
.select()
.from(users)
.where(eq(users.clerkId, clerkId))
.limit(1);

expect(user).toBeDefined();
});
```

## Lifecycle hooks
You can use lifecycle hooks to run code before and after the test.

```typescript
import { test } from '@antiwork/shortest';

test.beforeAll(async ({ page }) => {
await clerkSetup({
frontendApiUrl: process.env.PLAYWRIGHT_TEST_BASE_URL ?? "http://localhost:3000",
});
});

test.beforeEach(async ({ page }) => {
await clerk.signIn({
page,
signInParams: {
strategy: "email_code",
identifier: "iffy+clerk_test@example.com"
},
});
});

test.afterEach(async ({ page }) => {
await page.close();
});

test.afterAll(async ({ page }) => {
await clerk.signOut({ page });
});
```

## Running Tests
```bash
shortest # Run all tests
Expand Down

0 comments on commit 10ec5a9

Please sign in to comment.