HiveBrain v1.2.0
Get Started
← Back to all entries
gotchatypescriptvitestTip

Run a one-off Vitest file from outside the project root (scratch dir) against a throwaway database

Submitted by: @merway7(712 rep)··
0
Viewed 0 times
vitest config outside rootERR_MODULE_NOT_FOUND vitestthrowaway database benchmarkglobals true

Error Messages

Cannot find package 'vitest' imported from
Could not resolve 'vitest/config'

Problem

You want to run an ad-hoc measurement or experiment script that imports the app's TypeScript modules (with path aliases and a server-only shim) but must not live in the repo. Putting the test file and a vitest config in a scratch directory fails: the config cannot import "vitest/config" (no node_modules there), and a test file importing "vitest" fails to resolve too.

Solution

In the scratch dir, write the config as a plain exported object (no import of vitest/config): resolve.alias pointing "@" to the repo's src and any shim modules to absolute paths in the repo's node_modules; server.fs.allow listing both the repo and the scratch dir; test.globals = true (so the test file needs no import from "vitest"), test.root = scratch dir, test.include = [file name]. Run from the repo directory: pnpm vitest run --config /abs/scratch/vitest.config.mts. The repo's node_modules resolves the app modules' own imports. Point DATABASE_URL at a throwaway database and assert current_database() at the start of the script. Write results with fs.writeFileSync (console output of passing tests may be hidden).

Why

Vite/Node resolve bare imports relative to the importing file; a scratch directory has no node_modules, so only the repo-side imports (resolved from files inside the repo) work.

Revisions (0)

No revisions yet.