Unit Test javascript files in AEM | Community
Skip to main content
FernandoUchiyam
Level 3
Jan 22, 2019 3:48 PM UTC
Solved

Unit Test javascript files in AEM

Hi guys,

I have a requirement here in my company that we have to do unit tests on our Javascript files. What do you recommend? (we are already unit testing Java Sling Models, now we want to test javascript code).

When I say Javascript I mean the JS files that are inside Clientlibs of custom developed Components. We want to do 100% coverage, so we need some way to build a report coverage too.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Gaurav-Behl
Gaurav-BehlAccepted solution
Level 10
Jan 22, 2019 5:05 PM UTC

My bad, I probably overlooked javascript keyword

You can use any industry standard js unit testing tools with AEM, e.g. karma/jasmine etc.

Use frontend-maven-plugin to integrate it with node/npm  or  About Node.js Maven Plugin | Node.js Maven Plugin

check -

GitHub - eirslett/frontend-maven-plugin: "Maven-node-grunt-gulp-npm-node-plugin to end all maven-node-grunt-gulp-npm-plu…

e.g. add this to pom.xml

<execution>

  <id>javascript tests</id>

  <goals>

  <goal>karma</goal>

  </goals>

   <!-- optional: the default plase is "test". Some developers

  choose to run karma in the "integration-test" phase. -->

  <phase>test</phase>

  <configuration>

   <!-- optional: the default is "karma.conf.js" in your working directory -->

  <karmaConfPath>src/test/javascript/karma.conf.ci.js</karmaConfPath>

  </configuration>

</execution>

5 replies

BrianKasingli
New Participant
Oct 17, 2019 6:10 PM UTC

Yes, it is possible to run JavaScript unit test directly on the logic that is written in your client library. Utilising tools like Jest and the Maven Front end Plugin, these tools will enable you to write tests.

This is great because you can write tests for custom touch ui dialogue hooks, etc...

examples shows here: https://sourcedcode.com/how-to-write-javascript-unit-tests-for-aem-client-libraries-with-jest

Translate
Gaurav-Behl
Gaurav-BehlAccepted solution
Level 10
Jan 22, 2019 5:05 PM UTC

My bad, I probably overlooked javascript keyword

You can use any industry standard js unit testing tools with AEM, e.g. karma/jasmine etc.

Use frontend-maven-plugin to integrate it with node/npm  or  About Node.js Maven Plugin | Node.js Maven Plugin

check -

GitHub - eirslett/frontend-maven-plugin: "Maven-node-grunt-gulp-npm-node-plugin to end all maven-node-grunt-gulp-npm-plu…

e.g. add this to pom.xml

<execution>

  <id>javascript tests</id>

  <goals>

  <goal>karma</goal>

  </goals>

   <!-- optional: the default plase is "test". Some developers

  choose to run karma in the "integration-test" phase. -->

  <phase>test</phase>

  <configuration>

   <!-- optional: the default is "karma.conf.js" in your working directory -->

  <karmaConfPath>src/test/javascript/karma.conf.ci.js</karmaConfPath>

  </configuration>

</execution>

Translate
smacdonald2008
Level 10
Jan 22, 2019 4:57 PM UTC

To learn more about AEM Testing - see this video

Translate
FernandoUchiyam
Level 3
Jan 22, 2019 4:51 PM UTC

Hi, Hobbes.js is a tool for UI Testing.

I would like to know more about Unit Test.

Translate
Gaurav-Behl
Level 10
Jan 22, 2019 3:57 PM UTC

For js testing, check -

Hobbes.js Index

Testing Your UI

you could also use npm package for node integration, if applicable - hobbes - npm

Translate