clojurescriptk6shadow-cljs

Writing Grafana K6 test in ClojureScript


I'm trying to use K6 with a test written in ClojureScript. K6 is written in Golang, runs a bundled JS runtime, which in turn runs tests written in JavaScript.

I don't have a problem running a simplistic test that just does (js/console.log "Hello world") while using shadow-cljs with :esm target. However, the moment I try to (:require ["k6" :as k6]) - which is needed to meaningfully communicate with K6 toolset, and is available at runtime only - the compilation is refused with quite understandable error message - "The required JS dependency "k6" is not available, it was required by "k6_test/core.cljs"."

I tried to use shadow.esm/dynamic-import, but K6 fails to run the test because import is not defined:

ERRO[0000] SyntaxError: dist/main.js: Unexpected token (3:47)

> 3 | const shadow_esm_import = function(x) { return import(x) };
    |                                                ^

And I have no idea what I should change for the import to be defined, or that's just a property of K6 JS runtime that I cannot do anything about.

Are there any other ways to tell ClojureScript that a dependency is going to become available only at runtime?


Solution

  • You can just keep using (:require ["k6" :as k6]). Do not use dynamic imports.

    You can set :js-options {:keep-as-import #{"k6"}} in your build config. That will make shadow-cljs bundle everything except the k6 dependency. If the runtime can resolve that at runtime it should work. You may add more dependencies to this set if needed.

    If instead you do not intend on using any npm dependencies you may also just set :js-options {:js-provider :import}. That will make shadow-cljs emit only regular import statements, instead of trying to bundle any non-CLJS dependencies at all.