Maven在构建期间运行Angular测试。

huangapple go评论74阅读模式
英文:

Maven run Angular tests during build

问题

我有一个基于Maven的项目,后端使用Spring,前端使用Angular 8,我想在构建过程中自动运行Angular单元测试。

我已经阅读到通常使用PhantomJS,但它不再维护,所以我想看到一个完整的,即使是基本的示例,演示如何在构建过程中运行Angular测试。

我也听说有Headless Chrome可用,但我不清楚如何连接所有组件以运行测试。

请问有人能提供给我一个示例吗?

英文:

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?

答案1

得分: 1

要运行 Angular 单元测试,你应该运行:

ng test

它会自动下载你需要的工具(包括 Chrome Headless)。更多信息可以在这里找到:https://angular.io/guide/testing。

如果你必须从 Maven 运行这个命令,你可以使用

exec-maven-plugin

例如,参考:https://stackoverflow.com/questions/3491937/i-want-to-execute-shell-commands-from-mavens-pom-xml

但是最好将前端和后端的构建分开,并将它们视为独立的构建组件。

英文:

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: https://stackoverflow.com/questions/3491937/i-want-to-execute-shell-commands-from-mavens-pom-xml

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

答案2

得分: 1

以下是翻译好的部分:

<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>
英文:

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>

huangapple
  • 本文由 发表于 2020年1月6日 17:42:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609766.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定