Monday 24th, November 2014
This article is at the time of Kotlin 0.9.206, and some parts are likely to be quickly out of date.
Don't be overwhelmed by the number of steps, most of these are common sense, I'm just trying to be thorough.
This article is to get a Hello World, with unit tests, up and running in Kotlin for the browser. At the time of this article, there is only one somewhat outdated tutorial I could find: writing-kotlin-in-the-browser so this is an attempt to fill in some of the gaps.
This article was written using IntelliJ IDEA 14.
Note: If you are upgrading IDEA and previously had the plugin, be sure to uninstall the Kotlin plugin and get the latest version.
File > Settings > Plugins > Browse Repositories
File > New Project
You should have an empty project now that looks something like this:
Inside the lib folder you should have the kotlin-jslib.jar header files, inside the script folder there should be a runtime kotlin.js file.
Copy the kotlin.js file from your scripts folder to your web/js/lib folder.
Right click your src directory, New > Kotlin File
We need a package-level main function. This is our entry point.
At this point, we could compile, but we'd have no way to run the application. We need to set up the output path to go to our js/app folder.
File > Project Structure > Project Settings > Modules (Your module) > Paths Tab
Set the output path to your web/js/app folder.
Create a new file in your web folder called hello.html
Now we create a run configuration to tell IntelliJ to run your application.
Click the Edit Configurations button
Select the html file we created in Step 10, and add Before Launch: Make
Run the launch configuration we just made.
At this point IntelliJ will try to install a debugging plugin in your browser if you've never done this before.
Install this extension so we can see logs in the IntelliJ console and debug our application.
Now when we run, we should see a browser notification that we are debugging with IntelliJ.
Optional: The way to disable this notification in Chrome, is go to chrome://flags and enable the silent debugging flag.
If all is well, 3 things should happen, your browser should open up (with a blank page), a .js file should be created in your web/js/app folder, and in the IntelliJ console we should see a "Hello World".
Phew! That was a lot of stuff. But we're not quite done yet!
What use is a fancy IDE if we can't set breakpoints and debug our errors! Here's how to do that.
We must enable source maps. To do this, go to File > Settings > Build, Execution, Deployment > Compiler > Kotlin Compiler and enable source maps.
Note: At the time of this article incremental compilation (experimental) causes problems. Don't check that.
At this point, we have a Hello World, we're able to set breakpoints, and we can see our output to the console.
There is one more piece that I consider to be critical toward trying out a new language, and that is Unit Testing.
Now I am not a Test Driven Development nut. I won't give you my opinions on what are the best frameworks or styles of testing to use, but I do like to write my unit tests alongside my application. I do this mainly because it simply means faster iteration time. I can focus on a single piece of code instead of an entire application.
Before we create a test, we need something simple to test. In your Hello.kt file let's create a simple class.
Next create a folder named "test", and then mark that directory as a test sources root.
Oops: This doesn't seem to work with the current Kotlin build. For now, set it to a regular sources root instead of test sources root. Hopefully this is fixed soon.
Create a new kotlin file in your test folder, choose "class", and call it "HelloTest".
Pay close attention to the imports. The "Test" annotation is what tells the test runner which methods are tests, and you must be sure the org.junit.Test class is imported for that to work.
Kotlin should work with many different kinds of test frameworks, but QUnit is the one I've tried. You can read about it here: QUnit JS
Change your html file to include the testing scripts.
I hope this helps people get started with Kotlin!