Skip to content

adequatica/api-vitesting

Repository files navigation

API Testing with Vitest

Stack

This stack illustrates the article API Testing with Vitest.

A basic set of packages to test API with TypeScript:

Example API for testing: APOD NASA API.

How to Use

You have to have Node.js >= 21 in order to use fetch().

  1. Clone repository;
  2. Install dependencies: npm install
  3. Run tests: npm run test

CLI Options

  • Different tested host could be passed to tests through .env variable (it can be useful for testing different environments):

HOST=https://api.nasa.gov npm test

  • Individual API key could be passed to tests through .env variable (otherwise, it will be used DEMO_KEY value):

API_KEY={api_key} npm test

npm test epic

npm run test:ui

Examples of Test Cases

Tests replicate API tests on Jest from this repository:

  • apod.test.ts — test with JSON schema validation;
  • epic.test.ts — test has a loop through array for checking elements with Chai assertion;
  • insight-weather.test.ts — test will be conditionally skipped in an inappropriate environment.

Concerns of the stack: Zod is highly inconvenient as JSON schema validator in case of dynamic keys inside an object with other typed keys.