Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can't load angular.js for unit test with karma, jasmine, phantomjs, and maven

I'm trying to arrange my alphabet soup of angular, karma, jasmine, phantomjs, and maven. I'm doing this with a toy project for now, in preparation for integrating into a real project.

The following is my relevant list of files:

./pom.xml
./src/main/webapp/index.html
./src/main/webapp/js/blog.js
./src/test/webapp/js/blogSpec.js
./src/test/webapp/js/karma.conf.js
./src/test/webapp/js/libs/angular-mocks.js
./src/test/webapp/js/libs/angular.js

Here is my complete pom:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.att.cdits</groupId>
  <artifactId>angularjsexp</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <build>
    <plugins>
      <plugin>
        <groupId>com.kelveden</groupId>
        <artifactId>maven-karma-plugin</artifactId>
        <version>1.5</version>
        <executions>
          <execution>
            <goals>
              <goal>start</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
            <configFile>${basedir}/src/test/webapp/js/karma.conf.js</configFile>
            <browsers>PhantomJS</browsers>
            <reporters>dots</reporters>
            <colors>false</colors>
            <autoWatch>false</autoWatch>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
        <groupId>com.kelveden</groupId>
        <artifactId>maven-karma-plugin</artifactId>
        <version>1.5</version>
    </dependency>
  </dependencies>
</project>

And my "karma.conf.js" file:

module.exports = function(config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine'],
    files: [
      "../../../../src/test/webapp/js/libs/*.js",
      "../../../../src/main/webapp/js/*.js",
      "../../../../src/test/webapp/js/*.js"
    ],
    exclude: [],
    reporters: ['dots'],
    port: 9876,
    colors: false,
    logLevel: config.LOG_DEBUG,
    autoWatch: false,
    browsers: ['PhantomJS'],
    captureTimeout: 60000,
    singleRun: true
  });
};

The following is an excerpt from my "mvn install" output, starting at the Surefire execution:

[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ angularjsexp ---
[INFO] Surefire report directory: myhome\workspace6\angularjsexp\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO] 
[INFO] --- maven-karma-plugin:1.5:start (default) @ angularjsexp ---
cmd /C karma start myhome\workspace6\angularjsexp\src\test\webapp\js\karma.conf.js --browsers PhantomJS --reporters dots --single-run --no-auto-watch --colors false
DEBUG [plugin]: Loading karma-* from myhome\AppData\Roaming\npm\node_modules 
DEBUG [plugin]: Loading plugin myhome\AppData\Roaming\npm\node_modules/karma-chrome-launcher. 
DEBUG [plugin]: Loading plugin myhome\AppData\Roaming\npm\node_modules/karma-coffee-preprocessor. 
DEBUG [plugin]: Loading plugin myhome\AppData\Roaming\npm\node_modules/karma-firefox-launcher. 
DEBUG [plugin]: Loading plugin myhome\AppData\Roaming\npm\node_modules/karma-html2js-preprocessor. 
DEBUG [plugin]: Loading plugin myhome\AppData\Roaming\npm\node_modules/karma-jasmine. 
DEBUG [plugin]: Loading plugin myhome\AppData\Roaming\npm\node_modules/karma-phantomjs-launcher. 
DEBUG [plugin]: Loading plugin myhome\AppData\Roaming\npm\node_modules/karma-requirejs. 
DEBUG [plugin]: Loading plugin myhome\AppData\Roaming\npm\node_modules/karma-script-launcher. 
DEBUG [plugin]: Loading inlined plugin (defining ). 
INFO [karma]: Karma v0.10.2 server started at http://localhost:9876/ 
INFO [launcher]: Starting browser PhantomJS 
DEBUG [launcher]: Creating temp dir at C:\Cygwin\tmp\karma-77302282 
DEBUG [launcher]: myhome\AppData\Roaming\npm\node_modules\karma-phantomjs-launcher\node_modules\phantomjs\lib\phantom\phantomjs.exe C:\Cygwin\tmp\karma-77302282/capture.js 
DEBUG [watcher]: Excluded file "myhome/workspace6/angularjsexp/src/test/webapp/js/karma.conf.js" 
DEBUG [watcher]: Resolved files: 
    myhome/AppData/Roaming/npm/node_modules/karma-jasmine/lib/jasmine.js 
    myhome/AppData/Roaming/npm/node_modules/karma-jasmine/lib/adapter.js 
    myhome/workspace6/angularjsexp/src/test/webapp/js/libs/angular-mocks.js 
    myhome/workspace6/angularjsexp/src/test/webapp/js/libs/angular.js 
    myhome/workspace6/angularjsexp/src/main/webapp/js/blog.js 
    myhome/workspace6/angularjsexp/src/main/webapp/js/checkbox-array.js 
    myhome/workspace6/angularjsexp/src/main/webapp/js/my-include.js 
    myhome/workspace6/angularjsexp/src/test/webapp/js/blogSpec.js 
