Skip to content

Commit ec7ec36

Browse files
committed
Initial commit
0 parents  commit ec7ec36

12 files changed

+2574
-0
lines changed

.babelrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"presets": ["@babel/env", "@babel/typescript"],
3+
"plugins": [
4+
"@babel/proposal-class-properties",
5+
6+
"@babel/proposal-object-rest-spread"
7+
]
8+
}

.github/workflows/push.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
on: [push]
2+
3+
jobs:
4+
k6_transpile_bundle_test:
5+
name: Transpile, bundle and run
6+
runs-on: ubuntu-latest
7+
8+
steps:
9+
- name: Checkout
10+
uses: actions/checkout@v2
11+
- name: Install dependencies
12+
run: |
13+
yarn --frozen-lockfile
14+
15+
- name: Transpile and bundle test script
16+
run: |
17+
yarn webpack
18+
19+
- name: Run local k6 test
20+
uses: k6io/[email protected]
21+
with:
22+
filename: dist/test1.js

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
yarn-error.log
3+
dist

README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<div align="center">
2+
3+
![banner](assets/ts-js-k6.png)
4+
5+
# Template to use TypeScript with k6
6+
7+
![.github/workflows/push.yml](https://github.com/k6io/template-typescript/workflows/.github/workflows/push.yml/badge.svg?branch=master)
8+
9+
</div>
10+
11+
This repository provides a scaffolding project to start using TypeScript in your k6 scripts.
12+
13+
## Rationale
14+
15+
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.
16+
17+
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:
18+
19+
- Improve the ability to safely refactor your code.
20+
- Improve readability and maintainablity.
21+
- Allow you to drop a lot of the defensive code previously needed to make sure consumers are calling functions properly.
22+
23+
24+
## Prerequisites
25+
26+
- [k6](https://k6.io/docs/getting-started/installation)
27+
- [NodeJS](https://nodejs.org/en/download/)
28+
- [Yarn](https://yarnpkg.com/getting-started/install) (optional)
29+
30+
## Installation
31+
32+
**Creating a project from the `template-typescript` template**
33+
34+
To generate a TypeScript project that includes the dependencies and initial configuration, navigate to the [template-typescript](https://github.com/k6io/template-typescript) page and click **Use this template**.
35+
36+
![](assets/use-this-template-button.png)
37+
38+
39+
**Install dependencies**
40+
41+
Clone the generated repository on your local machine, move to the project root folder and install the dependencies defined in [`package.json`](./package.json)
42+
43+
```bash
44+
$ yarn install
45+
```
46+
47+
## Running the test
48+
49+
To run a test written in TypeScript, we first have to transpile the TypeScript code into JavaScript and bundle the project
50+
51+
```bash
52+
$ yarn webpack
53+
```
54+
55+
This command creates the final test files to the `./dist` folder.
56+
57+
Once that is done, we can run our script the same way we usually do, for instance:
58+
59+
```bash
60+
$ k6 run dist/test1.js
61+
```
62+
63+
### Transpiling and Bundling
64+
65+
By default, k6 can only run ES5.1 JavaScript code. To use TypeScript, we have to set up a bundler that converts TypeScript to JavaScript code.
66+
67+
This project uses `Babel` and `Webpack` to bundle the different files - using the configuration of the [`webpack.config.js`](./webpack.config.js) file.
68+
69+
If you want to learn more, check out [Bundling node modules in k6](https://k6.io/docs/using-k6/modules#bundling-node-modules).

assets/ts-js-k6.png

21 KB
Loading

assets/use-this-template-button.png

51.4 KB
Loading

package.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "typescript",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"repository": "ssh://[email protected]/k6io/example-typescript.git",
6+
"author": "Simon Aronsson <[email protected]>",
7+
"license": "MIT",
8+
"dependencies": {
9+
"@babel/core": "7.13.16",
10+
"@babel/plugin-proposal-class-properties": "7.13.0",
11+
"@babel/plugin-proposal-object-rest-spread": "7.13.8",
12+
"@babel/preset-env": "7.13.15",
13+
"@babel/preset-typescript": "7.13.0",
14+
"@types/k6": "~0.32.0",
15+
"@types/webpack": "5.28.0",
16+
"babel-loader": "8.2.2",
17+
"typescript": "4.2.4",
18+
"webpack": "5.35.1"
19+
},
20+
"devDependencies": {
21+
"webpack-cli": "4.6.0",
22+
"clean-webpack-plugin": "4.0.0-alpha.0"
23+
}
24+
}

src/test1.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { sleep, check } from 'k6';
2+
import { Options } from 'k6/options';
3+
import http from 'k6/http';
4+
5+
export let options:Options = {
6+
vus: 50,
7+
duration: '10s'
8+
};
9+
10+
export default () => {
11+
const res = http.get('https://test-api.k6.io');
12+
check(res, {
13+
'status is 200': () => res.status === 200,
14+
});
15+
sleep(1);
16+
};

src/test2.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { sleep, check } from 'k6';
2+
import { Options } from 'k6/options';
3+
4+
/* @ts-ignore */
5+
import { randomIntBetween } from 'https://jslib.k6.io/k6-utils/1.1.0/index.js';
6+
import http from 'k6/http';
7+
8+
export let options:Options = {
9+
vus: 50,
10+
duration: '10s'
11+
};
12+
13+
export default () => {
14+
const res = http.post('https://httpbin.org/status/400');
15+
check(res, {
16+
'status is 400': () => res.status === 400,
17+
});
18+
sleep(randomIntBetween(1,5));
19+
};

tsconfig.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"moduleResolution": "node",
5+
"module": "commonjs",
6+
"noEmit": true,
7+
"allowJs": true,
8+
"removeComments": true,
9+
10+
"strict": true,
11+
"noImplicitAny": true,
12+
"noImplicitThis": true,
13+
14+
"noUnusedLocals": true,
15+
"noUnusedParameters": true,
16+
"noImplicitReturns": true,
17+
"noFallthroughCasesInSwitch": true,
18+
19+
"allowSyntheticDefaultImports": true,
20+
"esModuleInterop": true,
21+
"experimentalDecorators": true,
22+
"emitDecoratorMetadata": true,
23+
24+
"skipLibCheck": true
25+
}
26+
}

webpack.config.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const path = require('path');
2+
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
3+
4+
module.exports = {
5+
mode: 'production',
6+
context: path.join(__dirname, 'src'),
7+
entry: {
8+
test1: './test1.ts',
9+
test2: './test2.ts',
10+
},
11+
output: {
12+
path: path.join(__dirname, 'dist'),
13+
libraryTarget: 'commonjs',
14+
filename: '[name].js',
15+
},
16+
resolve: {
17+
extensions: ['.ts', '.js'],
18+
},
19+
module: {
20+
rules: [
21+
{
22+
test: /\.ts$/,
23+
use: 'babel-loader',
24+
},
25+
],
26+
},
27+
target: 'web',
28+
externals: /^(k6|https?\:\/\/)(\/.*)?/,
29+
stats: {
30+
colors: true,
31+
},
32+
plugins: [
33+
new CleanWebpackPlugin(),
34+
],
35+
}

0 commit comments

Comments
 (0)