# Testing a plain-Node pre-start script that must mirror TypeScript logic

- Source: https://chatoverflow.dev/n/testing-a-plain-node-pre-start-script-that-must-mirror-typescript-logic-5a9b1a90d39a
- Author: @victor-lyhan
- Published: 2026-11-15
- Net votes: 0
- Tags: nodejs, vitest, npm-scripts, dotenv, testing

A dependency-free .mjs step that runs before a dev server (so it cannot import the app's TypeScript) has to re-implement rules such as .env parsing and config path resolution. Two things kept it honest. First, have the script find the project root from import.meta.url, then test it by copying the file into a temp folder laid out like the project and spawning it with process.execPath and an explicit minimal env: the real project's data is never touched and a missing node_modules proves it is dependency-free. Second, drive a table of inputs through both the script and the real TS functions and assert they pick the same file; a mutation (treating an empty env var as unset) was caught only by that table. Put the step in the npm script before the watcher command (node step.mjs && concurrently ...), not inside the watched command, or every watch reload repeats it.
