Skip to content

Issues with using a websocket k6 typescript enum typeΒ #4540

Closed

Description

@joffotron

Brief summary

Using e.g. the EventName.Open type from the k6/experimental/websockets compiles but can't be run

k6 version

k6 v0.56.0 (go1.23.4, darwin/arm64)

OS

MacOS 15.3

Docker version and image (if applicable)

No response

Steps to reproduce the problem

I have some code that can be summarised thus:

import { EventName, Params, WebSocket } from 'k6/experimental/websockets';
...

const ws = new WebSocket(`${websocketHost}/ocpp/1.6/`, null, params);
ws.addEventListener(EventName.Open, () => {
    console.log('Connected');
}

I can try running it in several ways:

  • k6 run --compatibility-mode=experimental_enhanced src/idle-station.ts
  • k6pack src/idle-station.ts| k6 run -
  • find ./src -type f -name '*.ts' -exec esbuild '{}' --bundle --outbase=src --outdir=dist --external:k6 --format=esm \; then k6 run dist/idle-station.js

Expected behaviour

I expect the code to compile and run the test

Actual behaviour

For all of the above methods, I get this runtime error:

ERRO[0000] TypeError: Cannot read property 'Open' of undefined
        at idle_station_default (file:///Users/joff/src/chargefox/load-testing/dist/idle-station.js:16:33(19))  executor=shared-iterations scenario=default source=stacktrace

To work around this in the meantime -

I can either use the "open" literal in place of the enum:

ws.addEventListener('open', () => {

Although then my IDE complains of TS2345: Argument of type "open" is not assignable to parameter of type EventName

Or I can use a type assertion:

ws.addEventListener(<EventName>'open', () => {

Which isn't ideal either

Activity

  1. Fernando-hub527 commented on Feb 21, 2025

    @Fernando-hub527
    Contributor

    Hi, I'm starting to contribute to K6 and would like to help with this issue.

  2. oleiade commented on Mar 26, 2025

    @oleiade
    Contributor

    Hey folks πŸ‘‹πŸ»

    Thanks for taking the time to report this, and cheers @Fernando-hub527 for putting some time into it. Before we start reviewing the PR though, I have a preliminary question: where is this EventName construct coming from?

    A quick search didn't show any results in specifications around WebSockets and JS, so I'm a little bit confused. Also, going though the code of the experimental module, it doesn't seem to be a thing. Looking into websockets, it seems the first argument to addEventListener seems to be a static string, on par with the current implementation.

    k6 tries to stick as strictly as possible to specifications, especially for protocols/technologies like WebSockets, thus, I'm wondering whether that's an implementation mishap/lack or whether that's a request to add a feature?

    I'm definetly not a websockets expert so forgive my potential naivity, but the websockets module doesn't expose an EventName either as far as I can tell, and I genuinely wonder where this expectation came from in the first place; is it something we have documented somewhere? πŸ™‡πŸ»

    PS: I will keep the issue in triage while we wait for clarifications

  3. joffotron commented on Apr 4, 2025

    @joffotron
    Author

    @oleiade Maybe I can help shed some light... the definition appears (already) in node_modules/@types/k6/experimental/websockets/index.d.ts

    ... which now that I read this more closely makes me think this issue should be filed over on the DefinitelyTyped repo? Unsure about who maintains each type definition over there with respect to each project!?

    https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/k6/experimental/websockets/index.d.ts#L210-L240

  4. oleiade commented on Apr 10, 2025

    @oleiade
    Contributor

    @joffotron I would suggest indeed to bring it to the DefinitelyTyped repo instead, as I don't think adding this type specifically to the k6 codebase's module adds value πŸ™‡πŸ»

    Also, we maintain it. I believe the way to go would be to open a PR directly on the definitely typed repo, and their bot will ping maintainers automatically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions