-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add assertion support and DB validation to test framework Changes: - Added Jest expect integration for assertions - Added support for async DB validations in test steps - Improved error handling and reporting for assertions - Added test script to verify assertion implementation - Updated test builder to handle both sync/async test execution Example usage: ```typescript .when('Logged in', async () => { const user = await db.query.users.findFirst(); expect(user).toBeDefined(); }) ``` Demo: ![image](https://github.com/user-attachments/assets/7bcd4e13-8d29-476b-b8bb-2c03d9431681)
- Loading branch information
Showing
16 changed files
with
469 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { define, UITestBuilder, expect } from 'shortest'; | ||
import { db } from "@/lib/db/drizzle"; | ||
|
||
interface User { | ||
email: string; | ||
name: string; | ||
} | ||
|
||
define('Test Assertions', async () => { | ||
// Test 1: Basic Assertions (Will Pass) | ||
new UITestBuilder<User>('/') | ||
.test('Basic assertions that pass') | ||
.given('a test user', { email: 'test@test.com', name: 'Test User' }, async () => { | ||
expect(true).toBe(true); | ||
expect({ foo: 'bar' }).toEqual({ foo: 'bar' }); | ||
expect([1, 2, 3]).toContain(2); | ||
}) | ||
.when('checking database', async () => { | ||
const mockUser = { id: 1, email: 'test@test.com' }; | ||
expect(mockUser).toHaveProperty('email'); | ||
expect(mockUser.email).toMatch(/test@test.com/); | ||
}) | ||
.expect('all assertions to pass'); | ||
|
||
// Test 2: Failing Assertions (Will Fail) | ||
new UITestBuilder<User>('/') | ||
.test('Assertions that should fail') | ||
.given('some data', { email: 'fail@test.com', name: 'Fail Test' }, async () => { | ||
expect(true).toBe(false); | ||
expect({ foo: 'bar' }).toEqual({ foo: 'baz' }); | ||
}) | ||
.when('checking values', async () => { | ||
expect(null).toBeDefined(); | ||
expect(undefined).toBeTruthy(); | ||
}) | ||
.expect('to see failure reports'); | ||
|
||
// Test 3: Async Assertions (Mix of Pass/Fail) | ||
new UITestBuilder<User>('/') | ||
.test('Async assertions') | ||
.given('database connection', async () => { | ||
const user = await db.query.users.findFirst(); | ||
expect(user).toBeDefined(); | ||
}) | ||
.when('querying data', async () => { | ||
const result = await Promise.resolve({ status: 'error' }); | ||
expect(result.status).toBe('success'); | ||
}) | ||
.expect('to see mix of pass/fail results'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,17 @@ | ||
import { afterAll, beforeAll, define, UITestBuilder } from 'shortest'; | ||
import { define, UITestBuilder, expect } from 'shortest'; | ||
|
||
interface LoginState { | ||
username: string; | ||
password: string; | ||
} | ||
|
||
define('Validate login feature implemented with Clerk', () => { | ||
|
||
// new UITestBuilder<LoginState>('/') | ||
// .test('Login to the app using Email and Password') | ||
// .given('username and password', { username: 'argo.mohrad@gmail.com', password: 'Shortest1234' }) | ||
// .when('Logged in, click on view dashboard button') | ||
// .expect('should successfully redirect to /') | ||
|
||
new UITestBuilder<LoginState>('/') | ||
define('Validate login feature implemented with Clerk', async () => { | ||
new UITestBuilder<LoginState>('/') | ||
.test('Login to the app using Github login') | ||
.given('Github username and password', { username: `${process.env.GITHUB_USERNAME}`, password: `${process.env.GITHUB_PASSWORD}` }) | ||
.expect('should successfully redirect to /dashboard') | ||
|
||
.given('Github username and password', { | ||
username: process.env.GITHUB_USERNAME || '', | ||
password: process.env.GITHUB_PASSWORD || '' | ||
}) | ||
.when('Logged in') | ||
.expect('should redirect to /dashboard'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { UITestBuilder, expect } from '../src/index'; | ||
import pc from 'picocolors'; | ||
|
||
async function testAssertions() { | ||
console.log(pc.cyan('\n🧪 Testing Assertion Implementation')); | ||
console.log(pc.cyan('================================')); | ||
|
||
let failedTests = 0; | ||
let passedTests = 0; | ||
|
||
try { | ||
// Test 1: Verify failing assertions are caught | ||
console.log(pc.cyan('\nTest 1: Verify failing assertions')); | ||
try { | ||
const builder = new UITestBuilder('/') | ||
.test('Test failing assertion') | ||
.given('test data', undefined, async () => { | ||
expect(true).toBe(false); | ||
}); | ||
|
||
console.log(pc.red('❌ Failed: Assertion should have thrown error')); | ||
failedTests++; | ||
} catch (error) { | ||
console.log(pc.green('✅ Passed: Caught failing assertion')); | ||
passedTests++; | ||
} | ||
|
||
// Test 2: Verify async assertions | ||
console.log(pc.cyan('\nTest 2: Verify async assertions')); | ||
try { | ||
const builder = new UITestBuilder('/') | ||
.test('Test async assertion') | ||
.given('test data', undefined, async () => { | ||
const result = await Promise.resolve(false); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
console.log(pc.red('❌ Failed: Async assertion should have thrown')); | ||
failedTests++; | ||
} catch (error) { | ||
console.log(pc.green('✅ Passed: Caught async failing assertion')); | ||
passedTests++; | ||
} | ||
|
||
// Test 3: Verify assertion steps are recorded | ||
console.log(pc.cyan('\nTest 3: Verify assertion recording')); | ||
const builder = new UITestBuilder('/') | ||
.test('Test step recording') | ||
.given('test data', undefined, async () => { | ||
expect(true).toBe(true); | ||
}); | ||
|
||
if (builder.steps.length === 1 && builder.steps[0].assert) { | ||
console.log(pc.green('✅ Passed: Assertion step recorded')); | ||
passedTests++; | ||
} else { | ||
console.log(pc.red('❌ Failed: Assertion step not recorded')); | ||
failedTests++; | ||
} | ||
|
||
// Summary | ||
console.log(pc.cyan('\n📊 Test Summary')); | ||
console.log(pc.cyan('=============')); | ||
console.log(pc.green(`Passed: ${passedTests}`)); | ||
console.log(pc.red(`Failed: ${failedTests}`)); | ||
|
||
} catch (error) { | ||
console.error(pc.red('\n❌ Test script failed:'), error); | ||
} | ||
} | ||
|
||
testAssertions().catch(console.error); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.