Files
nicktrn 49a3f72e13 Publish redis-worker and add graceful shutdown manager (#1810)
* add shutdown manager

* update ai test instructions

* add shutdown timeout to redis-worker

* move redis worker to packages

* add unregister method

* prep for publishing package

* fix types

* update ai files

* fix cursor terminal links

* prevent overly friendly ids

* use structured logger

* use unique shutdown handler names

* rework suspend completion

* add trycatch util

* rework suspend restore

* add http server metrics

* add missing prom-client to core

* add prom metrics to redis worker

* bundle redis-worker

* fix esm/cjs interop

* remove proxy from changeset ignore and add supervisor

* add pause to prerelease script for any manual edits

* unregister the correct handler and add early detection

* small change to http handler return

* fix worker tests

* fix shutdown manager tests
2025-03-21 14:53:27 +00:00

2.3 KiB

Running Tests

We use vitest exclusively for testing. To execute tests for a particular workspace, run the following command:

pnpm run test --filter webapp

Prefer running tests on a single file (and first cding into the directory):

cd apps/webapp
pnpm run test ./src/components/Button.test.ts

If you are cd'ing into a directory, you may have to build dependencies first:

pnpm run build --filter webapp
cd apps/webapp
pnpm run test ./src/components/Button.test.ts

Writing Tests

We use vitest for testing. We almost NEVER mock anything. Start with a top-level "describe", and have multiple "it" statements inside of it.

New test files should be placed right next to the file being tested. For example:

  • Source file: ./src/services/MyService.ts
  • Test file: ./src/services/MyService.test.ts

When writing anything that needs redis or postgresql, we have some internal "testcontainers" that are used to spin up a local instance, redis, or both.

redisTest:

import { redisTest } from "@internal/testcontainers";
import { createRedisClient } from "@internal/redis";

describe("redisTest", () => {
  redisTest("should use redis", async ({ redisOptions }) => {
    const redis = createRedisClient(redisOptions);

    await redis.set("test", "test");
    const result = await redis.get("test");
    expect(result).toEqual("test");
  });
});

postgresTest:

import { postgresTest } from "@internal/testcontainers";

describe("postgresTest", () => {
  postgresTest("should use postgres", async ({ prisma }) => {
    // prisma is an instance of PrismaClient
  });
});

containerTest:

import { containerTest } from "@internal/testcontainers";

describe("containerTest", () => {
  containerTest("should use container", async ({ prisma, redisOptions }) => {
    // container has both prisma and redis
  });
});

Dos and Dont's

  • Do not mock anything.
  • Do not use mocks in tests.
  • Do not use spies in tests.
  • Do not use stubs in tests.
  • Do not use fakes in tests.
  • Do not use sinon in tests.
  • Structure each test with a setup, action, and assertion style.
  • Feel free to write long test names.
  • If there is any randomness in the code under test, use seedrandom to make it deterministic by allowing the caller to provide a seed.