Skip to content

Commit

Permalink
TS support is on by default and reword compatibility-mode page
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov committed Feb 7, 2025
1 parent f3a3d54 commit 4f0ddcd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 19 deletions.
2 changes: 1 addition & 1 deletion docs/sources/k6/next/get-started/write-your-first-test.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ Let’s walk through creating a simple test which performs 10 `GET` HTTP request
After you're comfortable with this basic script, you can extend its functionality in many ways. Here are a few ideas to get you started:

1. **Multiple requests**: You can add more `http.get()` or `http.post()` requests to simulate complex user flows.
2. **Using TypeScript**: If you prefer TypeScript, k6 also supports it. You can learn more in our [TypeScript guide](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/javascript-typescript-compatibility-mode/#experimental-enhanced-mode).
2. **Using TypeScript**: If you prefer TypeScript, k6 also supports it. You can learn more in our [TypeScript guide](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/javascript-typescript-compatibility-mode/#typescript-support).
3. **Thresholds, checks, and metrics**: You can add conditions to monitor performance. For example, you can set thresholds to ensure the response time doesn’t exceed a certain limit. Refer to [Thresholds](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/thresholds/) and [Checks](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/checks/) for more details.
4. **Browser tests**: Use the browser module to simulate user interactions like clicking buttons or filling out forms. This is useful for testing web applications. Refer to [Using k6 browser](https://grafana.com/docs/k6/<K6_VERSION>/using-k6-browser/) for more details.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,24 @@ aliases:
- ./javascript-compatibility-mode/ # /docs/k6/<K6_VERSION>/using-k6/javascript-compatibility-mode/
title: JavaScript and TypeScript compatibility mode
menuTitle: JavaScript and TypeScript mode
excerpt: 'k6 supports running test scripts with different ECMAScript and TypeScript compatibility modes using --compatibility-mode'
excerpt: 'k6 supports running test scripts with different compatibility modes using --compatibility-mode'
weight: 19
---

# JavaScript and TypeScript compatibility mode
# JavaScript compatibility mode

You can write k6 tests in various ECMAScript versions:
This is option most users should no longer be using.

- ES6+ JavaScript with ES modules (ESM).
- ES6+ JavaScript with CommonJS modules.
In previous versions k6 used to not natively support as much of the ECMAScript standard as was desired. For this reason the default mode used to use babel internally to add support for what was missing. This though had performance impact on starting k6 and might still not have supported as much as was desired. As such a different mode without babel (and core.js) was added that was much faster in initialization, but likely required transpilation from the user.

k6 supports both module types and most ES6+ features in all k6 execution modes: local, distributed, and cloud.
Since v0.53.0 this hasn't been needed and in general this option has minimal usability and is likely to be removed completely in future versions. See [issue](https://github.com/grafana/k6/issues/3864) for the last remaining difference.

Additionally, k6 also has experimental support for [esbuild](https://esbuild.github.io/), to transpile TypeScript (TS) code.
Additionally, k6 also has support for [esbuild](https://esbuild.github.io/), to transpile TypeScript (TS) code. This used to use compatibility-mode but is now enabled by default and no configuration is required.

Some users prefer to bundle their test code outside k6. For this reason, k6 offers three JavaScript compatibility modes:

- [Extended mode](#extended-mode): The default option.
- [Experimental enhanced mode](#experimental-enhanced-mode): The experimental option, supporting TS.
- [Base mode](#base-mode): After v0.53.0 this is almost the same as extended, but doesn't alias `global` to `globalThis` - a nodejs compatibility.
- [Extended mode](#extended-mode): The default option. That is `base` + aliasing `global` to `globalThis` - a nodejs compatibility.
- [Base mode](#base-mode): Only using native support in k6 and the underlying JS engine. After v0.53.0 it has the same functionality as `extended` apart from the `global` aliasing.

When running tests, you can change the mode by using the `--compatibility-mode` option:

Expand All @@ -44,23 +42,23 @@ $ k6 run script.js

After v0.53.0 the only difference with base is that `global` (node's global variable) is aliased to the value of `globalThis`.

## Experimental enhanced mode
## Typescript support

{{< code >}}

```cli
$ k6 run --compatibility-mode=experimental_enhanced script.ts
$ k6 run script.ts
```

```env
$ K6_COMPATIBILITY_MODE=experimental_enhanced k6 run script.ts
$ k6 run script.ts
```

{{< /code >}}

The experimental enhanced mode is similar to the extended mode, but it uses [esbuild](https://esbuild.github.io/) instead of Babel to transpile TypeScript (TS) code.
It uses [esbuild](https://esbuild.github.io/) to transpile TypeScript (TS) code for all files that have the `.ts` extension.

TypeScript support is partial as it removes the type information but doesn't provide type safety.
TypeScript support is partial as it strips the type information but doesn't provide type safety.

## Base mode

Expand All @@ -76,8 +74,6 @@ $ K6_COMPATIBILITY_MODE=base k6 run script.js

{{< /code >}}

After v0.53.0 there isn't a big reason to use this. Before that it was dropping ESM support for usually improved startup speed.

### CommonJS Example

{{< code >}}
Expand Down
2 changes: 1 addition & 1 deletion docs/sources/k6/next/using-k6/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ $ k6 run dist/signup.bundle.js \

## Use TypeScript

k6 supports partial TypeScript support with the `experimental_enhanced` compatibility mode. For more details, refer to [JavaScript and TypeScript compatibility mode](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/javascript-typescript-compatibility-mode/).
k6 supports partial TypeScript support. For more details, refer to [JavaScript and TypeScript compatibility mode](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/javascript-typescript-compatibility-mode/).

## Use modules with Docker

Expand Down

0 comments on commit 4f0ddcd

Please sign in to comment.