#Making tests with Jest

1 messages ยท Page 1 of 1 (latest)

late spoke
#

Hi! I was wondering if someone has created some tests using the Node SDK + Jest.

In Jenkins, where I have a Groovy library with utilities to install/use tools in a pipeline, I have tried many times to create a good solution to run integration tests over some of those functions. At the end, this is not possible/real/worth the effort as Jenkins does run Groovy in a serialized way + Jenkins plugins are not available locally.
One of the things that I was hopping to do with Dagger was to be able to finally create this integrations tests, but jest is not playing well with dagger. I am not very familiar with the node ecosystem, so this may be because I'm doing some weird stuff because of my lack of knowledge.

This is the test I have created:

import { describe, expect, test } from "@jest/globals"
import { connect, Client } from "@dagger.io/dagger"

import { withSdkman } from "./install.mjs"

describe("test sdk installation", () => {
  test("sdk directory exists after installation", async () => {
    connect(async (client: Client) => {
      let container = client.container().from("ubuntu:20.04")
      container = (await withSdkman(client, container)).withExec(
        "ls /root/.sdkman/sdkman".split(" "),
      )
      await container.stdout()
      expect(0).toBe(0)
    })
  })
})

this is the function I am trying to test:

export async function withSdkman(
  client: Client,
  container: Container,
  with_cache: Boolean = false,
): Promise<Container> {
  if (with_cache) {
    const sdkman_cache = client.cacheVolume(SDKMAN_CACHE)
    container.withMountedCache(SDKMAN_PATH, sdkman_cache)
  }

  container = container
    .withExec("apt-get update".split(" "))
    .withExec("apt-get install -y zip unzip wget curl".split(" "))
    .withEnvVariable("SDKMAN_DIR", `${SDKMAN_PATH}/sdkman`)
    .withExec(["bash", "-c", "curl -s https://get.sdkman.io | bash"])

  return container
}
#

When I run node --experimental-vm-modules node_modules/jest/bin/jest.js the test passes instantly, but then gets stuck as I imagine dagger is running behind the scenes?

somber path
#

One of the things that I was hopping to do with Dagger was to be able to finally create this integrations tests,

This is the dream!

Would you be open to sharing whatever repo you are doing this in? I dont have an immediate answer, but would be great to try to hack on some things locally myself and try to debug this a bit.

late spoke
zenith crystal
#

we'll make it work @late spoke ๐Ÿ’ช

#

@late spoke node --experimental-vm-modules node_modules/jest/bin/jest.js doesn't seem to be running the test.mts test as I think has a different extension

#

how are you hinint jest to run that file?

#
130|marcos:tmp/dagger-lib (jest) (โŽˆ |N/A)$ node --experimental-vm-modules node_modules/jest/bin/jest.js
No tests found, exiting with code 1
Run with `--passWithNoTests` to exit with code 0
In /tmp/dagger-lib
  5 files checked.
  testMatch: **/__tests__/**/*.[jt]s?(x), **/?(*.)+(spec|test).[tj]s?(x) - 0 matches
  testPathIgnorePatterns: /node_modules/ - 5 matches
  testRegex:  - 0 matches
Pattern:  - 0 matches
late spoke
#

You are right, misconfiguration here. I thought that I was running the test.mts, but I was running the compiled js test generated in the dist directory. I guess that running npx tsc should compile and allow you to run the compiled test.

zenith crystal
#

same thing since npx tsc generates an mjs

#

and jest doesn't recognize that pattern

brazen crane
#

@late spoke this is a nice simple example. Might help?

#

Uses a jest test and a service container for a test with a db

zenith crystal
#

@late spoke I'm blocked since I can't make jest or ts-jest to discover that file and run the test. LMK if you fix it so we have a way to run it ๐Ÿ™

late spoke
#

@zenith crystal , sorry for your waste of time. I am still being used to node + ts and its not overwhelming at all ecosystem ๐Ÿ˜…

#

I updated the configuration and pushed the changes, jest will be able to catch the test when running node --experimental-vm-modules node_modules/jest/bin/jest.js

zenith crystal
#

@late spoke this fixes your issue:

describe("test sdk installation", () => {
  test("sdk directory exists after installation", done => {
    connect(async (client: Client) => {
      let container = client.container().from("ubuntu:20.04")
      container = (await withSdkman(client, container)).withExec(
        "ls /root/.sdkman/sdkman".split(" "),
      )
      await container.stdout()
      done();
    }, { LogOutput: process.stderr })
  })
})
#

you'll have to incerase the --testTimeout since the default 5s is not enough

late spoke
#

Oh, I see. I will try it out. Many thanks!

zenith crystal
#

sure! let us know how it goes!