eirslett / frontend-maven-plugin

"Maven-node-grunt-gulp-npm-node-plugin to end all maven-node-grunt-gulp-npm-plugins." A Maven plugin that downloads/installs Node and NPM locally, runs NPM install, Grunt, Gulp and/or Karma.
Apache License 2.0
4.26k stars 869 forks source link

ERRORS & BUILD FAILURE: When executing tests with karma #819

Open codehan-de opened 5 years ago

codehan-de commented 5 years ago

When I run mvn install within an Angular project, I get the following error messages when executing the unit tests:

[INFO] --- frontend-maven-plugin:1.7.6:karma (run tests) @ test-ui --- [INFO] Running 'karma start karma.conf.ci.js' in /Users/codehan/Documents/projects/test-ui [ERROR] [ERROR] /Users/codehan/Documents/projects/test-ui/node_modules/@angular-devkit/build-angular/src/angular-cli-files/plugins/packages/angular_devkit/build_angular/src/angular-cli-files/plugins/karma.ts:62 [ERROR] throw new Error(The '@angular-devkit/build-angular/plugins/karma' karma plugin is meant to + [ERROR] ^ [ERROR] Error: The '@angular-devkit/build-angular/plugins/karma' karma plugin is meant to be used from within Angular CLI and will not work correctly outside of it.

And some lines below

[INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 24.666 s [INFO] Finished at: 2019-05-21T14:31:09+02:00 [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal com.github.eirslett:frontend-maven-plugin:1.7.6:karma (run tests) on project test-ui: Failed to run task: 'karma start karma.conf.ci.js' failed. org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1) -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

I use the frontend-maven-plugin in the version 1.7.6 and have it inserted like this in my pom.xml.

  <build>
    <plugins>
      <!-- Frontend plugin -->
      <plugin>
        <groupId>com.github.eirslett</groupId>
        <artifactId>frontend-maven-plugin</artifactId>
        <version>1.7.6</version>
        <configuration>
          <nodeVersion>v11.10.0</nodeVersion>
        </configuration>
        <executions>
        ...

My karma.conf.js looks like this:

module.exports = function (config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', '@angular-devkit/build-angular'],
    plugins: [
      require('karma-jasmine'),
      require('karma-jasmine-html-reporter'),
      require('karma-coverage-istanbul-reporter'),
      require('@angular-devkit/build-angular/plugins/karma'),
      require('karma-chrome-launcher'),
      require('karma-phantomjs-launcher')
    ],
    client:{
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },
    coverageIstanbulReporter: {
      dir: require('path').join(__dirname, 'coverage'), 
      reports: [ 'html', 'lcovonly' ],
      fixWebpackSourcePaths: true
    },
    angularCli: {environment: 'dev'},
    reporters: ['progress', 'kjhtml'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    phantomjsLauncher: {
      // Have phantomjs exit if a ResourceError is encountered (useful if karma
      // exits without killing phantom)
      exitOnResourceError: true
    },    
    singleRun: false
  });
};

My karma.conf.ci.js like this:

var baseConfig = require('./karma.conf.js');

module.exports = function (config) {
  baseConfig(config);
  config.set({
    plugins: [
      require('karma-jasmine'),
      require('karma-jasmine-html-reporter'),
      require('karma-coverage-istanbul-reporter'),
      require('@angular-devkit/build-angular/plugins/karma'),
      require('karma-chrome-launcher')
    ],
    browsers: ['Chrome'],
    singleRun: true
  });
};

I use the following versions of the packages:

Angular: "@ angular-devkit / build-angular": "^0.12.4", "@angular / cli": "^7.3.9", "@angle / compiler-cli": "7.2.0", "@angular / core": "7.2.0", "@ angular-devkit / architect": "^0.13.9", "@ angular-devkit / build-optimizer": "^0.13.9", "@ angular-devkit / build-webpack": "^0.13.9", "@ angular-devkit / core": "^7.3.9",

Karma "karma": "^1.7.1", "karma-chrome-launcher": "~2.1.1", "karma-cli": "~1.0.1", "karma-coverage-istanbul-reporter": "^1.4.3", "karma-firefox-launcher": "^1.0.1", "karma-jasmine": "^1.1.2", "karma-jasmine-html-reporter": "^0.2.2", "karma-phantomjs-launcher": "^1.0.4", "karma mocha reporter": "^2.2.5", "karma-webpack": "^3.0.5",

Jasmine "@ types / jasmine": "2.5.45", "jasmine-core": "^2.99.1", "jasmine-spec-reporter": "~4.1.0",

Java Java version: 1.8.0_201

Maven Apache Maven 3.6.1

Operating system MacOS Mojave (Version 10.14.5)

IDE Visual Studio Code

What am I doing wrong?

eirslett commented 5 years ago

You could try to use the npm mojo, and npm run-script etc instead?

codehan-de commented 5 years ago

You could try to use the npm mojo, and npm run-script etc instead?

Can you be more precise? How should my current configuration be adjusted?

eirslett commented 5 years ago
<execution>
    <id>run karma tests</id>
    <goals>
        <goal>npm</goal>
    </goals>

    <phase>test</phase>

    <configuration>
        <arguments>run-script run-tests-via-angular</arguments>
    </configuration>
</execution>

package.json:

{
  "scripts": {
    "run-scripts-via-angular": "angular-ci some-karma-command"
  }
}
codehan-de commented 5 years ago
<execution>
    <id>run karma tests</id>
    <goals>
        <goal>npm</goal>
    </goals>

    <phase>test</phase>

    <configuration>
        <arguments>run-script run-tests-via-angular</arguments>
    </configuration>
</execution>

package.json:

{
  "scripts": {
    "run-scripts-via-angular": "angular-ci some-karma-command"
  }
}

It does not work that way either. Still the same error.

eirslett commented 5 years ago

What happens then, if you run npm run-script run-scripts-via-angular directly on the command line (without maven?)

eirslett commented 5 years ago

Maybe this works: https://github.com/angular/angular-cli/issues/10703#issuecomment-387319138

codehan-de commented 5 years ago

Maybe this works: angular/angular-cli#10703 (comment)

If I do: ng test --karma-config karma.conf.js this error message occurs:

[ERROR] Project 'karma.conf.js' does not support the 'test' target. [ERROR] Error: Project 'karma.conf.js' does not support the 'test' target.

          <execution>
            <id>test</id>
            <goals>
              <goal>npm</goal>
            </goals>
            <configuration>
              <arguments>run test --karma-config karma.conf.js</arguments>
            </configuration>
          </execution>
imtiazShakil commented 5 years ago

The new angular Client needs project name if you override the default karma configuration. so you've to run test like this:

ng test my-app --karma-config karma.conf.ci.js

So to run the same command using npm we would need command like this: npm test -- my-app --karma-config karma.conf.ci.js

[Notice: I added -- after npm test command, because it allows us to include package specific arguments (here it is --karma-config) ]

Finally to sum it up, here is the updated pom:

<execution>
    <id>run karma tests</id>
    <goals>
        <goal>npm</goal>
    </goals>
    <phase>test</phase>

    <configuration>
        <arguments>test -- my-app --karma-config karma.conf.ci.js</arguments>
    </configuration>
</execution>