Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/grafana/k6-template-typescript
Template to use TypeScript with k6
https://github.com/grafana/k6-template-typescript
javascript k6 load-testing performance-testing templates typescript
Last synced: 4 days ago
JSON representation
Template to use TypeScript with k6
- Host: GitHub
- URL: https://github.com/grafana/k6-template-typescript
- Owner: grafana
- License: apache-2.0
- Created: 2020-06-09T09:09:44.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2024-08-30T22:40:13.000Z (3 months ago)
- Last Synced: 2024-10-29T16:45:30.185Z (about 1 month ago)
- Topics: javascript, k6, load-testing, performance-testing, templates, typescript
- Language: TypeScript
- Homepage:
- Size: 688 KB
- Stars: 279
- Watchers: 146
- Forks: 66
- Open Issues: 15
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-k6 - k6-typescript-template - Starter template using Webpack bundler to write k6 tests in TypeScript. (Examples/Templates)
- awesome-k6 - k6-typescript-template - Starter template using Webpack bundler to write k6 tests in TypeScript. (Examples/Templates)
README
![banner](docs/ts-js-k6.png)# Template to use TypeScript with k6
![.github/workflows/push.yml](https://github.com/grafana/k6-template-typescript/workflows/.github/workflows/push.yml/badge.svg?branch=main)
This repository provides a scaffolding project to start using TypeScript in your k6 scripts.
## Rationale
While JavaScript is great for a myriad of reasons, one area where it fall short is type safety and developer ergonomics. It's perfectly possible to write JavaScript code that will look OK and behave OK until a certain condition forces the executor into a faulty branch.
While it, of course, still is possible to shoot yourself in the foot with TypeScript as well, it's significantly harder. Without adding much overhead, TypeScript will:
- Improve the ability to safely refactor your code.
- Improve readability and maintainability.
- Allow you to drop a lot of the defensive code previously needed to make sure consumers are calling functions properly.## Installation
**Creating a project from the `template-typescript` template**
To generate a TypeScript project that includes the dependencies and initial configuration, navigate to the [template-typescript](https://github.com/grafana/k6-template-typescript) page and click **Use this template**.
![](docs/use-this-template-button.png)
**Install dependencies**
Clone the generated repository on your local machine, move to the project root folder and install the dependencies defined in [`package.json`](./package.json)
```bash
npm install
```## Running the test
To run a test written in TypeScript, we first have to transpile the TypeScript code into JavaScript running a bundler. This project uses `Babel` and `Webpack` to bundle the different files into ES modules (ESM), using its [`webpack.config.js`](./webpack.config.js) configuration.
The next command transforms each TypeScript test in `./src` to the `./dist` folder as ES modules.
```bash
npm run bundle
```Once that is done, we can run our script the same way we usually do, for instance:
```bash
k6 run dist/get-200-status-test.js
```**See also**
- [Using k6 / Modules](https://grafana.com/docs/k6/latest/using-k6/modules/)
- [Using k6 / JavaScript compatibility mode](https://grafana.com/docs/k6/latest/using-k6/javascript-compatibility-mode/)
- [grafana/k6-rollup-example](https://github.com/grafana/k6-rollup-example)