Grails application with AngularJS: Karma with test-app – Part 3

In Part 2, we added karma test runner to our AnGrails application. Its great that you can test your javascripts on the fly locally. What about testing it as part of your grails test-app lifecycle ?

Step: Setup JUnit/Karma integration so that karma can run via grails test-app

Edit BuildConfig.groovy

Add the following line under plugins

test ':karma-test-runner:0.2.0'

See karma-test-runner grails plugin for more information.

Create new file c:/projects/angrails/test/unit/angrails/JavaScriptUnitTestKarmaSuite.java

Add the following text:

package angrails;
import de.is24.util.karmatestrunner.junit.KarmaTestSuiteRunner;
import org.junit.runner.RunWith;
@RunWith(KarmaTestSuiteRunner.class)
@KarmaTestSuiteRunner.KarmaConfigPath("./test/javascript/config/karma.conf.js")
@KarmaTestSuiteRunner.KarmaRemoteServerPort(9876)
public class JavaScriptUnitTestKarmaSuite {
}

Run the test.

cmd> grails test-app

| Compiling 1 source files.....
Karma will be started with process builder args: [karma.cmd, start, C:\projects\angrails\.\test\javascript\config\karma.conf.js]
| Running 1 javascript test...
Starting karma result receiver server on localhost:9889
Warning: Native modules not compiled. XOR performance will be degraded.
INFO [karma]: Karma v0.10.9 server started at http://localhost:8001/
Warning: Native modules not compiled. UTF-8 validation disabled.
INFO [launcher]: Starting browser Chrome
INFO [Chrome 33.0.1750 (Windows 7)]: Connected on socket yX5XSRbnYl_IzwQp7ocG
LOG: 'angrails manifest load complete.'
LOG: 'calling'
Chrome 33.0.1750 (Windows 7): Executed 1 of 1 SUCCESS (0.248 secs / 0.03 secs)

Issues

If one of the following errors occur:

Could not load class in test type ‘javascript’
ERROR [karma]: { [Error: listen EACCES] code: ‘EACCES’, errno: ‘EACCES’, syscall: ‘listen’ }

There is some issue with how the karma is being started via test-app. I found both cmd line grails test-app and invoking via IntelliJ grails cmd window (Alt+G) yield different results. The common problem of both errors seems to be the port number that karma starts on.

Solutions

1. Change the port number in karma.conf.js to 8001 or try another number (above 1024 and available).
2. Remove line of the port number specified in JavaScriptUnitTestKarmaSuite.java: @KarmaTestSuiteRunner.KarmaRemoteServerPort(8001)

Results

With the above settings (ie karma.conf.js:port = 8001 and no port specified in the java code), the test-app works fine in both IntelliJ and command line.

During your test runs, sometimes the port binding is not released when test is shutdown. On *nix systems, you can just use kill <process-id>. For Windows, use PowerShell and do the following:

netstat -o -n -a | findstr "<portnumber>"
stop-process -Id <pid>

In Part 3, we saw how to add karma to grails test-app lifecycle.
In Part 4, lets see how to get karma tests running on your build server.

Advertisements

6 Responses to Grails application with AngularJS: Karma with test-app – Part 3

  1. asoftwareguy says:

    This is great!

    Have you dealt yet with interfacing AngularJS controllers controlling authentication to Grails Spring Security on the back end?

    Like

  2. Hi vasya,
    I’m following your tutorial to make a first grails app with angularJs as frontend framework.

    I run the test succesfully with command “karma start karma.conf.js” but I’m facing this problem when I run test with command “grails test-app”.

    | Running without daemon…
    | Error Error loading event script from file [/media/Data/Users/Simone/Projects/Grails/MyRecipes/target/work/plugins/asset-pipeline-1.6.1/scripts/_Events.groovy] No such property: assetPipelinePluginDir for class: _Events (Use –stacktrace to see the full trace)
    | Running Grails application
    | Server running. Browse to http://localhost:8080/MyRecipes
    | Waiting for server availablility
    | Waiting for server availablility.
    | Waiting for server availablility..
    | Waiting for server availablility…
    | Waiting for server availablility….
    | Waiting for server availablility…..
    | Server stopped
    | Tests PASSED – view reports in /media/Data/Users/Simone/Projects/Grails/MyRecipes/target/test-reports

    I installed karma-test-runner:0.2.0 as Karma plugin for Grails at version 2.3.7

    Could be an incompatibility between asset-pipeline:1.6.1 and karma-test-runner:0.2.0 plugins?

    Thanks,
    Simone.

    Like

  3. vasya10 says:

    @Simone, i have seen the “No such property” in some scenarios. Can you try the following:

    1. does the error happen if you run test-app within an ide? (IDEA or Eclipse)?
    2. can you delete your target/* and do a refresh-dependencies and then test-app?
    3. you can also try updating to latest asset-pipeline which is at 1.8.1
    4. in one of my earlier versions, a similar problem existed while using clover plugin – i commented out the clover plugin in buildconfig to avoid this error
    5. after excluding resources and including asset-pipeline in buildconfig, did you do a refresh-dependencies ?

    Like

  4. Hi Vasya.

    I have set up every thing accordingly and refreshed dependencies multiple times.
    But getting the following error. can you please

    Starting karma result receiver server on localhost:8001
    | Failure: angrails.JavaScriptUnitTestKarmaSuite
    | java.lang.NoClassDefFoundError: org/eclipse/jetty/server/Server
    at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at de.is24.util.karmatestrunner.JSTestExecutionServer.beforeTests(JSTestExecutionServer.java:26)
    at de.is24.util.karmatestrunner.junit.KarmaTestSuiteRunner$3.evaluate(KarmaTestSuiteRunner.java:186)
    at de.is24.util.karmatestrunner.junit.KarmaTestSuiteRunner$2.evaluate(KarmaTestSuiteRunner.java:168)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runners.Suite.runChild(Suite.java:127)
    at org.junit.runners.Suite.runChild(Suite.java:26)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at de.is24.grails.tools.KarmaTestType.doRun(KarmaTestType.groovy:52)
    Caused by: java.lang.ClassNotFoundException: org.eclipse.jetty.server.Server
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    … 22 more
    | Completed 0 javascript test, 1 failed in 0m 0s

    Like

  5. Stephen Chow says:

    Hi,

    I got the FAILED result when I do grails test-app unit:javascript. I setup a new project with just 1 sample test from your example. Any comment?

    | Running 1 javascript test…
    Starting karma result receiver server on localhost:9889
    INFO [karma]: Karma v0.12.16 server started at http://localhost:9876/
    INFO [launcher]: Starting browser PhantomJS
    INFO [PhantomJS 1.9.7 (Mac OS X)]: Connected on socket lDRjk7SIG3fQ3Dq_O2ZA with id 86099453
    PhantomJS 1.9.7 (Mac OS X) LOG: ‘angrails manifest load complete.’

    PhantomJS 1.9.7 (Mac OS X): Executed 1 of 1 SUCCESS (0.004 secs / 0.013 secs)
    Program terminated!
    | Completed 0 javascript test, 1 failed in 0m 2s
    | Tests FAILED – view reports in

    Like

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: