Skip to content

test-js.yml

Comprehensive JavaScript/TypeScript test execution using Vitest with automatic runtime and package manager detection. Supports Node.js and Bun runtimes with npm, pnpm, yarn, and bun package managers. Automatically installs Vitest if not present in dependencies and test files are detected.

Inputs

InputTypeDescriptionRequiredDefault
RUNTIME_VERSIONstringRuntime version to use (Node.js or Bun version)No"22"
PACKAGE_MANAGERstringPackage manager to use (npm, pnpm, yarn, bun)No"npm"
RUNTIMEstringJavaScript runtime to use (node, bun)No"node"
WORKING_DIRECTORYstringWorking directory for the projectNo"."
TEST_COMMANDstringCustom test command to run (defaults to vitest run)No""
COVERAGEbooleanWhether to collect test coverageNofalse
COVERAGE_REPORTERstringCoverage reporter to use (text, lcov, html, json)No"text"
COVERAGE_ARTIFACT_NAMEstringName of the coverage artifactNounit-tests-coverage
COVERAGE_ARTIFACT_PATHstringPath where to download the coverage artifactNo./coverage
FAIL_ON_ERRORbooleanWhether to fail the workflow on test failuresNotrue
TIMEOUTstringTest timeout in millisecondsNo"60000"

Permissions

ScopeAccessDescription
contentsreadRead source files for tests

Notes

  • Automatic Detection: Intelligently detects package manager and runtime based on lock files and configuration files.
  • Multi-Runtime Support: Works with Node.js and Bun runtimes.
  • Multi-Package Manager Support: Supports npm, pnpm, yarn, and bun package managers.
  • Test File Detection: Automatically detects test files (.test., .spec.) and test directories (test/, tests/, tests/).
  • Auto-Installation: Automatically installs Vitest if not present in package.json but test files are found.
  • Coverage Support: Optional test coverage collection with configurable reporters.
  • Flexible Test Commands: Uses package.json test script if available, falls back to direct Vitest execution.
  • Smart Skipping: Skips test execution gracefully when no test files are found.
  • Package Manager Detection: Same logic as lint-js.yml - looks for lock files to determine the appropriate package manager.
  • Runtime Detection: Detects Bun vs Node.js based on lock files and project configuration.

Examples

Simple example

yaml
jobs:
  test:
    uses: this-is-tobi/github-workflows/.github/workflows/test-js.yml@main
    with:
      RUNTIME_VERSION: "20"
      PACKAGE_MANAGER: "pnpm"

With coverage

yaml
jobs:
  test:
    uses: this-is-tobi/github-workflows/.github/workflows/test-js.yml@main
    with:
      COVERAGE: true
      COVERAGE_REPORTER: "lcov"
      RUNTIME_VERSION: "18"

Bun runtime

yaml
jobs:
  test:
    uses: this-is-tobi/github-workflows/.github/workflows/test-js.yml@main
    with:
      RUNTIME: "bun"
      PACKAGE_MANAGER: "bun"
      COVERAGE: true

Custom test command

yaml
jobs:
  test:
    uses: this-is-tobi/github-workflows/.github/workflows/test-js.yml@main
    with:
      TEST_COMMAND: "npm run test:unit"
      TIMEOUT: "120000"

Monorepo testing

yaml
jobs:
  test-frontend:
    uses: this-is-tobi/github-workflows/.github/workflows/test-js.yml@main
    with:
      WORKING_DIRECTORY: "packages/frontend"
      COVERAGE: true
      COVERAGE_REPORTER: "text"
      
  test-backend:
    uses: this-is-tobi/github-workflows/.github/workflows/test-js.yml@main
    with:
      WORKING_DIRECTORY: "packages/backend"
      PACKAGE_MANAGER: "pnpm"
      TIMEOUT: "180000"

Non-blocking tests

yaml
jobs:
  test:
    uses: this-is-tobi/github-workflows/.github/workflows/test-js.yml@main
    with:
      FAIL_ON_ERROR: false
      COVERAGE: true