k6-typescript-template
v1.0.0
Published
A modern TypeScript boilerplate for k6 performance testing
Maintainers
Readme
K6 Performance Testing Boilerplate
A modern, TypeScript-based boilerplate for creating and running performance tests with k6. This boilerplate provides a structured way to write, organize, and execute load tests with environment-specific configurations.
Features
- 🎯 TypeScript support
- 🌍 Environment-specific configurations (local, staging, production)
- 🛠️ CLI tool for generating new test files
- 📊 Default performance thresholds
- 🔄 Hot reloading during development
- 📝 Type definitions for better developer experience
- 🎨 Customizable test scenarios
Prerequisites
- Node.js (v14 or higher)
- k6 (latest version)
- npm or yarn
Installation
Clone the repository:
git clone https://github.com/yourusername/k6-boilerplate.gitInstall dependencies:
npm installBuild the project:
npm run buildProject Structure
├── src/
│ ├── config.ts # Environment configurations
│ ├── endpoints/ # Test files
│ │ ├── example.test.ts
│ │ └── new-endpoint.test.ts
│ ├── lib/ # Shared utilities
│ │ ├── baseRequest.ts
│ │ └── testBuilder.ts
│ ├── types/ # TypeScript type definitions
│ └── index.ts
├── scripts/
│ └── create-test.js # Test file generator
└── dist/ # Compiled JavaScript filesUsage
Creating a New Test
Use the CLI tool to generate a new test file:
npm run create-test -- "user-login" -m POST -e "/api/auth/login"Options:
-m, --method: HTTP method (GET, POST, PUT, DELETE, PATCH)-e, --endpoint: API endpoint path
Running Tests
Run in the local environment:
npm run test:local dist/endpoints/example.test.jsRun in the staging environment:
npm run test:staging dist/endpoints/example.test.jsRun in the production environment:
npm run test:prod dist/endpoints/example.test.jsDevelopment Mode
Watch for changes and rebuild:
npm run build:watchConfiguration
Environment Configuration
Edit src/config.ts to modify environment-specific settings:
const environments = {
local: {
baseUrl: 'http://localhost:3000',
defaultHeaders: {
'Accept': 'application/json',
// ... other headers
},
defaultThresholds: {
'http_req_duration': ['p(95)<500', 'p(99)<1000'],
// ... other thresholds
}
},
// ... staging and production configs
};Test Configuration
Each test can be customized with the following options:
export let options = createTestConfig({
name: 'Test Name',
request: {
method: 'GET',
endpoint: `${config.baseUrl}/path`,
headers: config.defaultHeaders,
},
thresholds: config.defaultThresholds,
stages: [
{ duration: '10s', target: 10 },
{ duration: '30s', target: 100 },
{ duration: '10s', target: 0 },
],
tags: {
testType: 'api'
}
});Available Scripts
npm test- Run k6 testsnpm run test:local- Run tests in local environmentnpm run test:staging- Run tests in staging environmentnpm run test:prod- Run tests in production environmentnpm run build- Build the projectnpm run build:watch- Build and watch for changesnpm run create-test- Create a new test file
Type Definitions
The boilerplate includes TypeScript definitions for:
- Request configurations
- Test configurations
- Load profiles
- Threshold configurations
Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- k6 - Modern load testing tool
- TypeScript - TypeScript language
- webpack - Module bundler
