8

I have a Kotlin JavaScript project in IntelliJ (Ultimate). I want to be able to write tests for this project.

I have tried a number of different things ranging from writing tests with Spek (this would be ideal) to writing them in Karma/Jasmine. The problem with Spek is that I could not get it to work with a JavaScript project. It complains about some jars not being a JavaScript library.

The problem with Karma/Jasmine is that I couldn't figure out how to get the test runner to pick up the tests from my Kotlin test output. When the tests are written in plain JavaScript, they can't access my Kotlin objects because I don't know how to reference them correctly with all of the name mangling that occurs when you do Kotlin to JavaScript compilation.

I'm open to anything at the moment, since this is an Angular project, I can probably figure out how to do end-to-end testing with Protractor, though I would really prefer some unit test coverage.

2
  • 1
    I guess my question is why not write the tests in Kotlin...? IMHO you should leave testing of proper conversion up to jetbrains. Commented Feb 17, 2015 at 5:23
  • 4
    I would love to write my tests in Kotline. Unfortunately, I have not been able to find a JavaScript test runner that can locate and execute Kotlin-to-JavaScript tests. For Java, standard (JUnit) test runners can locate tests in the code generated by Kotlin-to-Java, so the issue is JavaScript specific. Commented Feb 17, 2015 at 16:37

1 Answer 1

6

Below You can find simple test which I write using QUnit. I believe that You can do the same for other libraries. Moreover it can be improved by making it more type safe.

main.kt:

native("QUnit") val qunit: dynamic
val assert = qunit.assert

fun main(args: Array<String>) {

    qunit.test( "hello test")  { assert ->
        val t: Any = "1"
        assert.ok(1 == t, "Passed!")
    }

    qunit.test( "hello test")  {
        assert.ok(1 == 1, "Passed!")
    }
}

runner.html:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>QUnit Example</title>
    <link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-1.17.1.css">
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="https://code.jquery.com/qunit/qunit-1.17.1.js"></script>
<script src="lib/kotlin.js"></script>
<script src="js_test.js"></script>
</body>
</html>
Sign up to request clarification or add additional context in comments.

1 Comment

I got the basic test working in IntelliJ, thanks. I am currently stuck trying to figure out how to write a test that references my actual code. I have my Kotlin source code in my src directory and my Kotlin test code in my tests directory. This results in a out/production/MyProject/MyProject.js and out/test/MyProject/MyProject.js files being generated. However, when I try to reference my production code from my test code, Kotlin stops compiling, complaining about being unable to resolve the reference to my library package. :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.