Kindly suggest me the ways to bundle node js app with the test module and run tests before deploying to edge using maven.

Hi,

Kindly suggest me the ways to implement the following -

What do I need?

Need an approach to run test cases written in node js (using mocha and chai library) before deploying the proxy (+node js app) to apigee edge.

What do I have?

API proxy(+node js app) - which is deployable using mvn.

Test Module (written in node js as a seperate module) - Functional Testing Module.

What am I looking for?

An approach to integrate the above mentioned modules and run the process synchronously -

Build API Proxy + Run Test Cases + Deply API Proxy on apigee edge.

Solved Solved
1 3 588
1 ACCEPTED SOLUTION

Added the following build step to make it work -

	     <build>      
                <plugins>
                        <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>test-nodejs</id>
                                <phase>test</phase>
                                <configuration>
                                        <tasks name="Run mocha tests">
                                            <exec 
						dir="${basedir}" 
						executable="npm" 
						failonerror="true">
                                              	<arg value="test"/>
                                            </exec>
                                        </tasks>
                                </configuration>
                                 <goals>
                                    <goal>run</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin> 
                </plugins>
            </build>

Maven-Antrun-Plugin lets you run Ant tasks.

I have used maven-antrun-plugin for running the test cases as a build step.

 <exec 
	dir="${basedir}" 
	executable="npm" 
	failonerror="true">
 	<arg value="test"/>
</exec>

The above configuration is used to execute a command -

(Multiple Executions can be added)

  • dir - the directory where the command shall be executed
  • executable - specifies the executable command
  • arg - specifies argument values in the value attribute
  • failonerror - boolean value to abort or otherwise when encountered with an error

Let me know if any other approach works ( the above one is tested ),

Hope this helps,

Thank You

View solution in original post

3 REPLIES 3

Hi @Nisha Mallesh

Maven runs on a build lifecycle (ordered) - more info here

You can do that be specifying those execution with "validate" or "test" as the phase. For example

<!-- run unit tests -->
<execution>
	<id>unit</id>
	<phase>validate</phase>
	<goals>
		<goal>exec</goal>
	</goals>
	<configuration>
		<executable>node</executable>
		<commandlineArgs>
			node_modules/istanbul/lib/cli.js cover node_modules/mocha/bin/_mocha test/unit
		</commandlineArgs>
	</configuration>
</execution>


<!-- Check Code Coverage-->
<execution>
	<id>check-coverage</id>
	<phase>validate</phase>
	<goals>
		<goal>exec</goal>
	</goals>
	<configuration>
		<executable>node</executable>
		<commandlineArgs>
			node_modules/istanbul/lib/cli.js check-coverage
		</commandlineArgs>
	</configuration>
</execution>

This will execute the tests first and then the deployment happens. The above is an example of unit testing through Mocha and code coverage using Istanbul

Thanks @Sai Saran Vaidyanathan, I will try this and get back to you.

Added the following build step to make it work -

	     <build>      
                <plugins>
                        <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>test-nodejs</id>
                                <phase>test</phase>
                                <configuration>
                                        <tasks name="Run mocha tests">
                                            <exec 
						dir="${basedir}" 
						executable="npm" 
						failonerror="true">
                                              	<arg value="test"/>
                                            </exec>
                                        </tasks>
                                </configuration>
                                 <goals>
                                    <goal>run</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin> 
                </plugins>
            </build>

Maven-Antrun-Plugin lets you run Ant tasks.

I have used maven-antrun-plugin for running the test cases as a build step.

 <exec 
	dir="${basedir}" 
	executable="npm" 
	failonerror="true">
 	<arg value="test"/>
</exec>

The above configuration is used to execute a command -

(Multiple Executions can be added)

  • dir - the directory where the command shall be executed
  • executable - specifies the executable command
  • arg - specifies argument values in the value attribute
  • failonerror - boolean value to abort or otherwise when encountered with an error

Let me know if any other approach works ( the above one is tested ),

Hope this helps,

Thank You