anti-work / shortest
- четверг, 26 декабря 2024 г. в 00:00:02
QA via natural language AI tests
AI-powered natural language end-to-end testing framework.
npm install -D @antiwork/shortest
# or
pnpm add -D @antiwork/shortest
# or
yarn add -D @antiwork/shortest
Add .shortest/
to your .gitignore
(where Shortest stores screenshots of each test run):
echo ".shortest/" >> .gitignore
npx shortest # for npm
pnpm shortest # for pnpm
yarn shortest # for yarn
shortest.config.ts
import type { ShortestConfig } from '@antiwork/shortest';
export default {
headless: false,
baseUrl: 'http://localhost:3000',
testDir: 'app/__tests__',
anthropicKey: process.env.ANTHROPIC_API_KEY
} satisfies ShortestConfig;
app/__tests__/login.test.ts
import { shortest } from '@antiwork/shortest'
shortest('Login to the app using email and password', { username: process.env.GITHUB_USERNAME, password: process.env.GITHUB_PASSWORD })
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.
import { shortest } from '@antiwork/shortest';
import { db } from '@/lib/db/drizzle';
import { users } from '@/lib/db/schema';
import { eq } from 'drizzle-orm';
shortest('Login to the app using username and password', {
username: process.env.USERNAME,
password: process.env.PASSWORD
}).after(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();
});
You can use lifecycle hooks to run code before and after the test.
import { shortest } from '@antiwork/shortest';
shortest.beforeAll(async ({ page }) => {
await clerkSetup({
frontendApiUrl: process.env.PLAYWRIGHT_TEST_BASE_URL ?? "http://localhost:3000",
});
});
shortest.beforeEach(async ({ page }) => {
await clerk.signIn({
page,
signInParams: {
strategy: "email_code",
identifier: "iffy+clerk_test@example.com"
},
});
});
shortest.afterEach(async ({ page }) => {
await page.close();
});
shortest.afterAll(async ({ page }) => {
await clerk.signOut({ page });
});
shortest # Run all tests
shortest login.test.ts # Run specific test
shortest --headless # Run in headless mode using cli
And you're done!
Shortest currently supports login using Github 2FA. For GitHub authentication tests:
.env.local
file or use the Shortest CLI to add itshortest --github-code --secret=<OTP_SECRET>
Required in .env.local
:
ANTHROPIC_API_KEY=your_api_key
GITHUB_TOTP_SECRET=your_secret # Only for GitHub auth tests
You can run shortest in your CI/CD pipeline by running tests in headless mode. Make sure to add your Anthropic API key to your CI/CD pipeline secrets.
This guide will help you set up the Shortest web app for local development.
useFormStatus
Clone the repository:
git clone https://github.com/anti-work/shortest.git
cd shortest
Install dependencies:
npm install -g pnpm
pnpm install
vercel env pull
to get the latest environment variablespnpm run setup
to configure the environment variables.pnpm drizzle-kit generate
pnpm db:migrate
pnpm db:seed # creates stripe products, currently unused
You'll need to set up the following services for local development. If you're not a Gumroad Vercel team member, you'll need to either run the setup wizard pnpm run setup
or manually configure each of these services and add the corresponding environment variables to your .env.local
file:
.env.local
file.
Create Database
button.
Postgres
from the Browse Storage
menu.
Quickstart
.env.local
tab.
Developers
dashboard at stripe.com.Test mode
.API Keys
tab and copy your Secret key
.
pnpm run stripe:webhooks
. It will prompt you to login with a code then give you your STRIPE_WEBHOOK_SECRET
.
Create a GitHub OAuth App:
Developer settings
> OAuth Apps
> New OAuth App
.Configure Clerk with GitHub OAuth:
Once you have set up the environment variables and installed dependencies, run the development server:
pnpm dev
Open http://localhost:3000 in your browser to see the app in action.
pnpm build:pkg
pnpm install
# packages/shortest
cd packages/shortest
pnpm link --global
# root
cd ../..
pnpm link --global shortest
cd ~/shortest-test
cd /packages/shortest
pnpm pack
cd ~/test-cli
npm init -y
npm install ../packages/shortest/antiwork-shortest-{version}.tgz
# or to run globally
npm install -g ../packages/shortest/antiwork-shortest-{version}.tgz
npx shortest -h
# or
./node_modules/.bin/shortest -h
# or if you have installed shortest globally
shortest -h