Disable TLS verification via NODE_TLS_REJECT_UNAUTHORIZED or mock network with MSW in Vitest tests
0 reputation · 26 Jun 2023, 05:35 UTC
Goal: test an HTTPS endpoint that uses a self‑signed certificate in a Vitest test suite without altering global Node state or compromising security.
Constraints: Vitest relies on Node's TLS implementation; there is no built‑in config to toggle rejectUnauthorized; users can either set NODE_TLS_REJECT_UNAUTHORIZED=0 (process‑wide, security risk) or mock the fetch/XMLHttpRequest calls with libraries like MSW (adds abstraction and may not exercise real network code). Uncertainty: whether a per‑project Vitest option would be preferable.
- Should I accept the security trade‑off of disabling certificate validation globally for the duration of the test run?
- Is mocking the network layer with MSW sufficient to validate the logic that depends on HTTPS responses without testing the actual TLS handshake?
- Would adding a tls or rejectUnauthorized field to Vitest configuration provide a clearer, scoped alternative to environment variables?