1

I have a Maven based project with Spring in the backend and Angular 8 in the frontend, I would like to run the Angular unit tests automatically during the build.

I have read that usually PhantomJS was used, but it is not maintained anymore, so I would like to see a complete, even though basic, example of an application that is able to run Angular tests during the build.

I have also read that there is headless chrome available, but it is not clear to me how to wire things to make the tests run.

Does anyone can provide me with an example, please?

Pampa Nello
  • 206
  • 5
  • 16

2 Answers2

1

To run angular unit test you should run:

ng test

It will automatically download tools you need (including chrome headless). More information can be found here: https://angular.io/guide/testing.

If you must run this from maven, you can use the

exec-maven-plugin

See for example: I want to execute shell commands from Maven's pom.xml

But you are better of separating the front-end and back-end build and consider them individual artifacts.

Sander Sluis
  • 160
  • 7
  • If I run "ng test", it will wait to open a browser session. I am looking for a complete example, as mentioned in the question. – Pampa Nello Jan 06 '20 at 10:00
  • You should first get your tests to run stand alone, without maven. If 'ng test' is waiting for a browser session, you most likely have a configuration problem. Check ' karma.conf.js'. – Sander Sluis Jan 07 '20 at 09:57
1

This is what I ended to do:

<build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.3.2</version>
                <executions>
                    <execution>
                        <id>karma</id>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <phase>test</phase>
                        <configuration>
                            <executable>npm</executable>
                            <arguments>
                                <argument>test</argument>
                                <argument>--</argument>
                                <argument>--watch=false</argument>
                            </arguments>
                            <skip>${skipTests}</skip>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
Pampa Nello
  • 206
  • 5
  • 16