question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Setting NODE_TLS_REJECT_UNAUTHORIZED=0 doesn't work in jest@22+

See original GitHub issue

💥 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:

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 

Ref: https://github.com/aws/aws-sdk-js-v3/issues/244

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:30
  • Comments:21 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
chris-buessercommented, Feb 18, 2021

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:

  1. create a custom jsdom environment (save this as custom-jsdom-env.js next to package.json). This is similiar to the jest-environment-jsdom which ships with jest HOWEVER, it includes the “resources” component of JSDOM with the strictSSL set to false, there are two other options in the resources which you can find on jsdom but I have just set this one.
// custom-jsdom-environment
const JSDOMEnvironment = require('jest-environment-jsdom')
const { ResourceLoader, JSDOM, VirtualConsole } = require('jsdom')
const { installCommonGlobals } = require('jest-util')
const { ModuleMocker } = require('jest-mock')
const { LegacyFakeTimers, ModernFakeTimers } = require('@jest/fake-timers')

class CustomEnvironment extends JSDOMEnvironment {
  constructor(config, context) {
    super(config, context)

    this.dom = new JSDOM('<!DOCTYPE html>', {
      pretendToBeVisual: true,
      runScripts: 'dangerously',
      url: config.testURL,
      virtualConsole: new VirtualConsole().sendTo(context.console || console),
      ...config.testEnvironmentOptions,
      resources: new ResourceLoader({ strictSSL: false }),
    })
    const global = (this.global = this.dom.window.document.defaultView)

    if (!global) {
      throw new Error('JSDOM did not return a Window object')
    }

    // for "universal" code (code should use `globalThis`)
    global.global = global

    // Node's error-message stack size is limited at 10, but it's pretty useful
    // to see more than that when a test fails.
    this.global.Error.stackTraceLimit = 100
    installCommonGlobals(global, config.globals)

    // Report uncaught errors.
    this.errorEventListener = (event) => {
      if (userErrorListenerCount === 0 && event.error) {
        process.emit('uncaughtException', event.error)
      }
    }
    global.addEventListener('error', this.errorEventListener)

    // However, don't report them as uncaught if the user listens to 'error' event.
    // In that case, we assume the might have custom error handling logic.
    const originalAddListener = global.addEventListener
    const originalRemoveListener = global.removeEventListener
    let userErrorListenerCount = 0
    global.addEventListener = function(...args) {
      if (args[0] === 'error') {
        userErrorListenerCount++
      }
      return originalAddListener.apply(this, args)
    }
    global.removeEventListener = function(...args) {
      if (args[0] === 'error') {
        userErrorListenerCount--
      }
      return originalRemoveListener.apply(this, args)
    }

    this.moduleMocker = new ModuleMocker(global)

    const timerConfig = {
      idToRef: (id) => id,
      refToId: (ref) => ref,
    }

    this.fakeTimers = new LegacyFakeTimers({
      config,
      global,
      moduleMocker: this.moduleMocker,
      timerConfig,
    })

    this.fakeTimersModern = new ModernFakeTimers({ config, global })
  }

  async setup() {
    await super.setup()
  }

  async teardown() {
    await super.teardown()
  }

  runScript(script) {
    return super.runScript(script)
  }
}

module.exports = CustomEnvironment
  1. Add the testEnvironment to jest settings: You need to set the testEnvironment option to {PATH_TO}/cutom-jsdom-env.js or the --env={PATH_TO}/custom-jsdom-env.js
3reactions
trivikrcommented, Jun 27, 2019

Any update on this request?

Read more comments on GitHub >

github_iconTop Results From Across the Web

node_tls_reject_unauthorized=0 not working - You.com
Node says this warning when I run it: Warning: Setting the NODE_TLS_REJECT_UNAUTHORIZED environment variable to '0' makes TLS connections and HTTPS requests ...
Read more >
node.js - (node:26972) Warning: Setting the ... - Stack Overflow
(node:26972) Warning: Setting the NODE_TLS_REJECT_UNAUTHORIZED environment variable to '0' makes TLS connections and HTTPS requests insecure ...
Read more >
How to Resolve Certificate Errors in a NodeJS App with SSL ...
A practical guide to resolving SSL certificate errors. If you've worked on Node/Express App, you may already know that setting up the app...
Read more >
Warning: Setting the NODE_TLS_REJECT_UNAUTHORIZED ...
Having it set to 0 means that node.js is not verifying that the SSL/TLS certificates have a proper and unbroken path up to...
Read more >
HTTPS | Node.js v19.3.0 Documentation
HTTPS is the HTTP protocol over TLS/SSL. ... options <Object> Set of configurable options to set on the agent. ... Use 0 to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found