简体   繁体   中英

Maven run Angular tests during build

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?

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM