grafana/k6 · error

[options] must be an object

Error message

[options] must be an object

What it means

Thrown by the Blob constructor in k6/experimental/websockets when the second (options) argument is present but is not a real object. The constructor skips only undefined; isObject (internal/js/modules/k6/websockets/helpers.go:41) requires an actual object, so strings, numbers, booleans — and null — all throw '[options] must be an object'. Browsers tolerate new Blob(parts, null); k6 does not.

Solutions

  1. Pass an options object: new Blob(parts, { type: 'text/plain' })
  2. Omit the second argument entirely when no options are needed
  3. Never pass null for options in k6 — only undefined is skipped

Example fix

// before
const b = new Blob(['hello'], 'text/plain');

// after
const b = new Blob(['hello'], { type: 'text/plain' });
Defensive patterns

Strategy: validation

Validate before calling

function blobOptions(opts) {
  if (opts === undefined) return undefined; // only undefined is skipped
  if (opts === null || typeof opts !== 'object') {
    throw new TypeError('[options] must be an object, e.g. { type: \'text/plain\' }');
  }
  return opts;
}

const b = new Blob(['hello'], blobOptions(maybeOpts));

Type guard

function isPlainObject(v) {
  return typeof v === 'object' && v !== null;
}

Prevention

When it happens

Trigger: new Blob(['x'], 'text/plain'), new Blob(parts, 0), or new Blob(parts, null). A single-argument call new Blob(parts) is fine, as is passing undefined explicitly.

Common situations: Writing new Blob(parts, 'text/plain') instead of { type: 'text/plain' }; copying browser code that passes null as a defensive options value; a variable that was expected to hold an options object but holds a string.

Related errors


AI-assisted analysis of grafana/k6@01ffac6f24 (2026-08-18). Data as JSON: /api/errors/7be4fe29b2e907a5. Report an issue: GitHub.

Appendix: source

Thrown at internal/js/modules/k6/websockets/blob.go:43

func (r *WebSocketsAPI) blob(call sobek.ConstructorCall) *sobek.Object {
	rt := r.vu.Runtime()

	b := &blob{data: new(bytes.Buffer)}
	var blobParts []any
	if len(call.Arguments) > 0 {
		if err := rt.ExportTo(call.Arguments[0], &blobParts); err != nil {
			common.Throw(rt, fmt.Errorf("failed to process [blobParts]: %w", err))
		}
	}

	if len(blobParts) > 0 {
		r.fillData(b, blobParts, call)
	}

	if len(call.Arguments) > 1 && !sobek.IsUndefined(call.Arguments[1]) {
		opts := call.Arguments[1]
		if !isObject(opts) {
			common.Throw(rt, errors.New("[options] must be an object"))
		}

		typeOpt := opts.ToObject(rt).Get("type")
		if !sobek.IsUndefined(typeOpt) {
			b.typ = typeOpt.String()
		}
	}

	obj := rt.NewObject()
	must(rt, obj.DefineAccessorProperty("size", rt.ToValue(func() sobek.Value {
		return rt.ToValue(b.data.Len())
	}), nil, sobek.FLAG_FALSE, sobek.FLAG_TRUE))
	must(rt, obj.DefineAccessorProperty("type", rt.ToValue(func() sobek.Value {
		return rt.ToValue(b.typ)
	}), nil, sobek.FLAG_FALSE, sobek.FLAG_TRUE))

	must(rt, obj.Set("arrayBuffer", func(_ sobek.FunctionCall) sobek.Value {
		promise, resolve, _ := rt.NewPromise()

View on GitHub (pinned to 01ffac6f24)