Skip to content

Commit

Permalink
Added unit testing using jest (#11)
Browse files Browse the repository at this point in the history
* Added unit testing using jest
* Added job to build and test using npm

Signed-off-by: Saurabh Kamat <[email protected]>

---------

Signed-off-by: Saurabh Kamat <[email protected]>
  • Loading branch information
sauk2 authored Jun 14, 2024
1 parent f221901 commit 0852966
Show file tree
Hide file tree
Showing 7 changed files with 4,261 additions and 1,099 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/build-and-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash
set -euxo pipefail

npm install -g codecov

# When running inside a Docker container, by default, we're root and all files belond to root.
# However, calling the npm scripts (build, etc.) with 'npm run ...' runs the commands as user
# ID=1001, which means we can't open or write to any files. Therefore, if we're in Docker, chown
# everything under /__w (which is the workspace directory on the host: /home/runner/work) to user
# ID=1001. See:
# * https://github.com/ros-tooling/setup-ros/pull/521
# * https://github.com/npm/cli/issues/4589
docker_workdir="/__w"
if [ -d "${docker_workdir}" ]; then
chown -R 1001:1001 "${docker_workdir}"
fi

npm ci
npm run build
npm test

# Upload code coverage to CodeCov, but do not fail the CI if CodeCov upload
# fails as the service is sometimes flaky.
codecov -f ./coverage/coverage-final.json || true
25 changes: 22 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,33 @@ on:
push:
branches:
- main
- "releases/*"
- 'releases/*'
schedule:
# Run the CI automatically twice per day to look for flakyness.
- cron: "0 */12 * * *"
- cron: '0 */12 * * *'
defaults:
run:
shell: bash
jobs:
test_action_linux:
name: 'Run unit test on action'
runs-on: ubuntu-latest
container:
image: ${{ matrix.docker_image }}
strategy:
fail-fast: false
matrix:
docker_image:
- ubuntu:focal
- ubuntu:jammy
- ubuntu:noble
steps:
- uses: actions/checkout@v4
- uses: actions/[email protected]
with:
node-version: '20.x'
- run: .github/workflows/build-and-test.sh

test_gazebo_install_ubuntu:
name: 'Check installation of Gazebo on Ubuntu'
runs-on: ubuntu-latest
Expand Down Expand Up @@ -45,7 +64,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/[email protected]
with:
node-version: "20.x"
node-version: '20.x'
- name: 'Check Gazebo installation on Ubuntu runner'
uses: ./
with:
Expand Down
70 changes: 70 additions & 0 deletions __test__/main.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import * as core from "@actions/core";
import * as exec from "@actions/exec";

import * as linux from "../src/setup-gazebo-linux";
import * as utils from "../src/utils";

describe("workflow test without input", () => {
beforeAll(() => {
jest.spyOn(exec, "exec").mockImplementation(jest.fn());
});

afterAll(() => {
jest.resetAllMocks();
});

it("run Linux workflow without", async () => {
await expect(linux.runLinux()).rejects.toThrow();
});
});

describe("workflow test with a invalid distro input", () => {
beforeAll(() => {
jest.spyOn(exec, "exec").mockImplementation(jest.fn());
jest.spyOn(core, "getInput").mockReturnValue("dome");
});

afterAll(() => {
jest.resetAllMocks();
});

it("run Linux workflow without", async () => {
await expect(linux.runLinux()).rejects.toThrow();
});
});

describe("workflow test with a valid distro input", () => {
beforeAll(() => {
jest.spyOn(exec, "exec").mockImplementation(jest.fn());
jest.spyOn(core, "getInput").mockReturnValue("harmonic");
});

afterAll(() => {
jest.resetAllMocks();
});

it("run Linux workflow without", async () => {
await expect(linux.runLinux()).resolves.not.toThrow();
});
});

describe("validate distribution test", () => {
it("test valid distro", async () => {
await expect(utils.validateDistro(["citadel"])).toBe(true);
await expect(utils.validateDistro(["fortress"])).toBe(true);
await expect(utils.validateDistro(["garden"])).toBe(true);
await expect(utils.validateDistro(["harmonic"])).toBe(true);
await expect(utils.validateDistro(["fortress", "garden"])).toBe(true);
});
it("test invalid distro", async () => {
await expect(utils.validateDistro(["acropolis"])).toBe(false);
await expect(utils.validateDistro(["blueprint"])).toBe(false);
await expect(utils.validateDistro(["dome"])).toBe(false);
await expect(utils.validateDistro(["edifice"])).toBe(false);
await expect(utils.validateDistro(["doesNotExist"])).toBe(false);
await expect(utils.validateDistro(["dome", "fortress"])).toBe(false);
await expect(utils.validateDistro(["citadel", "edifice", "harmonic"])).toBe(
false,
);
});
});
12 changes: 12 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
clearMocks: true,
moduleFileExtensions: ["js", "ts"],
testEnvironment: "node",
testMatch: ["**/*.test.ts"],
testRunner: "jest-circus/runner",
transform: {
"^.+\\.ts$": "ts-jest",
},
transformIgnorePatterns: ["^.+\\.js$"],
verbose: true,
};
Loading

0 comments on commit 0852966

Please sign in to comment.