Run tests in Kotlin/Wasm
You can run Kotlin/Wasm tests in a browser to check how your code behaves in the environment where your application runs.
This page explains how to configure browsers, write tests, and review test results for the wasmJs target. To run tests for the wasmWasi target, see Get started with Kotlin/Wasm and WASI.
For browser testing, you can choose between the following options:
The Karma test runner is the default. However, the Karma project is deprecated, and no new features or bug fixes are expected.
The experimental DSL for browser testing is an alternative to Karma. It uses:
Playwright to automatically download and control the browsers based on the browser engines you specify.
Mocha to run the tests inside the browser.
Add test dependencies
To write and run tests, add the kotlin-test dependency to commonTest. This makes test annotations and assertion functions available in commonTest and in source sets that depend on it, such as wasmJsTest:
Configure browsers
You can run Kotlin/Wasm tests in specific browsers that meet Kotlin/Wasm requirements. By default, Karma uses Headless Chromium, and the experimental DSL uses Chromium. To configure specific browsers, use the testTask {} block for Karma or the test {} block for the experimental DSL for browser testing:
With Karma, install each configured browser on the machine where the tests run, such as your local machine or a CI runner.
The chromium() function enables a browser runner. If you add the test {} block but don't enable any browser runner, Gradle reports a warning and uses Chromium.
If you use IntelliJ IDEA, click Sync Gradle Changes to load the updated configuration.
Configure browser runners
With the DSL for browser testing, you can apply custom options to browser runners:
To apply options to all browser runners, set them in the
test {}block.To override an option for a specific runner, set it in that runner's configuration block.
The following example sets a timeout of 10 seconds for Chromium and Firefox and 35 seconds for WebKit:
Add tests
Place tests that use only common APIs in the commonTest source set. This way, they can run on all targets in the module. The wasmJsBrowserTest task compiles these tests to WebAssembly and runs them in a browser.
The following example shows a test class with one test failing intentionally:
The longKeepsPrecision() function passes because the sum fits in a Long. The intOverflows() function fails because adding 1 to Int.MAX_VALUE wraps the result to Int.MIN_VALUE (-2_147_483_648) before toLong() converts it.
Run tests
You can run Kotlin/Wasm browser tests from IntelliJ IDEA or the terminal:
In IntelliJ IDEA, click the green Run icon in the gutter next to the test class or function and choose the wasmJs option.
In the terminal, run the
wasmJsBrowserTestGradle task from the project root directory:```bash ./gradlew wasmJsBrowserTest ```
With the experimental DSL, Playwright downloads the configured browsers the first time you run the tests. Subsequent runs can be faster because they reuse the cached downloads, including runs in other projects that use the same browser versions.
You can also review the test results:
In IntelliJ IDEA, use the Run tool window. Click a failed test to see its stack trace or double-click it to navigate to its implementation. The square brackets in the test name identify the target, environment, and browser runner.

In the Gradle test report, open
/reports/tests/wasmJsBrowserTest/index.htmlin a browser. Follow the links in the report to see details about individual test cases.
In the example above, intOverflows() fails, resulting in a 50% success rate. To make it pass, convert Int.MAX_VALUE to Long before adding 1 by changing the expression to Int.MAX_VALUE.toLong() + 1. If you rerun the example after this change, both tests pass.
Leave feedback
Share feedback about your Kotlin/Wasm testing experience:
Slack: get a Slack invite and provide your feedback directly to the developers in our #webassembly channel.
Provide your feedback in YouTrack.