Skip to content

Commit

Permalink
ci: Add unit testing infra: (#115)
Browse files Browse the repository at this point in the history
- Add config for the `jest` unit testing framework.
- Add tests for `utils/math.ts`.
- Add gh-actions workflow to run unit tests.
  • Loading branch information
junhaoliao authored Nov 20, 2024
1 parent 9927069 commit 7e6073f
Show file tree
Hide file tree
Showing 6 changed files with 4,019 additions and 1,225 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: "test"

on:
pull_request:
types: ["opened", "reopened", "synchronize"]
push:
schedule:
# Run daily at 00:15 UTC (the 15 is to avoid periods of high load)
- cron: "15 0 * * *"
workflow_dispatch:

permissions: {}

concurrency:
group: "${{github.workflow}}-${{github.ref}}"

# Cancel in-progress jobs for efficiency
cancel-in-progress: true

jobs:
test:
runs-on: "ubuntu-latest"
steps:
- uses: "actions/checkout@v4"
with:
submodules: "recursive"
- uses: "actions/setup-node@v4"
with:
node-version: "22"
cache: "npm"
cache-dependency-path: "./package-lock.json"
- run: "npm clean-install"
- run: "npm run test"
71 changes: 71 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import type {Config} from "jest";
import os from "node:os";
import pathPosix from "node:path/posix";


let PRIMARY_REPORTER: string | [string, Record<string, unknown>] = "default";
if ("undefined" !== typeof process.env.GITHUB_ACTIONS) {
PRIMARY_REPORTER = [
"github-actions",
{silent: false},
];
}
console.log(`Environment variable "GITHUB_ACTIONS"="${process.env.GITHUB_ACTIONS}": ` +
`primary reporter will be "${JSON.stringify(PRIMARY_REPORTER)}".`);

const JEST_CONFIG: Config = {
collectCoverage: true,
collectCoverageFrom: ["src/**/*.{ts,tsx}"],
coverageProvider: "v8",
coverageReporters: ["text"],
coverageThreshold: {
"global": {
branches: 100,
functions: 100,
lines: 100,
},
// eslint-disable-next-line no-warning-comments
// TODO: Remove/adjust the overrides below as more test cases are added.
"src/": {
branches: 0,
functions: 0,
lines: 0,
},
"src/utils/math.ts": {
branches: 100,
functions: 100,
lines: 100,
},
},
displayName: {
name: "yscope-log-viewer",
color: "blue",
},
errorOnDeprecated: true,
maxConcurrency: os.cpus().length,
maxWorkers: "100%",
openHandlesTimeout: 1000,
reporters: [
PRIMARY_REPORTER,
"summary",
],
showSeed: true,
testMatch: [
pathPosix.join(__dirname, "test/**/?(*)test.{ts,tsx}"),
],
testTimeout: 5000,
transform: {
".(ts|tsx)$": [
"ts-jest",
{useESM: true},
],
},
verbose: true,

// NOTE: Extra properties in types such as `Error`, `Map`, or `Set` are not preserved when
// passing payloads between parent and child threads due to serialization.
// See https://jestjs.io/docs/29.6/configuration#workerthreads
workerThreads: true,
};

export default JEST_CONFIG;
Loading

0 comments on commit 7e6073f

Please sign in to comment.