DEBUG [web-server]: serving: myhome\AppData\Roaming\npm\node_modules\karma\static/client.html 
DEBUG [web-server]: serving: myhome\AppData\Roaming\npm\node_modules\karma\static/karma.js 
DEBUG [karma]: A browser has connected on socket LI1NDCsuY-0i0Uxwkhs_ 
INFO [PhantomJS 1.9.2 (Windows 7)]: Connected on socket LI1NDCsuY-0i0Uxwkhs_ 
DEBUG [karma]: All browsers are ready, executing 
DEBUG [web-server]: serving: myhome\AppData\Roaming\npm\node_modules\karma\static/context.html 
DEBUG [web-server]: serving: myhome/AppData/Roaming/npm/node_modules/karma-jasmine/lib/adapter.js 
DEBUG [web-server]: serving: myhome/AppData/Roaming/npm/node_modules/karma-jasmine/lib/jasmine.js 
DEBUG [web-server]: serving: myhome/workspace6/angularjsexp/src/test/webapp/js/libs/angular.js 
DEBUG [web-server]: serving: myhome/workspace6/angularjsexp/src/main/webapp/js/checkbox-array.js 
DEBUG [web-server]: serving: myhome/workspace6/angularjsexp/src/main/webapp/js/blog.js 
DEBUG [web-server]: serving: myhome/workspace6/angularjsexp/src/main/webapp/js/my-include.js 
DEBUG [web-server]: serving: myhome/workspace6/angularjsexp/src/test/webapp/js/blogSpec.js 
DEBUG [web-server]: serving: myhome/workspace6/angularjsexp/src/test/webapp/js/libs/angular-mocks.js 
PhantomJS 1.9.2 (Windows 7) ERROR 
    ReferenceError: Can't find variable: angular 
    at myhome/workspace6/angularjsexp/src/test/webapp/js/libs/angular-mocks.js:16 
PhantomJS 1.9.2 (Windows 7) ERROR 
    ReferenceError: Can't find variable: myApp 
    at myhome/workspace6/angularjsexp/src/main/webapp/js/checkbox-array.js:1 
PhantomJS 1.9.2 (Windows 7) ERROR 
    ReferenceError: Can't find variable: myApp 
    at myhome/workspace6/angularjsexp/src/main/webapp/js/my-include.js:1 
PhantomJS 1.9.2 (Windows 7) ERROR 
    ReferenceError: Can't find variable: module 
    at myhome/workspace6/angularjsexp/src/test/webapp/js/blogSpec.js:5 
PhantomJS 1.9.2 (Windows 7): Executed 0 of 0 ERROR (0.273 secs / 0 secs) 

I have the following issues so far, some annoyances, some blockers: * It's weird that karma resolves file path references relative to where it found my conf file, I wish I could override that. * It appears that Surefire fires before Karma. Is it possible to integrate these? * My references to "angular" and other objects in my scripts are obviously failing. I don't understand how to resolve this.

like image 281
David M. Karr Avatar asked Nov 30 '22 20:11

David M. Karr


1 Answers

Thanks to someone on the karma-users discussion group, I discovered that the files loaded from "files" have to be loaded in the correct order. For instance, instead of "../../../../src/test/webapp/js/libs/*.js", I need to specify "../../../../src/test/webapp/js/libs/angular.js", then "../../../../src/test/webapp/js/libs/angular-mocks.js".

I still have to figure out how to properly integrate Surefire, but I'll ask that separately.

like image 179
David M. Karr Avatar answered Dec 04 '22 10:12

David M. Karr