Skip to content

feat: Platform detection and conditional test skipping #5

@brainkim

Description

@brainkim

Summary

When writing cross-platform tests, we need the ability to:

  1. Detect the current platform at runtime
  2. Conditionally skip tests based on platform

Use Case

ZenDB has tests that use platform-specific features:

  • Some tests use require() which doesn't work in browsers
  • Some tests need database drivers only available in Node/Bun
  • Browser-specific tests (OPFS driver) should only run in browser

Proposed API

import { test, describe, expect, platform, skipIf } from "@b9g/libuild/test";

// Option 1: Platform detection constant
platform; // "bun" | "node" | "chromium" | "firefox" | "webkit"

// Option 2: Platform predicates
import { isBun, isNode, isBrowser } from "@b9g/libuild/test";

// Option 3: skipIf helper
test.skipIf(isBrowser)("uses require()", () => {
  const { NOW } = require("../src/database.js");
  // ...
});

// Option 4: test.skip/describe.skip (already in bun:test and node:test)
if (isBrowser) {
  test.skip("uses require()", () => {});
} else {
  test("uses require()", () => {
    // ...
  });
}

Current Workaround

None - tests either fail on incompatible platforms or we maintain separate test files.

Additional Context

Both bun:test and node:test support test.skip and describe.skip. The browser shim would need this added.

For platform detection, the test runner already knows the platform - it just needs to expose it to the test code (perhaps via define in esbuild).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions