jest: Setting NODE_TLS_REJECT_UNAUTHORIZED=0 doesn't work in jest@22+
đ„ Regression Report
A clear and concise description of what the regression is.
Last working version
Worked up to version: jest@21.2.1 Stopped working in version: jest@22.0.0
To Reproduce
Steps to reproduce the behavior:
- Clone repo https://github.com/trivikr/jest-self-signed-certificate
cd jest-self-signed-certificate
- Change to
jest-21
branch by runninggit checkout jest-21
npm test
is successful- Change to
master
branch by runninggit checkout master
npm test
fails
Expected behavior
I expected test to succeed, it fails with:
FAIL src/__tests__/server.test.js
server
â server sends response (38ms)
â server âș server sends response
self signed certificate
Link to repl or repo (highly encouraged)
https://github.com/trivikr/jest-self-signed-certificate
Run npx envinfo --preset jest
Paste the results here:
npx: installed 1 in 1.231s
System:
OS: macOS High Sierra 10.13.6
CPU: (4) x64 Intel(R) Core(TM) i7-5557U CPU @ 3.10GHz
Binaries:
Node: 12.1.0 - /usr/local/bin/node
Yarn: 1.15.2 - /usr/local/bin/yarn
npm: 6.9.0 - /usr/local/bin/npm
npmPackages:
jest: ^24.8.0 => 24.8.0
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 30
- Comments: 21 (9 by maintainers)
Hey I found a workaround for this in jest 26 (if you are using jsdom). You are going to have to create a custom environment to run jest in which extends jsdom. With the upgrade to jsdom 16, jest is not aware of the âresourcesâ option on the JSDOM constructor, this and a combination of a regression somewhere with process.env which some of this thread has picked up on doesnt allow NODE_TLS_REJECT_UNAUTHORIZED=0 to work anymore.
Steps to patch:
Any update on this request?
After researching this problem, it appears that jest wraps the env object with a proxy. Hence, any change on
process.env
would not be propagated to the real one.https://github.com/facebook/jest/blob/c83051789cc60789f2c575374270236e3a428879/packages/jest-util/src/createProcessObject.ts#L82-L128
I ended up solving the problem by a monkey patching technique which enforces node trusting the self-signed root CA via syswide-cas. i.e.
It looks like it doesnât pick up any change to
process.env
not only toNODE_TLS_REJECT_UNAUTHORIZED
.This is a blocker for us. Weâre trying not to have a flag in our services to turn off SSL. For testing, we have to use self-signed certs. Pretty much a dealbreaker đŠ
for me export NODE_TLS_REJECT_UNAUTHORIZED=0 worked set NODE_TLS_REJECT_UNAUTHORIZED=0 did not
After some experimenting/debugging, I tbh still have no idea why Node doesnât pick up the setting or which change in v22 could have introduced this. As a workaround it seems that it works if you launch Jest with the env var set to 0 straight away. If itâs just a few tests that need this, could also consider doing a separate Jest run for those if you donât want to set it for all tests.
Any update on this regression?
Hi, is there any update on this regression? Or any proposed solution to test https server by setting NODE_TLS_REJECT_UNAUTHORIZED=0
This might block users from updating past Jest v21