APIT Library for API Testing
npm install apit-coreAPIT is a lightweight, fully-typed API testing framework built with TypeScript. It allows you to define API services, write tests with dynamic data flows, and generate test reports including Mermaid diagrams.
---
``bash`
npm install apit-core
If using the CLI (coming soon):
`bash`
npm install -g apitcli
---
- Define reusable services for any API endpoint
- Write tests with dynamic param injection and assertions
- Create flows of test cases for end-to-end scenarios
- Generate Markdown and Mermaid diagrams for reports
- Full TypeScript support for request/response types
---
`ts
export const serviceSignUp = APIT.createService
id: "SIGN_UP",
endpoint: "http://localhost:4000/auth/sign-up",
method: HttpMethod.POST,
});
export const serviceVerifyEmail = APIT.createService<
void,
void,
{ tokenVerification: string }
>({
id: "VERIFY_EMAIL",
endpoint: "http://localhost:4000/auth/verify-user-email/{tokenVerification}",
method: HttpMethod.GET,
});
`
`ts
export const signUpTestService = APIT.createTest({
id: "SIGN_UP",
service: serviceSignUp,
body: { email, password, username },
expects: (res) => {
expect(res).toHaveProperty("tokenVerification");
expect(res).toHaveProperty("id");
},
});
export const verifyEmailTestService = APIT.createTest({
id: "VERIFY_EMAIL",
service: serviceVerifyEmail,
params: {
tokenVerification: () =>
signUpTestService.response?.tokenVerification || "",
},
expects: () => {},
});
`
`ts
const apit = new APITCore();
const flow = APIT.createFlow("FULL_FLOW", [
signUpTestService,
verifyEmailTestService,
]);
apit.add(flow);
await apit.run();
await apit.generateReportMermaid();
`
---
``
apit-tests/
āāā src/
ā āāā services.ts # Your API service definitions
āāā tests/
ā āāā specs.ts # Reusable test cases
ā āāā flows.ts # Combine test cases into flows
āāā test-report.md # Markdown execution report (autogenerated)
---
After running, APIT will generate:
- test-report.md ā Full request/response logsmermaid-test-report.md
- ā Execution flow diagram in Mermaid
Example:
`mermaid`
graph LR
SIGN_UP --> VERIFY_EMAIL
style SIGN_UP fill:#389B35
style VERIFY_EMAIL fill:#389B35
---
- š Full TypeScript type safety
- š Reuse services across tests
- 𤪠Clear test structure with expectations
- ā” Dynamic params from previous tests
- š Mermaid + Markdown reports out of the box
---
`ts`
export const signInTestService = APIT.createTest({
id: "SIGN_IN",
service: serviceSignIn,
body: {
email,
password,
mfaCode: "{mfaCode}",
},
params: {
mfaCode: () => getMfaCodeTestService.response?.tokenMfa || "",
},
expects: (result) => {
expect(result).toHaveProperty("credentials");
expect(result.credentials).toHaveProperty("token");
},
});
`ts`
headers: {
Authorization: "Bearer {token}"
},
params: {
token: () => signInTestService.response?.credentials.token || "",
},
`ts`
params: {
id: () => getListItems.response?.[0].id || ""
},
endpoint: "http://localhost:4000/items/{id}"
`ts`
export const healthCheck = APIT.createTest({
id: "HEALTH_CHECK",
service: APIT.createService
id: "HEALTH",
endpoint: "http://localhost:4000/health",
method: HttpMethod.GET,
}),
expects: (res) => {
expect(res.status).toBe("ok");
},
});
---
`bash``
apitcli start # Create starter files
apitcli run # Run all test flows
---
MIT Ā© 2024