When a Java application needs a library, you declare a dependency in build.gradle or pom.xml to allow the build tool to handle both obtaining the JAR file you depend on and all the necessary transitive dependencies. The same applies to a node.js application; you need a module, you add a dependency and npm handles the rest. However, npm doesn’t currently resolve Maven dependencies (at least I don’t know if it can do it), so we need to prepare a JAR file with all the necessary classes.

Clone the example Java application that uses Testcontainers and build an uberjar that contains Testcontainers and all its dependencies.



cd testcontainers-prod-src-example

./gradlew shadowJar

ls build/libs/testcontainers-prod-src-example-1.0-SNAPSHOT-all.jar git clone git@github.com :kiview/testcontainers-prod-src-example.gitcd testcontainers-prod-src-example./gradlew shadowJarls build/libs/testcontainers-prod-src-example-1.0-SNAPSHOT-all.jar

Now we need a runtime that is capable of running a node application and a Java application. GraalVM fulfils these requirements perfectly. Download it and ensure that both java and node commands work.

Now it’s time to create a Node.js app from which we’ll use the full power of Testcontainers!

Here’s my app/index.js file that uses GraalVM polyglot API call Java.type() to access the Java class org.testcontainers.containers.GenericContainer .

It will return us the type for the class we asked for and afterwards we can use it as any normal JavaScript object.

We will create a new container with the nginx image, expose it on port 80 , start it, and print some information about the container, showing we can access it from the outside. We then just wait for some command line input before shutting everything down.

Using test-containers library from Node.js (on GraalVM)

If you have Docker running on your machine and run this command:

→ node --jvm --vm.cp=testcontainers-prod-src-example/build/libs/testcontainers-prod-src-example-1.0-SNAPSHOT-all.jar app/index.js



It will start a Docker container with nginx and you can access it locally. Magnificent, isn’t it?

Nginx output from a docker container started by TestContainers from a Node.js app

Now if your Node application depends on any infrastructure, like a data store or some other service, you can easily spin it up during tests in a docker container. There are a bunch of specialized containers prepared by the Testcontainers team: databases, Selenium, Kafka, Neo4j, etc. And you can always programmatically define custom ones.

While we’re at it, you can do the same trick and use the same library from, for example Python. Start the Python:

→ graalpython --jvm --vm.cp=testcontainers-prod-src-example/build/libs/testcontainers-prod-src-example-1.0-SNAPSHOT-all.jar

Run the following snippet which is a very close translation of the JavaScript code above:

Using test-containers library from Python (on GraalVM)

And you have the nginx container up and running.

Naturally, you can find more appropriate use cases for the test-containers library, for example, actually managing the environment for your integration tests, but in this blog post we just wanted to highlight that you can use it from all languages supported on GraalVM very easily.

Okay, I feel like I can do one more without losing your attention. Here’s Ruby!